takepicture hangs on Android 2.3.3

后端 未结 5 1714
感动是毒
感动是毒 2020-12-28 21:33

I have some codes of taking picture which works in Android 2.1 and 2.2. But these codes broke at Android 2.3. After spending time to fix this issue which went in vain, I wou

5条回答
  •  清歌不尽
    2020-12-28 22:09

    I had the exact same problem today when testing our app on a Samsung Exhibit 4G with Android 2.3.3 and solved it using a workaround.

    I don't call takepicture anymore but use the last preview callback as the picture.

    The problem is that the preview callback sends the data buffer using the NV21 format.

    So you have to convert the image using this process: NV21 -> RGB -> Load the Bitmap -> Compress to JPEG

    Our code right now looks like this:

        camera.setPreviewCallback(new PreviewCallback() {
    
            @Override
            public synchronized void onPreviewFrame(byte[] data, Camera arg1) {
                if (!mTakePicture) {
                    CameraPreview.this.invalidate();
                } else {
    
                    if (mTakePictureCallback != null && !mPictureTaken) {
                        int rgb[] = new int[previewSize.width*previewSize.height];
                        decodeYUV420SP(rgb, data, previewSize.width, previewSize.height);
                        Bitmap memoryImage = Bitmap.createBitmap(rgb, previewSize.width, previewSize.height, Bitmap.Config.ARGB_8888);
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        memoryImage.compress(CompressFormat.JPEG, 100, baos);
                        shutterSound();
                        setBackgroundDrawable(new BitmapDrawable(getContext().getResources(), memoryImage));
                        mTakePictureCallback.onPictureTaken(baos.toByteArray(), arg1);
                    }
                    mPictureTaken = true;
                    camera.stopPreview();
                }
            }
        });
    

    The code of decodeYUV420SP is here http://www.41post.com/3470/programming/android-retrieving-the-camera-preview-as-a-pixel-array who found it on Ketai http://code.google.com/p/ketai/

    When you take a picture just set the mTakePicture variable on true

    I'm working on a better version but this should get you going.

提交回复
热议问题