开发者

Android Camera.takePicture() does not return some times?

开发者 https://www.devze.com 2023-04-10 06:57 出处:网络
I am writing an application for android to take pictures. The code does some processing after getting a frame from preview in onPreviewFrame(byte[] data, Camera camera).

I am writing an application for android to take pictures. The code does some processing after getting a frame from preview in onPreviewFrame(byte[] data, Camera camera). The problem is with function takePicture() of android.hardware.Camera that sometimes do exactly what I want and sometimes does not return and no callbacks will be called. When I run the app some times taking the first picture does not return and some times I can take four pictures and the fifth will cause app to hang. There is a simillar thread: problem with taking pictures using the android camera. The suggested solution is to use the frame from last preview but surely it is not a good solution! This problem may raise because takePicture is Asancronous (Android Doc).

I simply call the takePicture() function like this:

public static void takePicture() {
   mCamera.takePicture(null, null, jpegCallback);
}

Here is the link to Logcat output. You can search in the output with phrase "takePicture" and you will see that this function sometimes return and sometimes does not return.

This is the output for when takePicture return:

10-02 19:24:36.570: INFO/ShotSingle(3198): ShotSingle::takePicture start
10-02 19:24:36.570: DEBUG/CameraHal(3198): 2489: takePicture() ENTER
10-02 19:24:36.570: DEBUG/MessageQueue(3198): MQ.put(5,0xb6590,0x0,0x8,0x80b0af55)
10-02 19:24:36.570: DEBUG/MessageQueue(3198): MessageQueue::put EXIT
10-02 19:24:36.578: DEBUG/MessageQueue(3198): MQ.get(5,0xb6590,0x0,0x8,0x80b0af55)
10-02 19:24:36.578: DEBUG/CameraHal(3198): 1458: CameraStop() ENTER
10-02 19:24:36.617: DEBUG/CameraHal(3198): 1543: CameraStop() EXIT
10-02 19:24:36.617: DEBUG/MessageQueue(3198): MQ.put(14,0xb6590,0x0,0x8,0x80b0af55)
10-02 19:24:36.617: DEBUG/MessageQueue(3198): MQ.get(14,0xb6590,0x0,0x8,0x80b0af55)
10-02 19:24:36.617: DEBUG/CameraHal(3198): 2497: takePicture() EXIT
10-02 19:24:36.617: INFO/ShotSingle(3198): ShotSingle::takePicture end
开发者_如何学C

and this is the output for when takePicture does not return:

10-02 19:25:20.671: INFO/ShotSingle(3198): ShotSingle::takePicture start
10-02 19:25:20.671: DEBUG/CameraHal(3198): 2489: takePicture() ENTER
10-02 19:25:20.671: DEBUG/MessageQueue(3198): MQ.put(5,0xb8cb8,0x0,0x8,0x80b0af55)
10-02 19:25:20.671: DEBUG/MessageQueue(3198): MessageQueue::put EXIT
10-02 19:25:21.343: INFO/StatusBarPolicy(3393): onSignalStrengthsChanged
10-02 19:25:22.609: WARN/PowerManagerService(3330): Timer 0x7->0x3|0x7
10-02 19:25:23.062: INFO/AudioStreamOutALSA(3198): (virtual android::status_t android::AudioStreamOutALSA::standby())  enter
10-02 19:25:23.125: ERROR/AudioStreamOutALSA(3198): Output standby called!!. Turn off PCM device.
10-02 19:25:23.125: INFO/ALSAStreamOps(3198): [ALSAStreamOps]codecOff mode = 0
10-02 19:25:23.234: INFO/AudioStreamOutALSA(3198): [AudioOutLock]Relase_wake_Lock 

Does anyone have any explanation or solution for this problem?


It seems that the problem is due to insufficient memory. I solved it by adding System.gc() just before calling takePicture().

System.gc();
CameraParameters.mCamera.takePicture(null, null, jpegCallback);


Please see this. If you are using a preview callback you might solve the problem by removing it before taking the picture.

mCamera.setPreviewCallback(null);
mCamera.takePicture(null, null, mPictureCallback);
0

精彩评论

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

关注公众号