在Android上使用ZXing識別條碼/二維碼

本文來自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

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout android:id="@+id/FrameLayout01"  
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent"  
  4.     xmlns:android="http://schemas.android.com/apk/res/android">  
  5.     <SurfaceView android:layout_height="fill_parent"  
  6.         android:id="@+id/sfvCamera" android:layout_width="fill_parent"></SurfaceView>  
  7.     <RelativeLayout android:id="@+id/RelativeLayout01"  
  8.         android:layout_height="fill_parent" android:layout_width="fill_parent">  
  9.         <ImageView android:id="@+id/ImageView01"  
  10.             android:layout_height="100dip" android:layout_width="160dip"></ImageView>  
  11.         <View android:layout_centerVertical="true"  
  12.             android:layout_centerHorizontal="true" android:layout_width="300dip"  
  13.             android:background="#55FF6666" android:id="@+id/centerView"  
  14.             android:layout_height="180dip"></View>  
  15.         <TextView android:layout_centerHorizontal="true"  
  16.             android:layout_width="wrap_content" android:layout_below="@+id/centerView"  
  17.             android:layout_height="wrap_content" android:text="Scanning..."  
  18.             android:id="@+id/txtScanResult" android:textColor="#FF000000"></TextView>  
  19.     </RelativeLayout>  
  20. </FrameLayout>  
 

 

testCamera.java是主類,負責控制Camera和對圖像作解碼,源碼以下:code

 

  1. package com.testCamera;  
  2. import java.util.Timer;  
  3. import java.util.TimerTask;  
  4. import com.google.zxing.BinaryBitmap;  
  5. import com.google.zxing.MultiFormatReader;  
  6. import com.google.zxing.Result;  
  7. import com.google.zxing.Android.PlanarYUVLuminanceSource;  
  8. import com.google.zxing.common.HybridBinarizer;  
  9. import android.app.Activity;  
  10. import android.graphics.Bitmap;  
  11. import android.hardware.Camera;  
  12. import android.os.Bundle;  
  13. import android.view.SurfaceView;  
  14. import android.view.View;  
相關文章
相關標籤/搜索