本文來自http://blog.csdn.net/hellogv/ ,引用必須註明出處!java
愈來愈多的手機具有自動對焦的拍攝功能,這也意味着這些手機能夠具有條碼掃描的功能.......手機具有條碼掃描的功能,能夠優化購物流程,快速存儲電子名片(二維碼)等。android
本文使用ZXing 1.6實現條碼/二維碼識別。ZXing是個很經典的條碼/二維碼識別的開源類庫,long long ago,就有開發者在J2ME上使用ZXing了,不過要支持JSR-234規範(自動對焦)的手機才能發揮其威力,而目前已經有很多Android手機具有自動對焦的功能。app
本文代碼運行的結果以下,使用91手機助手截圖時,沒法截取SurfaceView的實時圖像:ide
本文使用了ZXing1.6的core,即把\zxing-1.6\core\下的src複製覆蓋工程的src;另外還要使用到\zxing-1.6\android\下的PlanarYUVLuminanceSource.java。 優化
PS:\zxing-1.6\android\ 是BarcodeScanner的源碼,本文程序至關於BarcodeScanner的精簡版,只保留最基本的識別功能。google
源碼目錄結果以下圖,ChecksumException.java下面還有不少源文件,截圖還沒有列出:spa
main.xml源碼以下,main.xml必需要用到FrameLayout才能重疊控件實現「範圍框」的效果:.net
- <?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和對圖像作解碼,源碼以下:code
- 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;