Accessing a USB camera using Android-Camera2 API

alex papa :

I have a UVC camera that want to access and grab frames from in my Android Pie (Android 9) code.

This is the code I'm using to enumerate the cameras connected to the Android phone:

    @Override
    public void onResume()
    {
        CameraManager manager =
                (CameraManager)getSystemService(CAMERA_SERVICE);
        try {
            for (String cameraId : manager.getCameraIdList()) {
                CameraCharacteristics chars
                        = manager.getCameraCharacteristics(cameraId);
                // Do something with the characteristics
                int deviceLevel = chars.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
                Log.d(TAG, " **** device ["+cameraId+"] level:"+deviceLevel);
            }
        } catch(CameraAccessException e){
            e.printStackTrace();
        }
    }

I was hopping on Android 9/Pie (Pixel 3) this shows the connected USB camera. But only two devices get listed, the front and back cameras og the phone.

This is the list of features and permissions in my manifest file (AndroidManifest.xml):

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature android:name="android.hardware.usb.host" />

I cannot find any sample code on the internet how to achieve this, i.e. access the USB camera using on Android 9 and later.

What am I missing to get my code enumerate the USB camera? I have seen some 3rd party UVC libraries for Android, but I don't to use them and want to use native Android code.

alex papa :

I think I found the answer to my own question. I purchased a Samsung Galaxy S10e and the same exact code without any modification returns the USB camera as the first one in the list. That is this line Log.d(TAG, " **** device ["+cameraId+"] level:"+deviceLevel); prints 3 times, one for the USB camera and two of the internal cameras.

My best guess is that most phones designed and manufactured after October 2018 when Android 9 was released do have this feature enabled. The phone that were released about that time or before and then later received an update to Android 9 (Pie) are unlikely to have this feature.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=139316&siteId=1