ZXing開發詳解

博客轉載自:https://blog.csdn.net/skillcollege/article/details/38852183android

什麼是Z*?

在Android平臺作過二維碼相關模塊的確定都熟知ZXing開源項目,Z*是一個開源Java類庫用於解析多種格式的1D/2D條形碼。目標是可以對QR編碼、Data Matrix、UPC的1D條形碼進行解碼。 其提供了多種平臺下的客戶端包括:J2ME、J2SE和Android。其GitHub地址是:傳送門git

Z*項目裏面代碼不少,實現的功能也不少,咱們的應用只須要剝離其中的掃描模塊便可,再多一點也就是生成二維碼的功能;接下來咱們就一塊兒來精簡ZXing項目,最終造成一個小的Demo案例,固然江湖上已經有過N多種版本的ZXing精簡項目,什麼橫屏改豎屏,繪製掃描界面,開啓閃光燈等等,而且許多都是基於ZXing2.3.0來作精簡的,後續有許多更新的版本,包括自動對焦,Camera管理,bug修復等等新功能;筆者使用的是ZXing3.1.0版本,這裏須要說明的就是個人這版Demo絕對是江湖上面尚未出現的,也算是一點點小小的創新把,那就是去除ZXing項目中惱人的ViewFinderView的繪製,使用XML佈局掃描界面,添加掃描動畫,精確計算掃描區域github

克隆Z*項目到本地

git clone https://github.com/zxing/zxing.git

整理ZXing代碼

打開ZXing項目的文件夾,能夠看到以下文件目錄:佈局

其中咱們主要關注2個文件夾裏的內容: 動畫

1. core : Z*項目的核心代碼,能夠新建一個Java工程,而後export成jar來調用。以下圖所示:編碼

免打包便可得到的zxing-3.1.0.jar  猛戳下載 spa

2. android : Android示例工程代碼,成功運行以後就是一個專業的掃碼應用了。以下圖所示:.net

免引入免整理的zxing原始工程 ZXingRawProject  猛戳下載code

可是這樣就讓你知足了,那怎麼能夠說是極致二維碼掃描呢,有木有感受ZXing的掃描框的繪製很不爽啊?自定義的View繪製的很醜,多屏幕適配的時候還常常不兼容,原始項目仍是橫屏模式的,目前你們都習慣豎屏掃描呢。怎麼辦?別怕,我來告訴你,我要將ViewFinderView砍掉,使用xml界面佈局,添加掃描動畫,最終同樣準確無誤的掃描到二維碼數據,只須要對準,是的,毫釐不差的對準就能夠了。xml

精簡Z*代碼,打造極致掃描

1. 去掉Z*中一些和掃描無關的代碼,最終留下的代碼結構以下圖所示,最關鍵的是你看不到ViewFinderView 了

2. 佈局掃描界面,xml代碼以下:

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:background="@android:color/transparent"  
    android:orientation="vertical" >  
  
    <SurfaceView  
        android:id="@+id/capture_preview"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent" />  
  
    <RelativeLayout  
        android:id="@+id/capture_container"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent" >  
  
        <ImageView  
            android:id="@+id/capture_mask_top"  
            android:layout_width="match_parent"  
            android:layout_height="120dp"  
            android:layout_alignParentTop="true"  
            android:background="@drawable/shadow" />  
  
        <RelativeLayout  
            android:id="@+id/capture_crop_view"  
            android:layout_width="200dp"  
            android:layout_height="200dp"  
            android:layout_below="@id/capture_mask_top"  
            android:layout_centerHorizontal="true"  
            android:background="@drawable/qr_code_bg" >  
  
            <ImageView  
                android:id="@+id/capture_scan_line"  
                android:layout_width="match_parent"  
                android:layout_height="wrap_content"  
                android:layout_alignParentTop="true"  
                android:layout_marginBottom="5dp"  
                android:layout_marginTop="5dp"  
                android:src="@drawable/scan_line" />  
        </RelativeLayout>  
  
        <ImageView  
            android:id="@+id/capture_mask_bottom"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:layout_alignParentBottom="true"  
            android:layout_below="@id/capture_crop_view"  
            android:background="@drawable/shadow" />  
  
        <ImageView  
            android:id="@+id/capture_mask_left"  
            android:layout_width="wrap_content"  
            android:layout_height="match_parent"  
            android:layout_above="@id/capture_mask_bottom"  
            android:layout_alignParentLeft="true"  
            android:layout_below="@id/capture_mask_top"  
            android:layout_toLeftOf="@id/capture_crop_view"  
            android:background="@drawable/shadow" />  
  
        <ImageView  
            android:id="@+id/capture_mask_right"  
            android:layout_width="wrap_content"  
            android:layout_height="match_parent"  
            android:layout_above="@id/capture_mask_bottom"  
            android:layout_alignParentRight="true"  
            android:layout_below="@id/capture_mask_top"  
            android:layout_toRightOf="@id/capture_crop_view"  
            android:background="@drawable/shadow" />  
    </RelativeLayout>  
  
</RelativeLayout>

3. 計算截取區域 貼心註解: 若是你沒有看上一篇ZBar掃描中關於掃描區域計算的解釋,那趕忙回去,咱不能急,看完再來接上,不然你會不理解的!傳送門

private void initCrop() {  
    int cameraWidth = cameraManager.getCameraResolution().y;  
    int cameraHeight = cameraManager.getCameraResolution().x;  
  
    /** 獲取佈局中掃描框的位置信息 */  
    int[] location = new int[2];  
    scanCropView.getLocationInWindow(location);  
  
    int cropLeft = location[0];  
    int cropTop = location[1] - getStatusBarHeight();  
  
    int cropWidth = scanCropView.getWidth();  
    int cropHeight = scanCropView.getHeight();  
  
    /** 獲取佈局容器的寬高 */  
    int containerWidth = scanContainer.getWidth();  
    int containerHeight = scanContainer.getHeight();  
  
    /** 計算最終截取的矩形的左上角頂點x座標 */  
    int x = cropLeft * cameraWidth / containerWidth;  
    /** 計算最終截取的矩形的左上角頂點y座標 */  
    int y = cropTop * cameraHeight / containerHeight;  
  
    /** 計算最終截取的矩形的寬度 */  
    int width = cropWidth * cameraWidth / containerWidth;  
    /** 計算最終截取的矩形的高度 */  
    int height = cropHeight * cameraHeight / containerHeight;  
  
    /** 生成最終的截取的矩形 */  
    mCropRect = new Rect(x, y, width + x, height + y);  
}

5. 完整項目代碼: 猛戳下載

相關文章
相關標籤/搜索