越來越多的手機具備自動對焦的拍攝功能,這也意味著這些手機可以具備條碼掃描的功能.......手機具備條碼掃描的功能,可以最佳化購物流程,快速儲存電子名片(二維碼)等。
本文使用ZXing 1.6實現條碼/二維碼識別。ZXing是個很經典的條碼/二維碼識別的開源類庫,long long ago,就有開發人員在J2ME上使用ZXing了,不過要支援JSR-234規範(自動對焦)的手機才能發揮其威力,而目前已經有不少Android手機具備自動對焦的功能。
本文代碼啟動並執行結果如下,使用91手機小幫手時,無法截取SurfaceView的即時映像:
本文使用了ZXing1.6的core,即把zxing-1.6core下的src複製覆蓋工程的src;另外還要使用到zxing-1.6android下的PlanarYUVLuminanceSource.java。
PS:zxing-1.6android 是BarcodeScanner的源碼,本文程式相當於BarcodeScanner的精簡版,只保留最基本的識別功能。
源碼目錄結果如,ChecksumException.java下面還有很多源檔案,尚未列出:
main.xml源碼如下,main.xml必須要用到FrameLayout才能重疊控制項實現“範圍框”的效果:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/FrameLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<SurfaceView android:layout_height="fill_parent"
android:id="@+id/sfvCamera" android:layout_width="fill_parent"></SurfaceView>
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<ImageView android:id="@+id/ImageView01"
android:layout_height="100dip" android:layout_width="160dip"></ImageView>
<View android:layout_centerVertical="true"
android:layout_centerHorizontal="true" android:layout_width="300dip"
android:background="#55FF6666" android:id="@+id/centerView"
android:layout_height="180dip"></View>
<TextView android:layout_centerHorizontal="true"
android:layout_width="wrap_content" android:layout_below="@+id/centerView"
android:layout_height="wrap_content" android:text="Scanning..."
android:id="@+id/txtScanResult" android:textColor="#FF000000"></TextView>
</RelativeLayout>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/FrameLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<SurfaceView android:layout_height="fill_parent"
android:id="@+id/sfvCamera" android:layout_width="fill_parent"></SurfaceView>
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<ImageView android:id="@+id/ImageView01"
android:layout_height="100dip" android:layout_width="160dip"></ImageView>
<View android:layout_centerVertical="true"
android:layout_centerHorizontal="true" android:layout_width="300dip"
android:background="#55FF6666" android:id="@+id/centerView"
android:layout_height="180dip"></View>
<TextView android:layout_centerHorizontal="true"
android:layout_width="wrap_content" android:layout_below="@+id/centerView"
android:layout_height="wrap_content" android:text="Scanning..."
android:id="@+id/txtScanResult" android:textColor="#FF000000"></TextView>
</RelativeLayout>
</FrameLayout>
testCamera.java是主類,負責控制Camera和對映像做解碼,源碼如下:
view plaincopy to clipboardprint?
package com.testCamera;
import java.util.Timer;
import java.util.TimerTask;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.Android.PlanarYUVLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import android.app.Activity;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class testCamera extends Activity {
/** Called when the activity is first created. */
private SurfaceView sfvCamera;
private SFHCamera sfhCamera;
private ImageView imgView;
private View centerView;
private TextView txtScanResult;
private Timer mTimer;
private MyTimerTask mTimerTask;
// 按照標準HVGA
final static int width = 480;
final static int height = 320;
int dstLeft, dstTop, dstWidth, dstHeight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);