Android Camera2 API OpenCamera错误 [英] Android camera2 api openCamera error

查看:307
本文介绍了Android Camera2 API OpenCamera错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用camera2 API制作一个简单的flashlite应用程序. 我正在使用以下代码:

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:

这连续打印了三遍:

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

然后一次:

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

我什至在camera2中都找不到与此特定错误相关的任何内容.是什么原因引起的? 该设备是Redmi note 2. 5.0.2,相机具有手电筒,并且我已在清单中添加了相机权限.

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.

推荐答案

我怀疑您的问题来自CameraDevice.TEMPLATE_MANUAL.并非所有设备都支持此功能.另外,当设备使用旧版支持时,我认为这种方法效果不佳.尝试切换到CameraDevice.TEMPLATE_PREVIEW.

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.

此外,当您不熟悉SufaceTexture时,请不要忘记释放它.

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

这篇关于Android Camera2 API OpenCamera错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆