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

來源:互聯網
上載者:User

 越來越多的手機具備自動對焦的拍攝功能,這也意味著這些手機可以具備條碼掃描的功能.......手機具備條碼掃描的功能,可以最佳化購物流程,快速儲存電子名片(二維碼)等。

      本文使用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);  
       

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.