Home » Android » ZXing android use front camera

ZXing android use front camera

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m trying to build a QR Code reader following this tutorial

http://code.tutsplus.com/tutorials/android-sdk-create-a-barcode-reader–mobile-17162

I managed to get everything working, except that I need the camera to be the front camera of my device instead of the rear camera. I can’t find any place in the tutorial that allows me to change this. I tried following this answer, but I still could not get it to work.

Mainly, my issue is with importing the library. I get the following error.

operator is not allowed for source level below 1.7

When I set my compiler settings to 1.7, I get this

Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead

I’m not exactly very proficient with Android and I apologize if it might not be a good question.

So, any way for me to use ZXing with the front camera in my app? Any links?

Thank you very much.

How to&Answers:

The source code uses Java 7. Android does not require Java <= 6. You can see that the build provided in the project happily feeds Java 7 bytecode to dex and produces a working app. I am not sure what tool you are using that suggests otherwise. Maybe it is old.

You should not need to copy and compile the project’s code though. Why is that necessary? use the core.jar file.

You don’t need any of this to use the front camera. Just invoke by Intent (https://github.com/zxing/zxing/wiki/Scanning-Via-Intent) and set extra SCAN_CAMERA_ID to the ID of the camera you want — usually 1 for the front one.

Example:

        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        intent.putExtra("SCAN_CAMERA_ID", 1);

Answer:

After quite a few search i found how to use the front camera. There is this piece of code in com.google.zxing.client.android.camera.CameraConfigurationManager.java

public void openDriver(SurfaceHolder holder) throws IOException {
Camera theCamera = camera;
if (theCamera == null) {
  theCamera = Camera.open();
  if (theCamera == null) {
    throw new IOException();
  }
  camera = theCamera;
}
theCamera.setPreviewDisplay(holder);

jus change the Camera.open() to Camera.open(1)
worked fine for me

Answer:

If you use IntentIntegrator, you can use setCameraId() to specify the front camera:

IntentIntegrator integrator = new IntentIntegrator(yourActivity);
integrator.setCameraId(1);
integrator.initiateScan();