5

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 .

Robert
  • 39,162
  • 17
  • 99
  • 152
Aravind OR
  • 97
  • 2
  • 10
  • 1
    OpenCamera is open source. Why don't you just check the source code of the app yourself how they access the camera? – Robert May 09 '21 at 10:51
  • 4
    AFAIK, CameraX does not support external cameras (USB or otherwise) at this time. – CommonsWare May 09 '21 at 11:15
  • 1
    https://sourceforge.net/p/opencamera/code/ci/master/tree/app/src/main/java/net/sourceforge/opencamera/cameracontroller/CameraController2.java#l20 They are using Camera2 Apis . I was looking for a cameraX solution . – Aravind OR May 09 '21 at 11:17
  • @CommonsWare Okay . – Aravind OR May 09 '21 at 11:19
  • 1
    I wonder if you'd be able to do it by building a custom `CameraSelector` using `CameraSelector.Builder().addCameraFilter()`, where the `CameraFilter` chooses the camera id of your external camera. You can get the camera id using Camera2 interop: `Camera2CameraInfo.from(cameraInfo).getCameraId()`. – Husayn Hakeem May 10 '21 at 16:17
  • @AravindOR have you found any solution for this problem? – Sejpalsinh Jadeja Nov 14 '21 at 13:49
  • @SejpalsinhJadeja No .. like CommonsWare said ," CameraX does not support external cameras (USB or otherwise) at this time." I used camera 2 apis and it worked . – Aravind OR Nov 24 '21 at 11:45

2 Answers2

1

As of now CameraX does not support the external usb camera. Instead of CameraX you can use Camera 2 API.

1

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.

yongle zhu
  • 73
  • 4
  • This gives me a NPE inside PreviewView, on version 1.1.0 – Erik B Aug 05 '22 at 01:10
  • thank you for your source code i have tried this source code in Redmi note 7s ,redmi note 7a , and moto E still its not working "cameras size: 2" stills hoes 2 my front and back camera but not my EXternal USB camera. but the usb camera is working fine in https://play.google.com/store/apps/details?id=com.shenyaocn.android.usbcamera in this app. is there anything else or am i missing something? – A. Chithrai Dec 14 '22 at 18:48