开发者

SurfaceView getting black when calling Camera.stopPreview()

开发者 https://www.devze.com 2023-03-29 20:52 出处:网络
I am doing some image processing of previews captured by the camera. It is cpu consuming task and I have to stop preview to make it faster. Before new frame is being processed I am calling Camera.stop

I am doing some image processing of previews captured by the camera. It is cpu consuming task and I have to stop preview to make it faster. Before new frame is being processed I am calling Camera.stopPreview() and after Camera.startPreview().

However, I would like to have last captured frame displayed on SurfaceView after stopping the 开发者_开发问答preview. It works 'out of the box' on 2.3 devices, however, SurfaceView gets black after calling Camera.stopPreview() on older versions of SDK. Does anyone know what has changed and what to do?


Yes, this was an improvement of the 2.3.

I had this problem in 2.2 as well, there was no way to work on a preview image despite the fact this was theoretically possible according to the API. To solve this I had to actually take a picture using Camera.takePicture(null, null, Camera.PictureCallback myCallback) (see info here) and then to implement a callback to handle the taken picture. The instance of the class that implements this callback is actually the parameter to pass to Camera.takePicture() and the callback method itself looks like this:

public void onPictureTaken(byte[] JPEGData, Camera camera) {
    final Bitmap bitmap = createBitmapFromView(JPEGData);

    // do something with the Bitmap
}

Doing that way prevents the picture to be saved on the external storage with the regular pictures taken with the camera application. Should you need to serialize the Bitmap you'll have to do it explicitely. But it doesn't prevent the camera's trigger sound from being emitted.

Camera.takePicture() has to be called wile the preview is running. stopPreview() can be called right after.

One thing to be careful with /!\:

Camera.takePicture() is not reentrant (at all). The callback must have returned before any subsequent call of Camera.takePicture(). This was freezing my phone, I had to shutdown and restart it before it to be usable again. As the action was triggered by a button, on my side, I had to shield it with a boolean:

if (!mPictureTaken) {
    mPictureTaken = true; // absolutely NOT reentrant. Any double click sticks the phone otherwise.
    mCameraView.takePicture(callback);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号