Cordova Barcodescanner - Use the front camera

I am using this barcodescanner ( https://github.com/wildabeast/BarcodeScanner ) in an app made with Cordova specifically for use on an android device.

I have the following function set up in JavaScript:

$(document).ready(function(){    
    $('#scanner').click( function(){

        console.log('clicked'); //to see if the function is firing      
        cordova.plugins.barcodeScanner.scan(

        function (result) {
          alert("We got a barcode\n" +
                "Result: " + result.text + "\n" +
                "Format: " + result.format + "\n" +
                "Cancelled: " + result.cancelled);
        }, 
        function (error) {
          alert("Scanning failed: " + error);
        }
    );
    });    
});

      

as stated in the documentation provided for this plugin. I added it to my project using the cordova CLI:

$ cordova add plugin https://github.com/wildabeast/BarcodeScanner.git

      

When I list the available plugins for my projects, I see that it is installed correctly. Also I can use the camera in my app by pressing the button with the correct function and can scan with the rear view camera without any problem.

Is there a way to use the front camera for scanning? If it's not in the plugin, is there a way to set the default camera used by the device in the code to use the default front camera? The application we are developing needs to specifically use only the front camera and not need the back of the camera.

Any help would be appreciated.

+3


source to share


2 answers


Here is a breakdown of all the steps we took to get this right. A large amount of material from various sources is related to this work, but most of it does not take into account that we are JAVA developers and that even some of the smallest JAVA methods are new or even unknown to us. This is a solution from the point of view of web developers.

A working repository can be found here for use: https://github.com/wilcovandeijl/camera_app

Thanks @Leo for your help. Your post got us in the right direction, but here's a more detailed approach we took that worked in the end. If you want to expand on your post in more detail, I would appreciate it if this was a different solution than the one that ultimately worked.

First, we added the barcode scanner plugin to our project using the CMD command line:

cordova plugin add com.phonegap.plugins.barcodescanner

Change the directory to the LibraryProject file that comes by default with the plugin loaded:

cd <project directory>\plugins\com.phonegap.plugins.barcodescanner\src\android\LibraryProject

Add a new file to this directory named local.properties with the path to your SDK i.e.

sdk.dir=C:\\Users\\QQQ\\Documents\\Android\\adt-bundle-windows-x86_64\\sdk

open the file



<Project directory>plugins\com.phonegap.plugins.barcodescanner\src\android\LibraryProject\src\com\google\zxing\client\android\camera\open\GingerbreadOpenCameraInterface.java

edit line 48 as follows: if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)

in CMD Change directory to:

cd <project directory>\plugins\com.phonegap.plugins.barcodescanner\src\android\LibraryProject

In CMD type ant release

. This creates a classes.jar file which can be found in <project directory > \plugins\com.phonegap.plugins.barcodescanner\src\android\LibraryProject\bin

.

If you get an error Unable to resolve target 'android-17'

, check out this solution .

One folder up, delete the file com.google.zxing.client.android.captureactivity.jar

, but don't forget to copy the filename. Place the classes.jar file in this directory (\ plugins \ com.phonegap.plugins.barcodescanner \ src \ android \ LibraryProject) and rename it tocom.google.zxing.client.android.captureactivity.jar

Finally, copy the newly created file com.google.zxing.client.android.captureactivity.jar

to the directory <project directory>\platforms\android\libs

.

You are now ready to run a command cordova build android

in CMD to compile the APK and run it on your device.

Thanks @Leo for your help on this, we really appreciate it.

+5


source


I don't think there is a way to choose which camera to use directly from the BarcodeScanner plugin. The way I solved this problem was to edit the ZXING library that this plugin uses for the scan function. Unfortunately, the ZXING Library that appears when you install the BarcodeScanner plugin is already compiled so you cannot edit the files.

Back up your Android project before making any changes. This is how I got the application to use the front camera:

  • The first thing you need to do is download the BarCodeScanner library from GitHub ( https://github.com/wildabeast/BarcodeScanner ) Android The ZXING library is located in the / src / android / LibraryProject / src directory.
  • Then remove the current ZXING library your Android project is using, in my case it was under "Android Private Libraries" (note your package name as you will need it for the next step).
  • The following copy in the project libraries of the ZXING library you downloaded and remember to name it the same as the one you deleted in the previous step.
  • After the library has been imported, you only need to change one line of code for your project to use the front camera. The file is in the com.google.zxing.client.android.camera.open package and in my case it was named "GingerbreadOpenCameraInterface.java". Line 48 has an if statement:

    if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK)



Just change the "CAMERA_FACING_BACK" parameter to "CAMERA_FACING_FRONT" and your application should use the default camera front.

If you get any errors related to lines containing "import com.google.xzing.client.android.R", just comment them out and the errors should go away.

0


source







All Articles