6

有人能做错什么吗...我想使用 SurfaceView 捕获具有预览屏幕的图像。我能够在表面视图上显示预览。

这是我捕获图像的代码

if (mCameraSession == null && mCameraDevice == null) return;

    ImageReader reader = ImageReader.newInstance(mTexturePreviewSize.getWidth(),
                            mTexturePreviewSize.getHeight(),
                            ImageFormat.JPEG, 1);
    reader.setOnImageAvailableListener(imageAvailableListener, mBackgroundHandler);

    Surface surface = reader.getSurface();
    try {

        CaptureRequest.Builder requestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        requestBuilder.addTarget(surface);
        mCameraSession.capture(requestBuilder.build(), sessionCaptureListener, null);

以下是我收到的错误消息。

java.lang.IllegalArgumentException: Bad argument passed to camera service
        at android.hardware.camera2.utils.CameraBinderDecorator.throwOnError(CameraBinderDecorator.java:114)
        at android.hardware.camera2.utils.CameraBinderDecorator$CameraBinderDecoratorListener.onAfterInvocation(CameraBinderDecorator.java:73)
        at android.hardware.camera2.utils.Decorator.invoke(Decorator.java:81)
        at java.lang.reflect.Proxy.invoke(Proxy.java:397)
        at $Proxy2.submitRequestList(Unknown Source)
        at android.hardware.camera2.impl.CameraDeviceImpl.submitCaptureRequest(CameraDeviceImpl.java:617)
        at android.hardware.camera2.impl.CameraDeviceImpl.capture(CameraDeviceImpl.java:503)
        at android.hardware.camera2.impl.CameraCaptureSessionImpl.capture(CameraCaptureSessionImpl.java:161)
        at Control.CameraApi21Plus.captureSinglePhoto(CameraApi21Plus.java:171)
        at com.CameraActivity$2.onClick(CameraActivity.java:108)

拍摄单张照片功能:

   public void captureSinglePhoto() {

    if (mCameraSession == null && mCameraDevice == null) return;

    ImageReader reader = ImageReader.newInstance(mTexturePreviewSize.getWidth(),
            mTexturePreviewSize.getHeight(),
            ImageFormat.JPEG, 1);
    reader.setOnImageAvailableListener(imageAvailableListener, mBackgroundHandler);

    Surface surface = reader.getSurface();
    try {

        CaptureRequest.Builder requestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        requestBuilder.addTarget(surface);

        mCameraSession.stopRepeating();
        mCameraSession.capture(requestBuilder.build(), sessionCaptureListener, null);

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

private ImageReader.OnImageAvailableListener imageAvailableListener = new ImageReader.OnImageAvailableListener()
{
    @Override
    public void onImageAvailable(ImageReader reader) {
        Image img = reader.acquireLatestImage();
    }
};

private CameraCaptureSession.CaptureCallback sessionCaptureListener = new CameraCaptureSession.CaptureCallback() {
    @Override
    public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
        //super.onCaptureCompleted(session, request, result);
    }
};
4

2 回答 2

8

您只能捕获到为会话配置的表面,因此您应该在创建会话之前准备表面。

在官方文档中:

IllegalArgumentException 如果请求的目标不是 Surfaces 或 Surfaces未配置为此会话的输出;...

就像那里一样,您需要在mImageReader.getSurface()创建会话时准备好表面

           mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
                new CameraCaptureSession.StateCallback() {
                    ...
                }, null);

官方样本:

https://github.com/googlesamples/android-Camera2Basic/tree/master/Application/src/main/java/com/example/android/camera2basic

于 2015-09-11T08:10:18.850 回答
2

在 Camera2 API 中,

引用文档CameraCaptureSession

如果相机设备创建了一个新会话,则关闭前一个会话,并调用其关联的 onClosed 回调。如果在会话关闭后调用所有会话方法,将抛出 IllegalStateException。

因此,正如文档所暗示的那样,您正在调用一个封闭的会话。找出来。我可以帮助你们中的更多人添加更多代码。

另外,我希望你在模仿 Camera2 API 示例代码。如果不是,我建议你看看

于 2015-09-11T06:42:34.377 回答