如何使用android.hardware.camera.external在android中打开usb相机

时间:2017-08-05 15:20:06

标签: android android-camera android-camera2

我已经连接了一台USB摄像头,想要打开并拍照。 我想知道如何使用“android.hardware.camera.external”功能访问该相机。 “manager.getCameraIdList();”返回2个摄像头(正面和背面)的数量,而不是连接的USB摄像头。

2 个答案:

答案 0 :(得分:0)

对我来说,使用官方API工作的USB相机的下一个配置:

相机(旧API)

在某些中国设备上, For Each l As Label In snake If l.Text = rooster(14, 10).Text Then l.Text = rooster(15, 10).Text End If Next 有效,但有时您应强制Camera.CameraInfo.CAMERA_FACING_FRONT为0。

camIdx

Camera2(新API)

类似于Camera2 API:

public static Camera getCameraInstance() {
    Camera c = null;
    try {
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        int cameraCount = Camera.getNumberOfCameras();
        for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
            Camera.getCameraInfo(camIdx, cameraInfo);
            Log.i(TAG, "[Camera] try to open camera camIdx:" + camIdx);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                try {
                    c = Camera.open(camIdx);
                    mCameraIdx = camIdx;
                    Log.i(TAG, "[Camera] camIdx:" + camIdx);
                } catch (RuntimeException e) {
                    Log.e(TAG, "[Camera] failed to open: " + e.getLocalizedMessage());
                }
            }
        }

        if (c == null) {
            Log.i(TAG, "[Camera] forcing open camera with camIdx 0");
            c = Camera.open(0); // force because FACING_FRONT not found
            mCameraIdx = 0;
        }
    } catch (Exception e) {
        Logger.e("TAG", "[Camera] Open camera failed: " + e);
    }
    return c;
}

但根据我的经验,一些Android ROM实现非常糟糕(有时是自定义硬件或中国硬件)然后它不起作用。在某些情况下,您应该在某些USB摄像头前使用public String getCamera(CameraManager manager) { String cameraIndex = "0"; try { for (String cameraId : manager.getCameraIdList()) { CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId); Logger.d(TAG, "cameraId " + cameraId); int cOrientation = characteristics.get(CameraCharacteristics.LENS_FACING); if (cOrientation != CAMERACHOICE) { cameraIndex = cameraId; } } } catch (CameraAccessException e) { e.printStackTrace(); } return cameraIndex; } 驱动程序:https://github.com/saki4510t/UVCCamera

答案 1 :(得分:0)

目前(从Android O开始)通过标准相机API在Android设备上不支持常见的USB摄像头。

一些Android制造商确实对USB摄像头有自己的支持,但很难知道哪些设备可以做什么,有什么不做。