0

I'm using zxing in my android app. When it starts it goes in 16:9 mode. I'm debugging onto a 10' tablet. I'm pretty sure it loads in 16:9 beacause, opening the stock camera application, I can choose either 16:9 resolution and 4:3. When I set 16:9, the image matches exactly the one I get from my app running zxing. I need to set the resolution to 4:3. And I know that it is supported beacuse it shows me the option in the stock camera app. I'm using the front camera. I'm in lollipop. I need help. I've tried this:

Camera camera = Camera.open(1);
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureSize(640, 480);

But when I later try to initiate the zxing IntentIntegrator it fails saying it was not able to access the camera with id 1 (front). I've searched for the equivalent with new camera2 API with no success. Maybe you know how to do it. I'm compiling zxing from the build.gradle in android studio. So I don't think I might be able to edit zxing sources.

Here's my code:

Camera camera = Camera.open(1);
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureSize(640, 480);
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCameraId(1);
integrator.initiateScan();

And here's the error:

https://i.stack.imgur.com/TCJGx.png

enrico
  • 77
  • 1
  • 2
  • 11
  • 1
    You should use parameter.getsupportedsize(); and then you can set Picturesize . do you understand? – shayan Dec 17 '15 at 23:22
  • I already used getsupportedsizes() and got a list of supported sizes in which there was the one I need (640x480). That's why I used directly the code above and then I initiate zxing. But it fails. – enrico Dec 18 '15 at 10:28
  • It seems like zxing is not able to open the camera because I already opened it before to set the picture size. – enrico Dec 18 '15 at 10:30
  • Please insert your code to help you. – shayan Dec 18 '15 at 11:57
  • I've inserted my code – enrico Dec 18 '15 at 12:10
  • see this two link http://stackoverflow.com/questions/7829162/failed-to-connect-to-camera-service AND http://stackoverflow.com/questions/23904459/android-java-lang-runtimeexception-fail-to-connect-to-camera-service – shayan Dec 18 '15 at 20:04
  • 1) uses-permission is a child of manifest in my project 2) As said before I suspect the error is generated by the fact I first open the camera to set picture size and then call zxing (which opens the camera again). But how do I change the resolution otherwise? Is there a way to work on zxing directly? – enrico Dec 18 '15 at 20:18

1 Answers1

0

The IntentIntegrator is just a convenient way to launch some other zxing-based barcode scanner app on your device. It has a very limited set of parameters, and the size is not one of them.

As you suspect, you're getting this error because you have the camera open when the barcode scanner app tries to open it when you launch it with the IntentIntegrator. Unless the IntentIntegrator took a camera instance object to relay to the barcode scanner app, your approach won't work. Setting the size and then closing the camera won't work either, as settings aren't persisted.

If you really need this to work, you likely need to build your own scanning Activity using the zxing library, and then you can set the preview size to whatever you'd like.

Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47
  • Thanks a lot for answering. Could you please tell me where I should start in building my own activity using zxing? And, do you know if the "setScanningRectangle" might help? If I didn't understand it wrong, it sets the sizes of the little scanning rectangle over the entire image. Am I right? Thanks again. – enrico Dec 20 '15 at 18:56
  • Sorry, not familiar enough with zxing to advise further, but the project seems to have quite a bit of documentation that'll hopefully help you. I don't think setScanningRectangle will adjust the preview size, it just sets the area that the scanner will look for barcodes in. – Eddy Talvala Dec 21 '15 at 19:28
  • Thanks again. I'll try. – enrico Dec 21 '15 at 22:55