Search This Blog

10 July 2010

Using the Barcode Scanner

Let's start with a basic app:

F:\work> mvn archetype:generate -DarchetypeCatalog=http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml
2: http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml -> galatea-archetype (null)
Choose a number: : 2
groupId: org.eoti.android.barcode
artifactId: BarcodeTest
version: 1.0-SNAPSHOT
package: org.eoti.android.barcode
 
F:\work> cd BarcodeTest
F:\work\BarcodeTest> mvn clean install

Now, let's modify it. 

Modify your main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/content"
    />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/format"
    />
</LinearLayout>

Modify your activity:

package org.eoti.android.barcode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class BarcodeTestActivity extends Activity
{
    private static String TAG = "BarcodeTest";
    private static final String INTENT_SCAN = "com.google.zxing.client.android.SCAN";
    private static final String SCAN_MODE = "SCAN_MODE";
    private static final String QR_CODE_MODE = "QR_CODE_MODE";
    private static final String SCAN_RESULT = "SCAN_RESULT";
    private static final String SCAN_RESULT_FORMAT = "SCAN_RESULT_FORMAT";
    private static final int REQCODE_SCAN = 0;
    private Handler handler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        handler = new Handler();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        Intent intent = new Intent(INTENT_SCAN);
        intent.putExtra(SCAN_MODE, QR_CODE_MODE);
        startActivityForResult(intent, REQCODE_SCAN);
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == REQCODE_SCAN)
        {
            set(R.id.content, "<b>Contents: </b>" + data.getStringExtra(SCAN_RESULT));
            set(R.id.format, "<b>Format: </b>" + data.getStringExtra(SCAN_RESULT_FORMAT));
        }else{
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    protected void set(final int id, final String text)
    {
        handler.post(new Runnable(){
            public void run() {
                ((TextView)findViewById(id)).setText(Html.fromHtml("<html><body>" + text + "</body></html>"));
            }
        });
    }
}


Unfortunately, we won't be able to test the rest of it in the emulator.  You will need an actual device for that.

Install it on your phone (I do it by using a webserver, but you could use adb) and launch it.

When the screen comes up, click the screen.  That will launch your Barcode Scanner.

Scan a QR CODE (I scanned this one )

It will go back to your test app. 
"Contents" will be the data content (the market url in the above case) and the "Format" will be QR CODE.

No comments:

Post a Comment