Thursday 12 September 2019

Accessing a USB camera using Android-Camera2 API

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.



from Accessing a USB camera using Android-Camera2 API

2 comments:

  1. Hi Hemanth,

    Working on similar problem.

    Did you find a solution? Did u follow this liks and tried all those settings?

    Thanks,
    Madhu Reddy

    ReplyDelete
    Replies
    1. https://source.android.com/devices/camera/external-usb-cameras

      Delete