相机onPictureTaken()有时不会被调用

时间:2014-07-06 16:26:57

标签: android android-camera android-memory

我有一个应用程序发布到Google Play,它在某些时候使用来自后台服务的前置摄像头拍照。现在,我收到用户的抱怨,认为应用程序的这个特定部分不是很一致,我知道这是真的,因为它通常在我致电camera.takePicture()后停止。 - 即没有调用PictureCallback

此处有关于此问题的其他几个问题,herehere,我已经查看了所有这些问题,他们的修复程序对我没有任何帮助。我的情况与其他情况不同,因为在我的应用程序中,onPictureTaken通常被调用,但不时失败(类似于1/10)。

我已经在这个问题上划了2天,没有任何进展。

我还试图评论出我在PictureCapllback中启动的线程,这似乎使它更加一致,但并不完全,所以它可能是某种内存问题。

    ...
    // Take photo
    takePhoto(new MyPictureCallback());
}

private class MyPictureCallback implements Camera.PictureCallback{
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.d(Constants.TAG_ALERT_PROCESS, "onPictureTaken() called");

        // Release camera
        cameraHandler.release();

        // Continue process
        new Thread(new GotPictureRunnable(data)).start();
    }
}

public void takePhoto(Camera.PictureCallback callback) {

    // Create new cameraInfo object
    cameraInfo = new Camera.CameraInfo();

    // Get camera object
    camera = getCamera();

    // If camera is null bail
    if(camera == null){
        return;
    }

    // Set camera options
    setCameraParameters();
    Log.v(Constants.TAG_ALERT_PROCESS, "Camera parameters set");

    try{
        camera.setPreviewTexture(new SurfaceTexture(0)); // Hack for no preview
        Log.v(Constants.TAG_ALERT_PROCESS, "Preview set");
    } catch (Exception e){
        Log.v(Constants.TAG_ALERT_PROCESS, "Failed to set Preview Texture at camera.takePicture()");
        e.printStackTrace();
        return;
    }

    // Start the preview
    camera.startPreview();
    Log.v(Constants.TAG_ALERT_PROCESS, "Preview started");

    // Preview needs time to start
    Utilities.sleepThread(1000);
    Log.v(Constants.TAG_ALERT_PROCESS, "Thread slept for 1000ms");

    // Take picture and pass our callback
    Log.v(Constants.TAG_ALERT_PROCESS, "Calling takePicture()");
    camera.takePicture(null, null, callback);
}

private void setCameraParameters() {

    // Create parameters object
    Camera.Parameters params = this.camera.getParameters();

    // Set picture size
    setPictureSize(params, context);

    // Set jpeg quality
    params.setJpegQuality(getPictureQuality());

    // Set orientation
    params.setRotation(getRotation());

    // Set Parameters
    this.camera.setParameters(params);
}

public void release(){
    if(camera != null){
        camera.release();
        camera = null;
    }
}

登录失败

07-06 18:09:38.134     135-1051/? I/CameraClient﹕ Opening camera 1
07-06 18:09:38.134     135-1051/? I/CameraHAL﹕ camera_device open
07-06 18:09:38.149  16009-19806/com...V/myTag﹕ Got front Camera
07-06 18:09:38.157  16009-19806/com...V/myTag﹕ Set camera parameters
07-06 18:09:38.157  16009-19806/com...V/myTag﹕ Preview set
07-06 18:09:38.540  16009-19806/com...V/myTag﹕ Preview started
07-06 18:09:38.548    135-19815/? D/﹕ PPM: Standby to first shot: Sensor Change completed -  :390.137 ms :  1404662978557 ms
07-06 18:09:39.540  16009-19806/com...V/myTag﹕ Thread slept for 1000ms
07-06 18:09:39.540  16009-19806/com...V/myTag﹕ Calling takePicture()
07-06 18:09:39.774    135-19815/? D/﹕ PPM: Shot to snapshot:  :228.852 ms :  1404662979777 ms
07-06 18:09:39.813    135-19815/? D/﹕ PPM: Shot to Jpeg:  :274.231 ms :  1404662979823 ms
07-06 18:09:39.931      135-135/? E/CameraHAL﹕ Adapter state switch PREVIEW_ACTIVE Invalid Op! event = 0x5
07-06 18:09:40.173      135-135/? I/CameraClient﹕ Destroying camera 1

登录成功

07-06 18:13:21.868      135-502/? I/CameraClient﹕ Opening camera 1
07-06 18:13:21.868      135-502/? I/CameraHAL﹕ camera_device open
07-06 18:13:22.048  16009-19890/com...V/myTag﹕ Got front Camera
07-06 18:13:22.056  16009-19890/com...V/myTag﹕ rotation value = Automatic
07-06 18:13:22.063  16009-19890/com...V/myTag﹕ Set camera parameters
07-06 18:13:22.063  16009-19890/com...V/myTag﹕ Preview set
07-06 18:13:22.517  16009-19890/com...V/myTag﹕ Preview started
07-06 18:13:22.524    135-19893/? D/﹕ PPM: Standby to first shot: Sensor Change completed -  :460.845 ms :  1404663202530 ms
07-06 18:13:23.517  16009-19890/com...V/myTag﹕ Thread slept for 1000ms
07-06 18:13:23.517  16009-19890/com...V/myTag﹕ Calling takePicture()
07-06 18:13:23.727    135-19893/? D/﹕ PPM: Shot to snapshot:  :216.3 ms :  1404663203736 ms
07-06 18:13:23.774    135-19893/? D/﹕ PPM: Shot to Jpeg:  :261.688 ms :  1404663203781 ms
07-06 18:13:23.837  16009-16009/com...D/myTag﹕ onPictureTaken() called // <-- This line is only diff from failure log
07-06 18:13:23.837      135-135/? E/CameraHAL﹕ Adapter state switch PREVIEW_ACTIVE Invalid Op! event = 0x5
07-06 18:13:24.001      135-135/? I/CameraClient﹕ Destroying camera 1

3 个答案:

答案 0 :(得分:2)

您是否使用过表面固定器?从kitkat开始,这是必要的,而不是一种选择。

还可以尝试将回调作为活动的实例变量。

另外看看我遇到的类似问题,我可以使用我在这里提出的第一个解决方案来解决这个问题。 Camera API not working on KITKAT

答案 1 :(得分:1)

我已经解决了类似的问题,我建议您在致电stopPreview之前尝试拨打startPreview方法,然后takePicture

答案 2 :(得分:0)

@GeneralAndroid的回答是正确的。您只需致电startPreview()。 但是您需要在startPreview()之前拨打takePicture()。我的意思是你应该在回调例程中调用它。

即使您在startPreview()之前致电takePicture(),它仍可以正常工作。但预览仍然保持冻结状态。当我在startPreview()中拨打PictureCallback.onPictureTaken()时,我能够重复拍照,每次拍摄后预览也会恢复。

谢谢你@GeneralAndroid,你为我开辟了道路。