W / CameraBase:0上camera.open()调用:连接到相机时发生错误连接到、发生错误、相机、CameraBase

2023-09-12 05:24:39 作者:爱情里不是骗子就是傻子

我正在写一个摄像头应用程序,每当我打电话camera.open()的应用程序崩溃,然后我得到这个错误:

W / CameraBase:0

:在连接到相机时发生错误

下面是我如何打开相机:

 公共无效getCameraInstance(){
    mCamera = NULL;

    尝试
    {
        mCamera = Camera.open(); //尝试获取摄像头实例
    }

    赶上(例外五)
    {
        //相机无法使用(使用或不存在)

    }
}
 

更新:

FPGA采集CameraLink相机Base模式解码输出,附带工程源码和技术支持

如果你正在读这篇文章,请注意,这是原来的摄像头API和不再适用的摄像头API(摄像机2)的最新版本。

您应该使用摄像机2 API从这时开始,因为它具有更强大的功能,也具有更好的图像处理流水线。

解决方案

要使用下面的方法

  android.hardware.Camera.open(INT cameraId)
 

您应该通过cameraId,如果你想要的前置摄像头编号,你可以使用下面的方法

 私人诠释findFrontFacingCamera(){

    //搜索的前置摄像头
    INT numberOfCameras = Camera.getNumberOfCameras();
    的for(int i = 0; I< numberOfCameras;我++){
        CameraInfo信息=新CameraInfo();
        Camera.getCameraInfo(ⅰ,信息);
        如果(info.facing == CameraInfo.CAMERA_FACING_FRONT){
            cameraId =我;
            cameraFront = TRUE;
            打破;
        }
    }
    返回cameraId;
}
 

如果同一个摄像头被其他应用程序打开,这将抛出一个RuntimeException。

您必须调用发布()当您使用相机完成,否则将保持锁定,无法使用其他应用程序。

您的应用程序只能有一次一个Camera对象主动 对于特定的硬件摄像头。

I'm writing a camera app and whenever I call camera.open() the app crashes and then I get this error:

W/CameraBase﹕ An error occurred while connecting to camera: 0

Here is how I'm opening the camera:

public void getCameraInstance(){
    mCamera = null;

    try 
    {
        mCamera = Camera.open(); // attempt to get a Camera instance
    }

    catch (Exception e)
    {
        // Camera is not available (in use or does not exist)

    }
}

UPDATE:

If you are reading this please note that this is for the original camera API and no longer applies the the latest version of the camera api (camera2).

You should use the camera2 api from this point onwards as it has greater functionality and also has a better image processing pipeline.

解决方案

To use the following method

android.hardware.Camera.open(int cameraId)

You should pass cameraId, If you want the front camera Id you can use the following method

private int findFrontFacingCamera() {

    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
            cameraId = i;
            cameraFront = true;
            break;
        }
    }
    return cameraId;
}

If the same camera is opened by other applications, this will throw a RuntimeException.

You must call release() when you are done using the camera, otherwise it will remain locked and be unavailable to other applications.

Your application should only have one Camera object active at a time for a particular hardware camera.