takepicture hangs on Android 2.3.3

后端 未结 5 1680
感动是毒
感动是毒 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.

    0 讨论(0)
  • 2020-12-28 22:17

    I ran into this issue with GB phones, and for me, it turned out to be because I was calling camera.startPreview() right after calling camera.takePicture(), and this was causing some thread locking in Android. The fix was to move camera.startPreview() into the callback passed into camera.takePicture(), such that it only got called once the picture data was in (example code below). This is of course only relevant if you're interested in restarting the preview after the picture is taken.

    // BAD BAD DON'T DO THIS!
    public void myTakePicture(Camera.PictureCallback callback) {
      mCamera.takePicture(null, null, null, callback);
      mCamera.startPreview();
    }
    
    // ...
    // The way that worked for me
    public void myTakePicture(final Camera.PictureCallback callback) {
      mCamera.takePicture(null, null, null, new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] pictureData, Camera camera) {
          callback.onPictureTaken(pictureData, camera);
          mCamera.takePicture();
        }
      });
    }
    

    This not only made the ANR go away when calling takePicture, but also fixed a native crash on startPreview that was happening on some higher end phones (particularly a >=4.3 Nexus 5). Hope it helps!

    0 讨论(0)
  • 2020-12-28 22:22

    I also observed mCamera.takePicture(null, null, handler) to freeze. I tried to clear the preview handler: mCamera.setPreviewCallback(null) before calling takePicture(), and it works now.

    0 讨论(0)
  • 2020-12-28 22:26

    I'm not sure what the setOptimalSize method used in your code, but make sure you have set camera parameter

    mCamera.setPictureSize(captureSize.width, captureSize.height);
    mCamera.setPictureFormat(ImageFormat.JPEG);
    
    0 讨论(0)
  • 2020-12-28 22:32

    The problem lie in the fact that below codes are writen.

    1. There is a PreviewCallback defined,

      PreviewCallback mPreviewCallback = new PreviewCallback() {
      
          @Override
              public void onPreviewFrame(byte[] data, Camera camera) {
                  //Log.i(TAG+".mPreviewCallback.onPreviewFrame", "being called!");
              }
      };
      
    2. mCamera.setPreviewCallback(mPreviewCallback);

    3. mCamera.takePicture()

    That works in 2.1/2.2, but not in 2.3.

    Not sure if Android team supported this way of using camera. If the above flow is expected, then Android team should fix this problem.

    0 讨论(0)
提交回复
热议问题