What we need
I kinda assume that you already have Eclipse installed with the Java/Android SDK. Otherwise, why would you be searching for something like this, right? Anyway, if you don’t have Eclipse and the Android SDK, make sure you download and install that first. The Android docs really do a nice job explaining you how to install it all. Next thing we need to do is download the ZXing library. So goto the following URL and download the latest version (in my case ZXing-2.0.zip): http://code.google.com/p/zxing/downloads/list Once it’s downloaded extract the .zip folder. So now you should have:- an Android project
- Downloaded and extracted the ZXing library
That’s all we need to get started.
Create a basic Android app
Select: Android > Android Application Project and hit Next. In the next screen enter a name for your Application and select the SDK versions you want to build against. I use the following settings:
Adding a button to our app
Ok, so we have a basic application set up. We can now add a button to our application which will later on invoke the ZXing QR reader. From the GUI window drag and drop a button somewhere on your view. Select the button on your view and switch from the Graphical Layout to activity_main.xml.(You can find those tabs near the bottom). You can change the button text there (ideally you shouldn’t hardcode the text into the button, but that’s not important right now). And also add the following code to the button:android:onClick="scanNow"Next we need to open our Java file. In the top left corner of Eclipse we see our project structure. Open the src folder and then expand yourpackage. My package name iscom.example.androidtut, so yours may differ if you entered a different name. Next open the MainActivity.java file and add the following code:
It probably gives you an error that it can’t find View, just let Eclipse works its magic by adding the right import for you. Also remove the unneeded imports at the top of the code. And clean your project if the errors don’t disappear (Project > Clean > Clean All Projects). By now you should be able to do a test run. Deploy your application to your phone and once the app is loaded, hit the newly created button. You should see a message in your LogCat console, like this:publicvoidscanNow(View view){Log.d("test","button works!");}
Integrate the ZXing library
So by now you should have your basic Android app and a button which responds to clicks. It’s now time to add the ZXing library to our project. So will be the most annoying part of the tutorial. Open the ZXing folder and navigate to: zxing-2.0 > android > res In there copy all the contents of the valuesfolder to the values folder in YOUR project.Becareful!!! by replacing existing files. Check the differences and merge them if needed. THe only conflict you’ll probably get is in strings.xml with app_name andmenu_settings. Just keep your settings and remove the duplicates that you got from zxing.- Next copy the contents of the drawable folder to your project. If the folder doesn’t exist, then simply create it.
- After that copy the rawfolder to your project (also place it inside the res folder!).
- Then copy the contents of the Layout folder to the Layouts folder of your project.
- And finally copy the xml folder to your project.
- Import ‘R’ (android)
- Import ‘R’ (com.yourpackage.project)
Finishing the code
And add this code to the scanNow method we made earlier:Below that method add the following method:Intent intent =newIntent("com.google.zxing.client.android.SCAN");intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE","QR_CODE_MODE");startActivityForResult(intent,0);
Next open your AndroidManifest.xml and make sure you have the following:publicvoidonActivityResult(intrequestCode,intresultCode, Intent intent){if(requestCode ==0){if(resultCode == RESULT_OK){String contents = intent.getStringExtra("SCAN_RESULT");String format = intent.getStringExtra("SCAN_RESULT_FORMAT");Log.i("xZing","contents: "+contents+" format: "+format);// Handle successful scan}elseif(resultCode == RESULT_CANCELED){// Handle cancelLog.i("xZing","Cancelled");}}}
That should be it! Now deploy your app to your phone again and hit the button. A scanner should be started.<manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.example.androidtut"android:versionCode="1"android:versionName="1.0"><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="15"/><uses-permissionandroid:name="android.permission.CAMERA"/><uses-permissionandroid:name="android.permission.INTERNET"/><applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme"><activityandroid:name=".MainActivity"android:label="@string/title_activity_main"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application></manifest>