Android (Camera) - How to synchronize stopPreview() with onPictureTaken()?

前端 未结 1 341
灰色年华
灰色年华 2020-12-21 07:37

I have an app in which the client uses the camera to take a picture. The preview of the image is being shown in the tablet using a SurfaceView, before the person hits my \"c

相关标签:
1条回答
  • 2020-12-21 07:51

    Unfortunately you can't acquire a reasonable good preview image just by calling stopPreview() because between the moment the picture is taken and the moment onPictureTaken() is called there can pass quite some time because it works like this:

    • The camera actually takes the picture (that's what you want to preview)
    • onShutter() is called
    • onPictureTaken() for the raw image data is called (on some devices)
    • onPictureTaken() for a scaled preview image is called (on some devices)
    • onPictureTaken() for the final compressed image data is called (the one we are talking about here)

    So you have to convert the byte[] data in your onPictureTaken() callback into a Bitmap and map that Bitmap onto an ImageView that you should position above your SurfaceView to show the still preview image. The code will probably look something like this:

    public void onPictureTaken(byte[] data, Camera camera) {
        camera.stopPreview();
        final Bitmap image = BitmapFactory.decodeByteArray(data, 0, data.length);
        surfaceView.setVisibility(SurfaceView.GONE);
        imageView.setVisibility(ImageView.VISIBLE);
        imageView.setImageBitmap(image);
        reference = data;
        new PictureCallbackHeavy().execute();
    }
    
    0 讨论(0)
提交回复
热议问题