[Andriod] 透過ZXing讀取1D/2D barcode

  • 27676
  • 0

ZXing(Zebra Crossing)使用手機內建相機讀取1D/2D barcode以進行解碼/編碼的open-source, 使用ZXing必須準備以下檔案, ZXing-2.1.zip包含兩個Andriod專案, CaptureActivity及ZXingTestActivity, 我們只需要CaptureActivity, 而兩者都需匯入core-2.1.jar及android-integration-2.1-honeycomb.jar, 若CaptureActivity在手機上無法正常執行, 可Skip STP1-3, 直接安裝BarcodeScanner4.31.apk在手機上, 再建立一個Andriod專案(ZXing)調用CaptureActivity即可

ZXing(Zebra Crossing)使用手機內建相機讀取1D/2D barcode以進行解碼/編碼的open-source, 使用ZXing必須準備以下檔案, ZXing-2.1.zip包含兩個Andriod專案, CaptureActivity及ZXingTestActivity, 我們只需要CaptureActivity, 而兩者都需匯入core-2.1.jar及android-integration-2.1-honeycomb.jar, 若CaptureActivity在手機上無法正常執行, 可Skip STP1-3, 直接安裝BarcodeScanner4.31.apk在手機上, 再建立一個Andriod專案(ZXing)調用CaptureActivity即可

core-2.1.jar
android-integration-2.1-honeycomb.jar
ZXing-2.1.zip
BarcodeScanner4.31.apk(required)
 

STP1. Unzip ZXing-2.1.zip, File>Import>Android>ExistingAndroidCodeIntoWorkspace, 專案匯入後會出現許多import錯誤, 是因為兩個jar尚未匯入

 

STP2. Eclipse, Project>Proerties>JavaBuildPath>Libraries>AddExternalJAR..>OK

 

STP3. Run CaptureActivity(Barcode Scanner)在手機上, 要多run幾次才會成功, 若還是不行, Skip STP1-3, 直接安裝BarcodeScanner4.31.apk在手機上

 

STP4. 建立一個Andriod專案(ZXing)調用CaptureActivity, res/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btScan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:onClick="onClick"
        android:text="ScanSN" />

</RelativeLayout>

 

STP5. src/../MainActivity.java, onClick時呼叫com.google.zxing.client.android.SCAN, 並Override com.google.zxing.client.android.CaptureActivity.onActivityResult

package com.example.zxing;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private static final int ZXING_SCAN = 3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btScan:
			Intent intent = new Intent("com.google.zxing.client.android.SCAN");
			startActivityForResult(intent, ZXING_SCAN);
			break;	
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
		
		super.onActivityResult(requestCode, resultCode, intent); 
		
		if(requestCode == ZXING_SCAN){
			if (resultCode == RESULT_OK) { 
				String contents = intent.getStringExtra("SCAN_RESULT");           
				String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
		        Toast.makeText(this, "contents: "+contents+" format: "+format, Toast.LENGTH_SHORT).show();
	        }else{	
	        	Toast.makeText(this, "RESULT_NOT_OK", Toast.LENGTH_SHORT).show();  	
		    }
		}else{
			Toast.makeText(this, "ZXING_SCAN<>3", Toast.LENGTH_SHORT).show();
		}
	}

}

 

STP6. AndroidManifest.xml, 加入android.permission.CAMERA, 及com.google.zxing.client.android.CaptureActivity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.example.zxing"
	android:versionCode="1"
	android:versionName="1.0" >

	<uses-sdk
		android:minSdkVersion="8"
		android:targetSdkVersion="17" />
    
	<uses-permission android:name="android.permission.CAMERA"/>

	<application
		android:allowBackup="true"
		android:icon="@drawable/ic_launcher"
		android:label="@string/app_name"
		android:theme="@style/AppTheme" >
		<activity
			android:name="com.example.zxing.MainActivity"
			android:label="@string/app_name" >
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
        
        <activity
			android:name="com.google.zxing.client.android.CaptureActivity"
			android:configChanges="orientation|keyboardHidden"    
			android:screenOrientation="landscape"    
			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" />
    		</intent-filter>
    		<intent-filter>
        		<action android:name="com.google.zxing.client.android.SCAN" />
        		<category android:name="android.intent.category.DEFAULT" />
    		</intent-filter>
		</activity>
        
    </application>

</manifest>

 

相關檔案如下

core-2.1.jar.zip

android-integration-2.1-honeycomb.jar.zip

BarcodeScanner4.31.apk.zip

ZXing.zip