gpt4 book ai didi

android - 通过 Camera API 2 捕获图片并将其保存为位图

转载 作者:搜寻专家 更新时间:2023-11-01 08:22:12 24 4
gpt4 key购买 nike

我正在开发一个使用相机拍照然后在另一个 Activity 中显示的应用程序,我尝试了以下代码

public void onImageAvailable(ImageReader imageReader) {
Image image = imageReader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
SetBitmap setBitmap = new SetBitmap();
setBitmap.setMbitmap(myBitmap);
image.close();
}

在此之后我在其他 Activity 中得到了我的位图

    myBitmap = CamActivity.setBitmap.getMbitmap();
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);

SetBitmap是一个设置位图的类

当我点击它时,我有一个捕捉摄像头的按钮,它应该拍照并启动另一个 Activity 来显示图像,但它随后崩溃了那么我现在该怎么办我以错误的方式开始我的 Activity ?或者问题出在位图上?这是错误日志

                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.camtest/com.example.android.camtest.DisplayActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap com.example.android.camtest.SetBitmap.getMbitmap()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap com.example.android.camtest.SetBitmap.getMbitmap()' on a null object reference
at com.example.android.camtest.DisplayActivity.onCreate(DisplayActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
at android.app.ActivityThread.access$900(ActivityThread.java:177) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5942) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

java.lang.IllegalStateException: Handler (android.media.ImageReader$ListenerHandler) {b9cb033} sending message to a Handler on a dead thread
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:325)
at android.os.Handler.enqueueMessage(Handler.java:631)
at android.os.Handler.sendMessageAtTime(Handler.java:600)
at android.os.Handler.sendMessageDelayed(Handler.java:570)
at android.os.Handler.sendEmptyMessageDelayed(Handler.java:534)
at android.os.Handler.sendEmptyMessage(Handler.java:519)
at android.media.ImageReader.postEventFromNative(ImageReader.java:511)
at android.hardware.camera2.legacy.LegacyCameraDevice.nativeProduceFrame(Native Method)
at android.hardware.camera2.legacy.LegacyCameraDevice.produceFrame(LegacyCameraDevice.java:516)
at android.hardware.camera2.legacy.RequestThreadManager$2.onPictureTaken(RequestThreadManager.java:224)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1142)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.hardware.camera2.legacy.CameraDeviceUserShim$CameraLooper.run(CameraDeviceUserShim.java:144)
at java.lang.Thread.run(Thread.java:818)

这是我拍照的地方

private void takePic() {
if (cameraDevice == null)
return;
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraDevice.getId());
Size[] jpegSizes = null;
if (characteristics != null)
jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
.getOutputSizes(ImageFormat.JPEG);

int width = 700;
int height = 600;

if (jpegSizes != null && jpegSizes.length > 0) {
width = jpegSizes[0].getWidth();
height = jpegSizes[0].getHeight();
}
final ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
List<Surface> outputSurface = new ArrayList<>(2);
outputSurface.add(reader.getSurface());
outputSurface.add(new Surface(textureView.getSurfaceTexture()));

final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(reader.getSurface());
captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);

int rotation = getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));


String fileName = "IMG_" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + ".jpg";

File captureDirectory = new File("/Download");

if (!captureDirectory.isDirectory()) captureDirectory.mkdirs();
final File mediaFile = new File(captureDirectory, fileName);

final Uri fileUri = FileProvider.getUriForFile(this,
"com.mydomain.fileprovider",
mediaFile);


ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader imageReader) {

Image mImage = imageReader.acquireLatestImage();
ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
try {
save(bytes);
} catch (IOException e) {
e.printStackTrace();
}
OutputStream output = null;
try {
output = getContentResolver().openOutputStream(fileUri);
output.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
mImage.close();
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


}

private void save(byte[] bytes) throws IOException {
OutputStream output = null;
try {
output = new FileOutputStream(mediaFile);
output.write(bytes);
} finally {
if (output != null)
output.close();
}

}
};
Intent displayIntent = new Intent(this, DisplayActivity.class);
displayIntent.putExtra("FILE_URI", fileUri);

Log.e("uri", fileUri + "");


startActivity(displayIntent);

reader.setOnImageAvailableListener(readerListener, mBackgroundHandler);

final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
Toast.makeText(CamActivity.this, "Saved ", Toast.LENGTH_SHORT).show();

}
};


} catch (CameraAccessException e) {
e.printStackTrace();
}
}

这是显示 Activity

   @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
imageView = findViewById(R.id.imageView);

bundleExtras = getIntent().getExtras();
if(bundleExtras != null){
Uri savedBitmapUri = bundleExtras.getParcelable("FILE_URI");
imageView.setImageURI(savedBitmapUri);
Log.e("uri",savedBitmapUri+"");
} else {
Toast.makeText(this, "null pic", Toast.LENGTH_SHORT).show();

}

}

文件提供者

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="myDownloads" path="Download" />
<root-path name="sdcard1" path="." /> </paths>

最佳答案

您将位图数据传递给另一个 Activity 的方法是错误的。首先,你应该检查Android Activity Lifecycle .当您启动不同的 Activity 时,让我们从 A 中调用它 B,A Activity 将被销毁,所有资源将被销毁。在 Android Activity LifeCycle 文档中,他们提到了这一点:

The onDestroy() callback releases all resources that have not yet been released by earlier callbacks such as onStop().

因此,您无法在 Activity 销毁后访问变量。你应该做的是,你应该将位图保存到文件并通过 intent extra 传递文件 URI,并从另一个 Activity 的 onCreate 回调中获取 URI。

您可以使用 FileProvider 来保存文件,并通过 intent 传递提供者 URI,这是最好的“Android kind”方法,或者您可以保存文件的路径到共享首选项,并从另一个不稳定且不是最佳实践的 Activity 中读取它。

让我们使用 FileProvider:您可以查看 Android Documentation about FileProvider来定义它。

// Assume that you added Captures path to your FileProvider's paths.
// Create temporary image file.
String fileName = "IMG_" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + ".jpg";
File captureDirectory = new File(getFilesDir(), "Captures");
if( !captureDirectory.isDirectory() ) captureDirectory.mkdirs();
File mediaFile = new File(captureDirectory, fileName);

Uri fileUri = FileProvider.getUriForFile(this,
"com.mydomain.fileprovider",
mediaFile);

// Save the Bitmap.
public void onImageAvailable(ImageReader imageReader) {
Image mImage = imageReader.acquireLatestImage();
ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
OutputStream output = null;
try {
output = getContentResolver().openOutputStream(fileUri);
output.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
mImage.close();
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

// Pass the file uri via intent.
Intent displayIntent = new Intent(this, DisplayActivity.class);
displayIntent.putExtra("FILE_URI", mediaFile);
startActivity(displayIntent);

// Read the uri from started activity and show the bitmap
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);

...
bundleExtras = getIntent().getExtras();
if(bundleExtras != null){
Uri savedBitmapUri = bundleExtras.getParcelable("FILE_URI");
mImageView_Preview.setImageURI(savedBitmapUri);
}
else{
// Uri not inside the intent data.
}
...
}

您不应该将所有字节作为位图加载到内存中。您只需将捕获的图像保存到文件中,然后传递从应用程序的 FileProvider 创建的文件 Uri,并使用一行代码显示文件 Uri。

基本优化: - 您可以在缓存目录中创建文件
- 您可以在使用 FileProvider permissions 完成任务后删除文件. - 您可以像 Google 在 android-Camera2Basic 中那样在后台线程上执行保存工作样本。 - 还有更多...

关于android - 通过 Camera API 2 捕获图片并将其保存为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49864119/

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