camera2 api输出黑色图像

时间:2016-10-13 05:32:48

标签: java android android-camera2

我正在使用相机2 api am使用此代码

private void createCameraPreviewSession() {
    try {
        SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
        surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        Surface previewSurface = new Surface(surfaceTexture);
        mPreviewCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewCaptureRequestBuilder.addTarget(previewSurface);

        mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, mImageReader.getSurface()),
                new CameraCaptureSession.StateCallback() {
                    @Override
                    public void onConfigured(CameraCaptureSession session) {

                        if(mCameraDevice == null) {
                            return;
                        }
                        try {

                            mPreviewCaptureRequest = mPreviewCaptureRequestBuilder.build();
                            mCameraCaptureSession = session;
                            mCameraCaptureSession.setRepeatingRequest(
                                    mPreviewCaptureRequest,
                                    mSessionCaptureCallback,
                                    mBackgroundHandler
                            );


                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onConfigureFailed(CameraCaptureSession session) {
                        Toast.makeText(
                                getApplicationContext(),
                                "create camera session failed!",
                                Toast.LENGTH_SHORT
                        ).show();
                    }
                }, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

这是lockfocus方法,我注意到它在关闭图像后产生零大小文件的黑色图像

if(getPackageManager().hasSystemFeature("android.hardware.camera.autofocus")){
                        photoButton.setEnabled(true);
                        mState = STATE__WAIT_LOCK;
                        mPreviewCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
                                CaptureRequest.CONTROL_AF_TRIGGER_START);
                        mPreviewCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
                        mPreviewCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
                        mPreviewCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_START);

                        mCameraCaptureSession.capture(mPreviewCaptureRequestBuilder.build(),
                                mSessionCaptureCallback, mBackgroundHandler);


    this is my capturing an image

    CaptureRequest.Builder captureStillBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
                captureStillBuilder.addTarget(mImageReader.getSurface());

                if (isAutoFocusSupported())
                    mPreviewCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                            CaptureRequest.CONTROL_AF_MODE_AUTO);
                else
                    mPreviewCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                            CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

                int rotation = getWindowManager().getDefaultDisplay().getRotation();
                captureStillBuilder.set(CaptureRequest.JPEG_ORIENTATION,
                        ORIENTATIONS.get(rotation));

                CameraCaptureSession.CaptureCallback captureCallback =
                        new CameraCaptureSession.CaptureCallback() {
                            @Override
                            public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request, long timestamp, long frameNumber) {
                                super.onCaptureStarted(session, request, timestamp, frameNumber);

                                try {
                                        mImageFile = createImageFile();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                                super.onCaptureCompleted(session, request, result);
                                mCaptureResult = result;
                                unLockFocus();
                            }
                        };

                mCameraCaptureSession.stopRepeating();
                mCameraCaptureSession.capture(
                        captureStillBuilder.build(), captureCallback, null
                );

有没有人会遇到这种情况,你是如何解决这个问题的?

0 个答案:

没有答案