Shot in the dark, but has anyone ever had to show ...
# random
c
Shot in the dark, but has anyone ever had to show a usb camera preview in an Android app? Camerax library doesn't seem to support usb cams?
c
I was curious too, so I looked around and found this https://issuetracker.google.com/issues/162382076?pli=1
Depending on your use case you might consider sending video over webrtc on local wifi or something. That would be a hassle on its own but might be more reliable
What is your use case anyway? Are you taking pictures or capturing video?
p
There are 2 APIs to access USB. The Android usb accessory API where Android act as a host. And the standard USB API where Android act as a device. What protocol does the camera uses? Once connected I assume you would copy the raw buffers from the camera device to the camera preview Android buffer, just guessing.
c
I basically have a usb webcam that needs to connect to an android device that doesn't have a camera. I'm taking pictures, but need a live video feed from the camera to show as a preview.
c
What’s the camera being used for?
So, to your original question, I think the answer is no, you can’t do this with CameraX. In general any low-level camera work will require camera2
Have you tried with camera2?
c
I have not tried with camera2. I thought camerax was camera2 lol
or wasn't there like camerax2 or something
c
CameraX is built on top of camera2
c
oh gotcha. TIL
c
Provides more high-level APIs
c
interesting. so maybe ill have to try that
I was able to get camera preview to show when using this library. but i can't use this library long term as it wasn't approved by legal department. https://github.com/jiangdongguo/AndroidUSBCamera
I also tried logging
packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_EXTERNAL)
on our specific device that we have to use and it reports as false.
so i guess my device doesn't handle external cameras... so camera2 wouldn't pick it up anyway?
c
You might get a sense for what’s required
c
yeah. i read through the top answer yesterday
c
Instead of trusting
hasSystemFeature
, just try to use the camera and see if it works
c
👍 1
but okay. at least i wasn't missing something basic here.
well camerax and camera2 apis being different was something i was missing lol
c
No not at all. Trying to use a USB camera definitely qualifies as low-level camera work, so using camera2 makes sense
1
Sorry the above was in response to this
at least i wasn’t missing something basic here.
👍 1
c
Cool. That does give me something to work off of. thank you for your insight!
c
Let me know if it works for you. I’ve done camera work as well so I’d be curious to know the results
The returned
CameraCharacteristics
will allow you to query a bunch of fields: https://developer.android.com/reference/android/hardware/camera2/CameraCharacteristics#fields_1
It also includes
CameraMetadata
which includes this field, which I think you’re interested in: https://developer.android.com/reference/android/hardware/camera2/CameraMetadata#LENS_FACING_EXTERNAL
So… something like this
Copy code
val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
val cameraIds = cameraManager.cameraIdList
val externalCamera = cameraIds.find {  cameraId ->
  cameraManager.getCameraCharacteristics(cameraId).get(CameraMetadata.LENS_FACING_EXTERNAL) === true
}

if (externalCamera != null) {
  cameraManager.openCamera(externalCamera, object : CameraDevice.StateCallback() {
    override fun onOpened(p0: CameraDevice) {
      TODO("Not yet implemented")
    }

    override fun onDisconnected(p0: CameraDevice) {
      TODO("Not yet implemented")
    }

    override fun onError(p0: CameraDevice, p1: Int) {
      TODO("Not yet implemented")
    }
  }, null)
}
If
externalCamera
in the above ends up being null, just try exploring the API a bit. Different device vendors will have some different behaviour, so also don’t hesitate to try different devices
c
Cool. I will try this out when im done putting out this fire for the day. thank you so much Chris!
👍 1
c
Note that the above doesn’t include any of the code for creating a capture session, using SurfaceView, etc. If you’ve never used camera2 you’ll need to spend a bit of time on that
c
Yep. All I really need at this point is to know whether the android device detects the usb device as a potential camera. I have the usb permission prompt working so it does tell me when I plug the camera in.
🦜 1
👍 1
l
There should be a way to access external cameras with CameraX, granted the device already sees them with Camera2 cameraIdList
c
Yeah I should clarify, I’m not familiar with CameraX capabilities. I may have overlooked something