You are here

ZXing QR Reader Direct Integration

Recently I was developing Android application that was supposed to scan QR codes. First I browsed a little, and found nice open source project ZXing (http://code.google.com/p/zxing/) which is quite useful. I developed application that called ZXing Intent for scanning, and everything was working fine. Just customer didn’t like that there needs to be 2 applications installed, and that there is appearing second Intent with big scanning screen with camera output visible on full screen. So I had to integrate ZXing directly into my application, and as I did not find such an example on the web, I am putting my solution here for others.
 

This is step by step tutorial what you need to do, when you want have QR scanner inside your application, and you don’t want to have full screen camera output on the screen:

1)      First go to the ZXing project from here http://code.google.com/p/zxing/downloads/list, and download projects file ZXing-2.0.zip.

2)      From downloaded archive take the android project “android” and jar file core.jar from core directory, and create from it eclipse project:

You must include core.jar into the build path, and you should see all zxing client packages like here.

3)      Then mark this project as Android Library project:

4)      There are needed small changes in the original code.

a.      Set android target to API level 7.

b.      Change class CaptureActivity to non final.

c.      In class CaptureActivity remove calling of function showHelpOnFirstLaunch in onCreate method.

d.      In classes DecodeHandler, CaptureActivityHandler, SearchBookContentsActivity you need to change switch statements that uses resource IDs to if statements. This you can do automatically in eclipse by pressing CTRL+1 when you are on switch statement.

5)      Now create your new project, I call it “scanner” here, and mark that it is using previous library project:

6)      Also include core.jar into the build path.

7)      Now we have two projects, one as library, one as main.

8)      Make your main activity is derived from CaptureActivity
public class ScannerActivity extends CaptureActivity

9)  Copy permissions from android project to scanner project. Maybe not all are needed, but I didn’t research this.
<uses-permissionandroid:name="android.permission.CAMERA"/>

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

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

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

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

<uses-permissionandroid:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>

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

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

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

10) Now we have to include in our layout file main.xml,  qr scanning objects:
 <FrameLayout

    android:layout_width="200dip"

        android:layout_height="200dip"

 android:layout_gravity="center_horizontal">

           <include layout="@layout/capture"/>

       </FrameLayout>

11)   Now we can run the example, and we will see scanning view inside our view.

This is basic integration of the QR reader. If we want to catch the read QR code, we override method handle handleDecodewhere is all processing of scanned code. We can call original routine, or just take the code from parameter rawResult.getText().  Simple showing QR code could be implemented like this.

    @Override

    public void handleDecode(Result rawResult, Bitmap barcode)

    {

       Toast.makeText(this.getApplicationContext(), "Scanned code "+ rawResult.getText(), Toast.LENGTH_LONG);

    }

 

In my application I just took the result, and then called function

handler.sendEmptyMessageDelayed(R.id.restart_preview, CaptureActivity.BULK_MODE_SCAN_DELAY_MS);
 

to ensure  that scanning will continue.  

In addition you can call onPause/onResume if you want to scan only after specific actions (some button pressed), and not all the time. You can also make QR view invisible, or behind your view, so it will not be visible at all.

Here is whole sample code, hope it helps you. 

http://www.androidaz.com/sources.zip

Short News

I have launched Facebook page for Woodebox Puzzle hints and tutorials.

Woodebox Puzzle Free reached 10,000 downloads :)

Small note about Woodebox also on Palmserver.cz.

Woodebox was recommended by famous xda-developers page to its fans.

Our Android Games

Drupal theme by pixeljets.com D7 ver.1.1