Search code examples
androidandroid-cameraandroid-camerax

How to access external USB cameras using Android cameraX?


I was following this codelab to create a cameraX application .

It's working fine in my phone . But the app I am developing is for an android media player.Which doesn't have any inbuilt camera , only an external usb cam is attached .

This is my code to start the camera.

private void startCamera() {
    ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
    Preview preview = new Preview.Builder().build();
    preview.setSurfaceProvider(viewFinder.getSurfaceProvider());
    imageCapture = new ImageCapture.Builder().build();
    cameraProviderFuture.addListener(() -> {
                try {
                    ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
                    cameraProvider.unbindAll();
                    cameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA,preview,imageCapture);
                } catch (ExecutionException e) {
                    e.printStackTrace();
                    Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                catch (IllegalArgumentException e){
                    Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }, ContextCompat.getMainExecutor(this)
    );


}

it's throwing IllegalArgumentException saying no camera connected .

in cameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA,preview,imageCapture);

only CameraSelector.DEFAULT_FRONT_CAMERA and CameraSelector.DEFAULT_BACK_CAMERA available.

How to access an external camera ?

Open Camera app from play store is working fine .


Solution

  • I have resolve the issue, you can find my project code in this site:

    reference JavaApplication Module

    I create a filter:

    @SuppressLint("UnsafeExperimentalUsageError")
    class MyCameraFilter implements CameraFilter {
    
        @SuppressLint("RestrictedApi")
        @NonNull
        @Override
        public LinkedHashSet<Camera> filter(@NonNull LinkedHashSet<Camera> cameras) {
            Log.i(TAG, "cameras size: " + cameras.size());
            Iterator<Camera> cameraIterator = cameras.iterator();
            Camera camera = null;
            while (cameraIterator.hasNext()) {
                camera = cameraIterator.next();
                String getImplementationType = camera.getCameraInfo().getImplementationType();
                Log.i(TAG, "getImplementationType: " + getImplementationType);
            }
            LinkedHashSet linkedHashSet = new LinkedHashSet<>();
            linkedHashSet.add(camera); // 最后一个camera
            return linkedHashSet;
        }
    }
    

    and pass it to CameraSelector:

    cameraSelector = new CameraSelector.Builder().addCameraFilter(new MyCameraFilter()).build();
    

    Now i can access usb camera. My develop android chip is Android API 11 The log "cameras size: " will tell you how many cameras connected to the usb. Default select the last camera.