Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

Scanning Barcodes

PhoneGap provides the ability to scan barcodes through the use of a plugin called Scandit. The following are instructions on how to add the necessary files for Scandit and how to make changes to an existing application so that it can take advantage of this functionality.

Scandit for Android

To begin, you will need to download the Scandit SDK for Android. You must register with Scandit first. The link for this can be found here: Scandit Options

There are 3 options.  For this tutorial, the community edition will suffice. Once you select an option, you will receive instructions on registering and an email confirming your registration. From there, you will be presented with a link to download the SDKs. Download the Android Community Edition SDK.

After the download is finished, extract the contents to a folder on your computer.

Setting Up Your Eclipse Environment

In Eclipse, open the project which you wish to add barcode scanning functionality to. Then, follow these instructions:

1. Open the folder into which you extracted the Scandit SDK.

2. Take the contents inside the libs folder and place them into the libs folder of your project.

3. Copy the res/raw folder inside of the SDK into the res folder of your project.

4. Create a package named com.mirasense.scanditsdk.plugin into your src folder for your project.  Select File->New->Package.

5. Take the ScanditSDK.java file and ScanditSDKActivity.java files located in com.mirasense.demos of the extracted SDK and place them in the newly created package within your project.

6. Inside of ScanditSDKActivity.java, you will find an error on the following line:

import com.mirasense.cordova.R;

You will have to change this to the name of your project's main package. For example:

import com.profoundlogic.exampleproject.R;

The reason for this is the java file included in the SDK is setup to import this file from a demo application. Of course, since you are adding this for your particular project, the com.mirasense.cordova package does not exist and needs to point to your project's package instead.

7. Add the file scanditsdk-barcodepicker-android-3.1.x.jar to the build path. To do this inside of your project, go to libs and right click on this file and select Build Path-> Add to Build Path. To verify this worked correctly, you should now see this file under Referenced Libraries. You shouldn't have to do this for the Cordova file since this is an already existing project, but verify that cordova-2.x.x.jar is also included inside of Referenced Libraries.

8. Add the following line to your res->xml->config file under the <plugins> tag along with all the other plugins:

<plugin name="ScanditSDK" value="com.mirasense.scanditsdk.plugin.ScanditSDK"/>

9. Add the following line to your AndroidManifest.xml file inside of the <Application ....> tag after the closing </activity> tag of the first activity:

<activity android:name="com.mirasense.scanditsdk.plugin.ScanditSDKActivity"/>

10. This option does not apply for anyone with an application that can already access the camera without problem. But otherwise, verify that this line is in the list of permissions in your manifest file; if it is not, add it or otherwise your application will throw an error indicating it cannot access the camera:

<uses-permission android:name="android.permission.CAMERA" />

11. At this point, the scanner can be called by calling cordova.exec with the following arguments:

  • argument #1: function to be called when the scan succeeds.
  • argument #2: function to be called when cancel button is pressed
  • argument #3: name of the plugin, which is "ScanditSDK"
  • argument #4: name of the function of the plugin to be called, which is "scan"
  • argument #5: an array where the first item is the Scandit SDK app key (available from your Scandit SDK web account) and the second item is a dictionary with customization options (listed in ScanditSDK.java)

Below is an example call to cordova.exec():

function scan() {
 cordova.exec(success, failure, "ScanditSDK", "scan",
 ["123456abcdefg",
 {"beep": true,
 "1DScanning": true,
 "2DScanning": true,
 "scanningHotspot": "0.5/0.5",
 "vibrate": true}]);
}
  • No labels