【问题标题】:android deprecated camera classandroid不推荐使用的相机类
【发布时间】:2016-02-03 01:22:46
【问题描述】:

我正在使用已弃用的 android 相机对象。我在这里读到: https://source.android.com/devices/camera/versioning.html

即使相机对象已被弃用,它仍应适用于 android 5.0+ 版本。我问这个,因为我发布了一个使用不推荐使用的相机对象的应用程序,我的朋友下载了该应用程序并且它崩溃了。他们报告了错误,我在跟踪堆栈中看到相机甚至无法打开。这个应用程序可以在我的手机上运行,​​我的手机是 5.0+ 版本。所以也许我没有正确打开相机???或者这些手机可能无法使用已弃用的类。这是我打开相机的代码:

@Override
public void onResume() {
    super.onResume();
    if(!StopTouch)//ignore this condition not relevant to the problem
        StartUpCam();
}

private void StartUpCam()
{

        if(faceProc==null)//ignore this as well
            faceProc = FacialProcessing.getInstance();
        camera=Camera.open(getCid()); //crashes here!!! Calls getCid() which is defined below
        camera.setPreviewCallback(this);
        initPreview(width, height);
        startPreview();
        startTimer();
}

private int getCid()
{
    if(cameraFront && findFrontFacingCamera()>0)
        return findFrontFacingCamera();
    return findBackFacingCamera();
}

public static int findBackFacingCamera() {
    int cameraId = -1;
    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_BACK) {
            cameraId = i;
            break;
        }
    }
    return cameraId;
}

public static int findFrontFacingCamera() {
    int cameraId = -1;
    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;
            break;
        }
    }
    return cameraId;
}

还有其他人在发布他们的应用程序时对已弃用的相机类有疑问吗?有什么想法我在这里做错了吗?

【问题讨论】:

  • 这可能来自我搜索前置摄像头和后置摄像头的方式。 CameraInfo 类也已被弃用,因此可能是因为......

标签: android camera android-camera deprecated


【解决方案1】:

如果您尝试在 Android Marshmallow 中运行它,您需要动态请求权限,而不仅仅是在清单中声明相机权限。 这是需要从棉花糖动态询问的权限列表 http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous

【讨论】:

  • 你说得很好,我的目标 sdk 版本是 23,所以我要试试这个并回复你。
【解决方案2】:

如果您想在您的应用中删除“已弃用”,请使用 camera2 检查文档:

API:android.hardware.camera2

【讨论】:

  • 我有整个应用程序使用相机类,如果不需要,我不想移动到 camera2。这就是我要问的问题,新手机是否不再支持它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-05
  • 1970-01-01
  • 2019-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多