问题
I'm trying to run my standard Android app with QR code scanner on the Raspberry Pi running Android Things version 0.7 with the Raspberry Pi Camera V2 attached.
The app and QR scanner runs fine on most Android phones (Lollipop+).
On Android Things I can load the app and login fine, but when I open the QR scanner tab I get a crash. It's strange because the camera is detected (Camera.getNumberOfCameras() = 1) and the camera appears to have the front-facing orientation (cameraInfo.facing = 1), but when I try to open the camera i get RuntimeException: Fail to connect to camera service (see stacktrace below). Note that I open the camera on a background thread using Rx:
[C:CameraSource] [M:getIdForRequestedCamera] [L:836]: numberOfCameras = 1
[C:CameraSource] [M:getIdForRequestedCamera] [L:839]: id 0, cameraInfo.facing = 1
W/CameraBase: An error occurred while connecting to camera 0: Status(-8): '10: connectHelper:1399: Failed to initialize camera "0": No such device (-19)'
W/System.err: io.reactivex.exceptions.OnErrorNotImplementedException: Fail to connect to camera service
W/System.err: at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
W/System.err: at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
W/System.err: at io.reactivex.internal.observers.ConsumerSingleObserver.onError(ConsumerSingleObserver.java:45)
W/System.err: at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.onError(SingleSubscribeOn.java:73)
W/System.err: at io.reactivex.internal.operators.single.SingleFromCallable.subscribeActual(SingleFromCallable.java:43)
W/System.err: at io.reactivex.Single.subscribe(Single.java:2700)
W/System.err: at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
W/System.err: at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
W/System.err: at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
W/System.err: at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
W/System.err: at java.lang.Thread.run(Thread.java:764)
W/System.err: Caused by: java.lang.RuntimeException: Fail to connect to camera service
W/System.err: at android.hardware.Camera.<init>(Camera.java:519)
W/System.err: at android.hardware.Camera.open(Camera.java:365)
Also note that I'm using the old Android Camera (1) API.
I came across a bug report where someone was trying to do the same thing running an Android app with Camera (1) API on AndroidThings/Rpi. They were able to connect to the same Raspberry Pi camera, but had some issues with the preview: https://issuetracker.google.com/issues/37130806 (More info https://plus.google.com/u/0/+PeterMeijer1/posts/1SKZGiP9jLr and https://plus.google.com/+RonaldVanDerLingen/posts/TE4JtJbHWqF)
Having played with Google Doorbell example app for AndroidThings, it appears that I might need to implement the Camera2 API. However, it looks like some have had success using the Camera (1) API.
Most of all, I'd like to know why the camera is detected, but when I try to open it I get "No such device".
Here's the code:
private static int getIdForRequestedCamera(int facing) {
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < Camera.getNumberOfCameras(); ++i) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == facing) {
return i;
}
}
return -1;
}
private Camera createCamera() {
int requestedCameraId = getIdForRequestedCamera(mFacing);
if (requestedCameraId == -1) {
throw new RuntimeException("Could not find requested
camera.");
}
Camera camera = Camera.open(requestedCameraId);
...
}
来源:https://stackoverflow.com/questions/50188777/cant-connect-camera-using-android-camera-1-api-on-raspberry-pi-running-androi