This tutorial is all about integrating a Barcode (and QRCode) scanning capability in your Android application. Without further ado, LET'S GET STARTED...
Step 1 : Retrieve and Prepare the zxing library
Download the zxing library to download the ZxingLibAndroid.zip file. Using your favorite archiver, extract the zip file's contents anywhere in your drive (e.g. C:\ZxingLibAndroid\). Next, fire up your Eclipse IDE and Import this by going to File > Import > Existing Android Code into Workspace. In the Root Directory textbox, click on the Browse button and select the go to the path where you have extracted the ZxingLibAndroid.zip earlier. you have just downloaded. Once you press the OK button, It should recognize the project (or rather library) inside the zipped file. If you want the library to be copied inside your workspace, Select the Copy projects into Workspace checkbox.
Step 2 : Create a new Android Project and create a reference to our zxing library
Create a new Android Project, with any name you want. After-which, create a reference to the Zxing by Right Clicking your project, then selecting properties. Select Android on the list on left and Press the Add Button. Select the ZxingLibAndroid project. Press Apply then OK.
Step 3 : Editing the manifest file
We have to give our application permissions to use the camera and its flash (if present). To do this, simple open the AndroidManifest.xml and enter these permissions:
...
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
...
<application ...
Next, we have to declare the Activity by adding this entry inside the Application tags
<activity android:configchanges="orientation|keyboardHidden" android:name="com.google.zxing.client.android.CaptureActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowsoftinputmode="stateAlwaysHidden"> <intent-filter> <action android:name="android.intent.action.MAIN"> <category android:name="android.intent.category.DEFAULT"> </category> </action> </intent-filter> <intent-filter> <action android:name="com.google.zxing.client.android.SCAN"> <category android:name="android.intent.category.DEFAULT"> </category> </action> </intent-filter> </activity>
Step 4 : Adding trigger, and handling results
Typically, you launch this functionality by a user action e.g. button click. In this tutorial, we will use the button click to launch our Intent. First off, edit your main.xml layout file and add a TextView and a Button by pasting this code:
In your MainActivity.java, declare this variable in the class level:
then paste this code anywhere inside your OnCreate method:
After doing so, override the onActivityResult by pasting this code in your MainActivity.java:
You're all set! Now, run your application and start Scanning those barcodes!
Now, to explain everything we've been doing. We are launching an Intent whenever we click our Scan button. The intent is filtered by the "com.google.zxing.client.android.SCAN" activity which we declared on the AndroidManifest.xml. We are also telling the System that we are interested in scanning barcodes and QRCodes by calling this line - intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE"). We are also telling the Android System that we are expecting a result from that intent by calling the StartActivityForResult method.
Once the user presses back on the Intent (e.g. closes the scanner when finished), the onActivityResult method is called. If the resultCode equals RESULT_OK, then we populate the results in our TextView by calling this line:
There you have it! I hope this tutorial has helped you in integrating Barcode and QRCode scanning with your Android Application. Feel free to post comments below!
Cheers! Happy coding! =)
Typically, you launch this functionality by a user action e.g. button click. In this tutorial, we will use the button click to launch our Intent. First off, edit your main.xml layout file and add a TextView and a Button by pasting this code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtHello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/marker_default"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world" />
<Button
android:id="@+id/btnscan"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Scan" />
</LinearLayout>
In your MainActivity.java, declare this variable in the class level:
private Button btnscan;
private TextView txtHello;
private final int XZING_REQUEST_CODE = 1000;
then paste this code anywhere inside your OnCreate method:
btnscan = (Button) findViewById(R.id.btnscan);
btnscan.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE");
startActivityForResult(intent, XZING_REQUEST_CODE);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getApplicationContext(), "ERROR:" + e, 1).show();
}
}
});
After doing so, override the onActivityResult by pasting this code in your MainActivity.java:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == XZING_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
txtHello.setText("Format: " + intent.getStringExtra("SCAN_RESULT_FORMAT") + " :: Result: " + intent.getStringExtra("SCAN_RESULT"));
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Scanning cancelled", Toast.LENGTH_SHORT).show();
}
}
}
You're all set! Now, run your application and start Scanning those barcodes!
![]() |
| Main UI |
![]() |
| Scanner UI |
![]() |
| Result UI |
Now, to explain everything we've been doing. We are launching an Intent whenever we click our Scan button. The intent is filtered by the "com.google.zxing.client.android.SCAN" activity which we declared on the AndroidManifest.xml. We are also telling the System that we are interested in scanning barcodes and QRCodes by calling this line - intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE"). We are also telling the Android System that we are expecting a result from that intent by calling the StartActivityForResult method.
Once the user presses back on the Intent (e.g. closes the scanner when finished), the onActivityResult method is called. If the resultCode equals RESULT_OK, then we populate the results in our TextView by calling this line:
There you have it! I hope this tutorial has helped you in integrating Barcode and QRCode scanning with your Android Application. Feel free to post comments below!
Cheers! Happy coding! =)



No comments:
Post a Comment