Android camera2 api openCamera error

会有一股神秘感。 提交于 2019-12-25 07:49:13

问题


I want to make a simple flashlite application using camera2 api. I am using the following code:

public class FlashLightUtilForL {
private CameraCaptureSession mSession;
private CaptureRequest.Builder mBuilder;
private CameraDevice mCameraDevice;
private CameraManager mCameraManager;

public FlashLightUtilForL(Context context) {
    try {
        mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
        //here to judge if flash is available
        CameraCharacteristics cameraCharacteristics = mCameraManager.getCameraCharacteristics("0");
        boolean flashAvailable = cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
        if (flashAvailable) {
            mCameraManager.openCamera("0", new MyCameraDeviceStateCallback(), null);
        } else {
            //todo: throw Exception
        }
        //mCameraManager.openCamera("0", new MyCameraDeviceStateCallback(), null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

class MyCameraDeviceStateCallback extends CameraDevice.StateCallback {

    @Override
    public void onOpened(CameraDevice camera) {
        mCameraDevice = camera;
        //get builder
        try {
            mBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_MANUAL);
            //flash on, default is on
            mBuilder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AF_MODE_AUTO);
            mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
            List<Surface> list = new ArrayList<Surface>();
            SurfaceTexture mSurfaceTexture = new SurfaceTexture(1);
            Size size = getSmallestSize(mCameraDevice.getId());
            mSurfaceTexture.setDefaultBufferSize(size.getWidth(), size.getHeight());
            Surface mSurface = new Surface(mSurfaceTexture);
            list.add(mSurface);
            mBuilder.addTarget(mSurface);
            camera.createCaptureSession(list, new MyCameraCaptureSessionStateCallback(), null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDisconnected(CameraDevice camera) {

    }

    @Override
    public void onError(CameraDevice camera, int error) {

    }
}

private Size getSmallestSize(String cameraId) throws CameraAccessException {
    Size[] outputSizes = mCameraManager.getCameraCharacteristics(cameraId)
            .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
            .getOutputSizes(SurfaceTexture.class);
    if (outputSizes == null || outputSizes.length == 0) {
        throw new IllegalStateException(
                "Camera " + cameraId + "doesn't support any outputSize.");
    }
    Size chosen = outputSizes[0];
    for (Size s : outputSizes) {
        if (chosen.getWidth() >= s.getWidth() && chosen.getHeight() >= s.getHeight()) {
            chosen = s;
        }
    }
    return chosen;
}

/**
 * session callback
 */
class MyCameraCaptureSessionStateCallback extends CameraCaptureSession.StateCallback {

    @Override
    public void onConfigured(CameraCaptureSession session) {
        mSession = session;
        try {
            mSession.setRepeatingRequest(mBuilder.build(), null, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onConfigureFailed(CameraCaptureSession session) {

    }
}

public void turnOnFlashLight() {
    try {
        mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
        mSession.setRepeatingRequest(mBuilder.build(), null, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void turnOffFlashLight() {
    try {
        mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
        mSession.setRepeatingRequest(mBuilder.build(), null, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void close() {
    if (mCameraDevice == null || mSession == null) {
        return;
    }
    mSession.close();
    mCameraDevice.close();
    mCameraDevice = null;
    mSession = null;
}
}

And here is how I use this class from my main activity:

FlashLightUtilForL util = new FlashLightUtilForL(getApplicationContext());
util.turnOnFlashLight();

But it does nothing. I found that there is an error while opening camera, but there is no hint on what this erros is caused by. here is the log:

This is printed three times in a row:

11-10 15:27:32.881 11801-11801/com.flashlight W/ArrayUtils: Ignoring invalid value manual
11-10 15:27:32.881 11801-11801/com.flashlight W/ArrayUtils: Ignoring invalid value fullscan
11-10 15:27:32.881 11801-11801/com.flashlight W/ArrayUtils: Ignoring invalid value nashville
11-10 15:27:32.882 11801-11801/com.flashlight W/ArrayUtils: Ignoring invalid value hefe
11-10 15:27:32.882 11801-11801/com.flashlight W/ArrayUtils: Ignoring invalid value valencia
11-10 15:27:32.882 11801-11801/com.flashlight W/ArrayUtils: Ignoring invalid value xproll
11-10 15:27:32.882 11801-11801/com.flashlight W/ArrayUtils: Ignoring invalid value lofi
11-10 15:27:32.882 11801-11801/com.flashlight W/ArrayUtils: Ignoring invalid value sierra
11-10 15:27:32.882 11801-11801/com.flashlight W/ArrayUtils: Ignoring invalid value walden
11-10 15:27:32.882 11801-11801/com.flashlight W/ArrayUtils: Ignoring invalid value normal

and then once:

11-10 15:27:32.909 11801-11801/com.flashlight I/CameraManager: Using legacy camera HAL.
11-10 15:27:32.911 11801-12695/com.flashlight W/Camera: An error occurred while connecting to camera: 0

I could not even find anything related to this particular error in camera2. What could cause the problem? The device is Redmi note 2 with 5.0.2, camera has flashlight, and I have added the camera permission in manifest.


回答1:


I suspect your issue is from CameraDevice.TEMPLATE_MANUAL. Not all devices support this. Also I do not think this works well when the device is using legacy support. Try switching to CameraDevice.TEMPLATE_PREVIEW.

Also, do not forget to release the SufaceTexture when you are don with it.



来源:https://stackoverflow.com/questions/40529057/android-camera2-api-opencamera-error

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