android.hardware.camera2 CameraManager返回空相机列表

时间:2019-01-10 19:21:59

标签: android camera

我有一个要求相机正常工作的应用程序,最近我在连接USB相机(在Mi Box和Nvidia Shield上测试)时在Android TV设备上遇到了问题。尽管有一个摄像头连接到Android TV,但CameraManager的方法getCameraIdList()通常返回空数组,但在某些奇怪的情况下,它将返回摄像头ID的数组。总结起来,它是这样的:

  • 如果在应用程序运行时将相机连接到设备-CameraManager返回空数组
  • 如果在启动应用程序之前将摄像机连接到网络-CameraManager会重新调整ID的有效数组

要检测是否连接了摄像头设备,我将BroadcastReceiver与SystemActions.USB_DEVICE_ATTACHED操作配合使用:

public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    if (action.equals(SystemActions.USB_DEVICE_ATTACHED)) {

        final UsbDevice connectedDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        Log.d(TAG, "USB device attached: " + (connectedDevice != null ? connectedDevice.toString() : ""));

        if (OsVersionInfo.hasLolliPop() && null != connectedDevice) {

            if (!UsbDeviceHelper.defineWhetherVideoDevice(connectedDevice)) {
                Log.d(TAG, "Attached non-camera device");
                return;
            }

            if (ConfigurationHelper.isAndroidTvMode(App.getAppContext()) && null != getVideo().getCameraManager()
                    && getVideo().getCameraApiVersion(false) == ICameraManager.CAMERA2_API_VERSION) {
                App.getHandler().postDelayed(() -> {
                    Log.d(TAG, "postponing update event");
                    App.getManagers().getHardware().getVideo().getCameraManager().updateCameraList();
                }, 500);
            }
        }
    } else if (action.equals(SystemActions.USB_DEVICE_DETACHED)) {

        onUsbDeviceDetached(intent);

    } else if (action.equals(SystemActions.VOLUME_CHANGED_ACTION)) {
        //   EventBus.getDefault().post(new EventOnVolumeLevelChanged());
    }
}

App.getManagers()。getHardware()。getVideo()。getCameraManager()。updateCameraList()  方法是我更新应用程序的cameraList的地方:

public void updateCameraList() {
        try {
            CameraManager cameraManager = (CameraManager) App.getAppContext().getSystemService(Context.CAMERA_SERVICE);
            mCameraList.setList(cameraManager.getCameraIdList());
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

这是CameraManager返回空数组的地方。

1 个答案:

答案 0 :(得分:0)

我有同样的问题:(

场景:TVBox 与网络摄像头。如果我从连接的网络摄像头开始,它可以正常工作,但是否则

class USBReceiver : BroadcastReceiver(), ILogger {

    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action
        d("USBReceiver: $action")

        when (action) {

            UsbManager.ACTION_USB_DEVICE_ATTACHED,
            UsbManager.ACTION_USB_DEVICE_DETACHED -> {

                val device: UsbDevice? = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE)
                device?.let {
                    getDetails(it)
                }

                checkUSBWebCam()
            }
        }

    }

    private fun getDetails(device: UsbDevice) {
        d("USB: Device -> Model: ${device.deviceName}")
        d("USB: Device -> ID: ${device.deviceId}")
        d("USB: Device -> Vendor: ${device.vendorId}")
    }

    private fun checkUSBWebCam() {

        GlobalScope.launch {

            val typesCamera = ToolsCamera().getCamerasSync()
            val hasCamera = typesCamera.hasCameras
            d("USB: Tiene camara: $hasCamera")

            RxBus.publish(WebcamEvent(hasCamera))
        }
    }
}