gpt4 book ai didi

android - 在android中将图像添加到捕获的图像

转载 作者:行者123 更新时间:2023-11-30 04:33:54 25 4
gpt4 key购买 nike

我正在使用以下代码捕获图像

  public class PictureDemo extends Activity {
private SurfaceView preview=null;
private SurfaceHolder previewHolder=null;
private Camera camera=null;
private boolean inPreview=false;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

save=(Button)findViewById(R.id.save);
save.setOnClickListener(this);
image=(ImageView)findViewById(R.id.image);
image.setImageResource(R.drawable.sofa);
preview=(SurfaceView)findViewById(R.id.preview);
previewHolder=preview.getHolder();
previewHolder.addCallback(surfaceCallback);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void onResume() {
super.onResume();
camera=CameraFinder.INSTANCE.open();
}

@Override
public void onPause() {
if (inPreview) {
camera.stopPreview();
}

camera.release();
camera=null;
inPreview=false;

super.onPause();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.options, menu);

return(super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.camera) {
if (inPreview) {
camera.takePicture(null, null, photoCallback);
inPreview=false;
}

return(true);
}

return(super.onOptionsItemSelected(item));
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_CAMERA ||
keyCode==KeyEvent.KEYCODE_SEARCH) {

return(true);
}

return(super.onKeyDown(keyCode, event));
}

private Camera.Size getBestPreviewSize(int width, int height,
Camera.Parameters parameters) {
Camera.Size result=null;

for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
if (size.width<=width && size.height<=height) {
if (result==null) {
result=size;
}
else {
int resultArea=result.width*result.height;
int newArea=size.width*size.height;

if (newArea>resultArea) {
result=size;
}
}
}
}

return(result);
}

SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(previewHolder);
}
catch (Throwable t) {
Log.e("PictureDemo-surfaceCallback",
"Exception in setPreviewDisplay()", t);
Toast
.makeText(PictureDemo.this, t.getMessage(), Toast.LENGTH_LONG)
.show();
}
}

public void surfaceChanged(SurfaceHolder holder,
int format, int width,
int height) {
Camera.Parameters parameters=camera.getParameters();
Camera.Size size=getBestPreviewSize(width, height,
parameters);

if (size!=null) {
parameters.setPreviewSize(size.width, size.height);
parameters.setPictureFormat(PixelFormat.JPEG);

camera.setParameters(parameters);
camera.startPreview();
inPreview=true;
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
// no-op
}
};

Camera.PictureCallback photoCallback=new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
new SavePhotoTask().execute(data);
camera.startPreview();
inPreview=true;
}
};

class SavePhotoTask extends AsyncTask<byte[], String, String> {
@Override
protected String doInBackground(byte[]... jpeg) {
File photo=new File(Environment.getExternalStorageDirectory(),
"photo.jpg");

if (photo.exists()) {
photo.delete();
}

try {
FileOutputStream fos=new FileOutputStream(photo.getPath());

fos.write(jpeg[0]);
fos.close();
}
catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}

return(null);
}
}
}

xml文件是

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<android.view.SurfaceView
android:id="@+id/surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix"
android:layout_centerInParent="true"
/>

<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:layout_alignParentRight="true"
/>

</FrameLayout>

我可以使用此代码捕获图像,但我需要将提供给 ImageView 的图像添加到捕获的图像中。

如何做到这一点。

提前致谢。

最佳答案

在你的类中声明一个位图

public class PictureDemo extends Activity {
Bitmap bitmap;
...

我想你可以得到你刚刚拍摄的图像:

public void onPictureTaken(byte[] data, Camera camera) {
new SavePhotoTask().execute(data);
camera.startPreview();
inPreview=true;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
}

然后像这样将位图设置到您的 ImageView 中:

image.setImageBitmap(bitmap);

关于android - 在android中将图像添加到捕获的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7226636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com