Camera changes on power button press

93 views
Skip to first unread message

Nathan Wolfe

unread,
Mar 17, 2015, 8:07:29 PM3/17/15
to zx...@googlegroups.com
Hi,

I'm using the front facing camera (id:1) with the zxing QR code reader app but if I press the power button to turn off the tablet screen followed by pressing the power button again to bring back the app it switches which camera it uses to the back facing camera (id:0).

This is the code I run from my android app:
IntentIntegrator ii = new IntentIntegrator(this);
ii.initiateScan(IntentIntegrator.QR_CODE_TYPES, Camera.CameraInfo.CAMERA_FACING_FRONT);

Here are the logs I have gathered from before and after I press the power button:
03-17 19:51:50.700 10096-10096/? I/com.google.zxing.client.android.camera.open.OpenCameraInterface﹕ Opening camera #1
03-17 19:51:56.840 10096-10096/? I/com.google.zxing.client.android.camera.open.OpenCameraInterface﹕ Opening camera #0

I am unsure as to why it does this as simply minimizing the zxing qr code reader (pressing the home button) and bringing it back does not cause the camera to switch.


Thank you for your time, if you need any more information please let me know.
Nathan Wolfe

Nathan Wolfe

unread,
Mar 17, 2015, 8:50:04 PM3/17/15
to zx...@googlegroups.com
Following up on my own post I looked at this one stack overflow post
http://stackoverflow.com/questions/18971811/android-difference-between-pressing-home-button-and-power-button

They say that the difference between pressing the home button and power button is the surfaceView is destroyed when home button is pressed and the surfaceView is not destroyed when the power button is pressed.

Looking at the onResume() method in CaptureActivity the following code is before the camera id is set:

if (hasSurface) {
initCamera(surfaceHolder);
}

and the initCamera function calls:
cameraManager.openDriver(surfaceHolder);

which in turn calls:
if (theCamera == null) {
theCamera = OpenCameraInterface.open(requestedCameraId);
}

and because this code comes before the manual setting of the camera id (later in onResume()):
cameraManager.setManualCameraId(cameraId);

the requestedCameraId is defaulted to -1 which ends up being the back facing camera:
private int requestedCameraId = OpenCameraInterface.NO_REQUESTED_CAMERA;


I'm not sure what the solution of this is, if setting the manual camera id earlier in onResume is an option than that would fix this particular issue I believe.

Hopefully this helps,
Thanks,
Nathan Wolfe

Sean Owen

unread,
Mar 18, 2015, 7:00:31 AM3/18/15
to zx...@googlegroups.com
That seems like a bug, like the manual camera ID should be set earlier. Can you try that? if it works, feel free to open a PR against the open source project to fix that.

Nathan Wolfe

unread,
Mar 20, 2015, 6:11:02 PM3/20/15
to zx...@googlegroups.com
On Wednesday, 18 March 2015 04:00:31 UTC-7, Sean Owen wrote:
> That seems like a bug, like the manual camera ID should be set earlier. Can you try that? if it works, feel free to open a PR against the open source project to fix that.

Okay I was able to changing the order of these operations and it did fix this particular bug. Though I cannot say with confidence this won't cause other potential bugs with different use. I ran mvn package after the changes which I believe runs the tests and it was successful. I forked the repo made the following changes and am going to submit a pull request if that is alright.

Moved the following code to the end of the onResume method in CaptureActivity.java:
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
if (hasSurface) {
// The activity was paused but not stopped, so the surface still exists. Therefore
// surfaceCreated() won't be called, so init the camera here.
initCamera(surfaceHolder);
} else {
// Install the callback and wait for surfaceCreated() to init the camera.
surfaceHolder.addCallback(this);
}


Thanks,
Nathan
Reply all
Reply to author
Forward
0 new messages