在Camera.takePicture()之后没有调用回调

时间:2014-09-15 16:00:43

标签: java android callback

按照Android API文档给出的步骤后,我在调用Camera.takePicture()后没有收到任何回调(没有快门回调或图像回调)。我在清单中声明了正确的权限,我没有任何例外,我可以使用此代码录制视频。关于这个问题的类似问题仅涉及Camera.PictureCallback未被调用,但我没有发现任何未调用Camera.ShutterCallback的问题。这是我的代码:

    private class CameraPreview extends SurfaceView implements SurfaceHolder.Callback,
            Camera.PictureCallback, Camera.ShutterCallback {

        protected SurfaceHolder mHolder;

        @SuppressWarnings("deprecation")
        public CameraPreview() {
            super(VideoActivity.this);
            mHolder = getHolder();
            mHolder.addCallback(this);
            // Deprecated setting is required for API < 11
            // Automatically set for API >= 11, no issue with continuing to use it
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            Log.d(TAG, "Surface is created");
            try {
                mCamera = getCamera();
                Log.d(TAG, "Camera created when surface was created");
                if(mCamera != null) {
                    mCamera.lock();
                    Camera.Parameters parameters = mCamera.getParameters();
                    parameters.setPictureFormat(ImageFormat.JPEG);
                    mCamera.setParameters(parameters);
                    mCamera.setPreviewDisplay(mHolder);
                    mCamera.startPreview();
                    Log.d(TAG, "Preview Started");
                }
            } catch(IOException e) {
                Log.e(TAG, "IOException in setting camera preview: " + e.getMessage(), e);
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) { }

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {

            Log.d(TAG, "onPictureTaken called");
            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            if(pictureFile == null) {
                Log.e(TAG, "Error creating media file");
                return;
            }
            Log.d(TAG, "Writing to file: " + pictureFile);

            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.e(TAG, "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.e(TAG, "Error accessing file: " + e.getMessage());
            }

    }

    @Override
    public void onShutter() {
        Log.d(TAG, "onShuttter is called");
    }
}

该类的实例在onStart()中初始化,并存储在名为mPreview的实例变量中。 mPreview实现了Camera回调接口,因此我可以通过这种方式使用它们。 CameraPreview课程一直致力于录制视频。 拍照的逻辑:

mCamera.lock();
mCamera.takePicture(mPreview, null, mPreview);
Toast.makeText(VideoActivity.this, "Picture Taken",
        Toast.LENGTH_SHORT).show();
Log.d(TAG, "Picture Taken");

所以我的问题是,为什么我没有得到任何回调?

1 个答案:

答案 0 :(得分:0)

我刚才发现了这个问题而忘了分享它,所以这里是:

我需要在mCamera.reconnect()之后和mCamera.startPreview()之前致电mCamera.takePicture()。我相信这是因为我接收到来自另一个进程的回调以拍摄照片,并且在另一个进程使用相机后需要调用重新连接。