首页 > 解决方案 > 我的应用无法识别 USB 外接摄像头(可以识别前后摄像头,即使我可以使用它们),我正在使用 Android Camera2 API

问题描述

我的应用无法识别 USB 外接摄像头(可以识别前后摄像头,即使我可以使用它们),我正在使用 Android Camera2 API。我没有使用它(ICameraProvider 接口),因为我真的不知道如何使用它。我已经在motorola one action (Android 10.0) 和 LG 3 stylus (Android 7.0) 上对其进行了测试。

第一个问题:我需要使用ICameraProvider接口吗,怎么用? https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/master/camera/provider/2.4/ICameraProvider.hal

第二个问题:“External USB Cameras”Android 文档的实现部分是针对开发者的,还是我应该做一些步骤?https://source.android.com/devices/camera/external-usb-cameras

第三个问题:我应该根我的手机吗?

我的一些代码(此代码仅识别前后摄像头,但不识别 USB 摄像头):

package com.example.camera10;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;



import java.util.HashMap;


@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class IdCameras extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_id_cameras);

        Intent intent = getIntent();




        CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        try {
            String[] idList = manager.getCameraIdList();

            Log.d("Available Cameras", String.valueOf(idList.length));

            TextView textView = findViewById(R.id.textView);
            textView.setText(String.valueOf(idList.length));

            int maxCameraCnt = idList.length;

            for (int index = 0; index < maxCameraCnt; index++) {
                String cameraId = manager.getCameraIdList()[index];



                Log.d("Camera", cameraId);
                CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
                StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }

        // Capture the layout's TextView and set the string as its text

    }
}

在此处输入图像描述

标签: javaandroid-camera2

解决方案


Android 手机不强制支持外部网络摄像头。您找到的说明适用于制造商,以帮助他们使网络摄像头显示在 camera2 API 中。

如果制造商没有决定支持网络摄像头,您的选择是使用 USB API 并在您的应用程序中实现完整的 UVC 网络摄像头堆栈(有可用的库)。或者,如果您只关心特定的 Android 设备,并且可以为其制作自定义图像,则可以按照您找到的说明在设备上启用网络摄像头支持。


推荐阅读