Android: Camera2 Bad argument passed to camera service

梦想的初衷 提交于 2019-12-07 03:19:34

问题


Can somebody what I have done wrong... I wanted to capture the image which I have a preview screen using SurfaceView. I am able to show the preview on the surface view.

This is my code to capture the image

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);

Below is the error message i received.

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)

Capturing a single photo function:

   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);
    }
};

回答1:


You can only capture to a surface that is configured for the session, so you should prepare the surface before the session is created.

In the official document:

IllegalArgumentException if the request targets no Surfaces or Surfaces that are not configured as outputs for this session; ...

Just like there, you need to have the surface mImageReader.getSurface() ready when you create the session

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

official sample:

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




回答2:


In Camera2 API,

Quoting from the documentation of CameraCaptureSession,

If a new session is created by the camera device, then the previous session is closed, and its associated onClosed callback will be invoked. All of the session methods will throw an IllegalStateException if called once the session is closed.

So, as the documentation implies, you are calling a closed session. find this out. I can help a bit more of you put more code.

And also, I hope that you are imitating the Camera2 API sample code.If not I suggest you to have a look at it.



来源:https://stackoverflow.com/questions/32516539/android-camera2-bad-argument-passed-to-camera-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!