android - 通过Camera API 2捕获图片并保存为Bitmap

标签 android android-intent bitmap camera

我正在开发一个使用相机拍照然后在另一个 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 类型”方法,或者您也可以保存文件的路径共享首选项,并从另一个不稳定且不是最佳实践的 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捕获图片并保存为Bitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49864119/

相关文章:

android - ImageButton.setVisibility() 问题

java - Java 文件和文档文件的区别

安卓相机 : Does it have a unique package name?

android - ActiveAndroid 模型 bundle 额外异常

c# - 为什么 FastBitmap 没有被垃圾回收?

android - 使用 drawBitmap 缩放位图

android - 游戏因错误而崩溃 -> onRealTimeMessageSent(未知来源)

Android - Retrofit : java. security.cert.CertPathValidatorException:找不到证书路径的信任 anchor

android - Bundel.getString ("") 在 onActivityResult() 给出空值

android - 翻译用于绘制位图的矩阵时,它会绘制多个位图