1. 程式人生 > >Android 基於google Zxing實現二維碼、條形碼掃描,仿微信二維碼掃描效果(現在正做個掃描App、收藏)

Android 基於google Zxing實現二維碼、條形碼掃描,仿微信二維碼掃描效果(現在正做個掃描App、收藏)

瞭解二維碼這個東西還是從微信中,當時微信推出二維碼掃描功能,自己感覺挺新穎的,從一張圖片中掃一下竟然能直接加好友,不可思議啊,那時候還不瞭解二維碼,呵呵,然後做專案的時候,老闆說要加上二維碼掃描功能,然後自己的屁顛屁顛的去百度,google啥的,發現很多朋友都有介紹二維碼掃描的功能,然後我就跟著人家的介紹自己搞起了二維碼掃描功能,跟著人家的帖子,很快我的專案就加入了掃描二維碼的功能,然後自己還很開心。

隨著微信的到來,二維碼越來越火爆,隨處能看到二維碼,比如商城裡面,肯德基,餐廳等等,對於二維碼掃描我們使用的是google的開源框架Zxing,我們可以去http://code.google.com/p/zxing/

下載原始碼和Jar包,之前我專案中的二維碼掃描功能只實現了掃描功能,其UI真的是其醜無比,一個好的應用軟體,其UI介面也要被大眾所接納,不然人家就不會用你的軟體啦,所以說應用軟體功能和介面一樣都很重要,例如微信,相信微信UI被很多應用軟體所模仿,我也仿照微信掃描二維碼效果進行模仿,雖然沒有微信做的那麼精緻,但是效果還是可以的,所以將自己修改UI的程式碼和掃描二維碼的程式碼分享給大家,一是自己以後專案遇到同樣的功能直接拷貝來用,二是給還沒有加入二維碼功能的人一個參考,站在巨人的肩膀上,哈哈,我之前也是站在巨人的肩膀上加上此功能,接下來跟著我一步一步來實現此項功能,裡面去除了很多不必要的檔案

我們先看下專案的結構


  • 如果你專案也想加入此功能,你直接將com.mining.app.zxing.camera,com.mining.app.zxing.decoding,com.mining.app.zxing.view這三個包拷貝到你的專案中,然後引入相對應的資源進去,我也是從我的專案中直接引用過來的,包名都沒改呢,當然還需要引用Zxing.jar
  • com.example.qr_codescan包裡面有一個MipcaActivityCapture,也是直接引入我之前專案的程式碼的,這個Activity主要處理掃描介面的類,比如,掃描成功有聲音和振動等等,主要關注裡面的handleDecode(Result result, Bitmap barcode)方法,掃描完成之後將掃描到的結果和二維碼的bitmap當初引數傳遞到
    handleDecode(Result result, Bitmap barcode)裡面,我們只需要在裡面寫出相對應的處理程式碼即可,其他的地方都不用改得,我這裡處理掃描結果和掃描拍的照片
  1. /** 
  2.  * 處理掃描結果 
  3.  * @param result 
  4.  * @param barcode 
  5.  */
  6. publicvoid handleDecode(Result result, Bitmap barcode) {  
  7.     inactivityTimer.onActivity();  
  8.     playBeepSoundAndVibrate();  
  9.     String resultString = result.getText();  
  10.     if (resultString.equals("")) {  
  11.         Toast.makeText(MipcaActivityCapture.this"Scan failed!", Toast.LENGTH_SHORT).show();  
  12.     }else {  
  13.         Intent resultIntent = new Intent();  
  14.         Bundle bundle = new Bundle();  
  15.         bundle.putString("result", resultString);  
  16.         bundle.putParcelable("bitmap", barcode);  
  17.         resultIntent.putExtras(bundle);  
  18.         this.setResult(RESULT_OK, resultIntent);  
  19.     }  
  20.     MipcaActivityCapture.this.finish();  
  21. }  
  • 我對MipcaActivityCapture介面的佈局做了自己的改動,先看下效果圖,主要是用到FrameLayout,裡面巢狀RelativeLayout,裡面的圖片也是從微信裡面拿出來的,平常我看到需要什麼圖片就去微信裡面找,沒有美工的公司的程式設計師就是苦逼


佈局程式碼如下

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent">
  5.     <RelativeLayout
  6.         android:layout_width="fill_parent"
  7.         android:layout_height="fill_parent">
  8.         <SurfaceView
  9.             android:id="@+id/preview_view"
  10.             android:layout_width="fill_parent"
  11.             android:layout_height="fill_parent"
  12.             android:layout_gravity="center"/>
  13.         <com.mining.app.zxing.view.ViewfinderView
  14.             android:id="@+id/viewfinder_view"
  15.             android:layout_width="wrap_content"
  16.             android:layout_height="wrap_content"/>
  17.         <include
  18.             android:id="@+id/include1"
  19.             android:layout_width="fill_parent"
  20.             android:layout_height="wrap_content"
  21.             android:layout_alignParentTop="true"
  22.             layout="@layout/activity_title"/>
  23.     </RelativeLayout>
  24. </FrameLayout>
在裡面我將介面上面部分寫在另一個佈局裡面,然後include進來,因為這個activity_title在我專案裡面還供其他的Activity使用,我也是直接拷貝出來的
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="wrap_content"
  5.     android:background="@drawable/mmtitle_bg_alpha">
  6.     <Button
  7.         android:id="@+id/button_back"
  8.         android:layout_width="75.0dip"
  9.         android:text="返回"
  10.         android:background="@drawable/mm_title_back_btn"
  11.         android:textColor="@android:color/white"
  12.         android:layout_height="wrap_content"
  13.         android:layout_centerVertical="true"
  14.         android:layout_marginLeft="2dip"/>
  15.     <TextView
  16.         android:id="@+id/textview_title"
  17.         android:layout_width="wrap_content"
  18.         android:layout_height="wrap_content"
  19.         android:layout_alignBaseline="@+id/button_back"
  20.         android:layout_alignBottom="@+id/button_back"
  21.         android:layout_centerHorizontal="true"
  22.         android:gravity="center_vertical"
  23.         android:text="二維碼掃描"
  24.         android:textColor="@android:color/white"
  25.         android:textSize="18sp"/>
  26. </RelativeLayout>
  • 在我這個demo裡面,有一個主介面MainActivity,裡面一個Button, 一個ImageView和一個TextView,點選Button進入到二維碼掃描介面,當掃描OK的時候,回到主介面,將掃描的結果顯示到TextView,將圖片顯示到ImageView裡面,然後你可以不處理圖片,我這裡隨帶的加上圖片,主介面的佈局很簡單如下
Android 廣告banner圖片輪播圖片瀏覽仿大圖檢視控制元件支援視訊和gif圖片支援動態新增資料

    之前專案需要做個仿微信檢視大圖,需要新增圓形下載進度,支援視訊和圖片切換等一系列功能控制元件,自己抽空把開發的自定義控制元件的成果重新構造、整理處理封裝成庫(aar),提供出來,有需要朋友,歡迎使用,如果有什麼建議歡迎留言或者GitHub上提issues

Android 多張圖片展示仿圖片上傳可以選擇多張圖片

我們經常會遇到需要多張圖片展示上傳的需求 ,如圖 這樣的需求我已經遇到過多次,個人總結一下,希望大家多多指點,支援選擇多張圖片 佈局:一個GridView <com.zuihou.drunkenmonkey.widget.view.Di

Android仿呼叫第三方地圖應用導航高德百度騰訊

好久沒有寫Andorid程式碼啦!最近剛好要實現一個這個功能,順便就在部落格裡分享一下。 實現目標 先來一張微信功能截圖看看要做什麼 其實就是有一個目的地,點選目的地的時候彈出可選擇的應用進行導航。 大腦動一下,要實現這個功能應該大體分成兩步:

基於VCamera仿錄製短視訊

基於VCamera,Android仿微信錄製短視訊,如果喜歡請star,如果覺得有紕漏請提交issue,如果你有更好的點子可以提交pull request。 recoder4.gif 使用 1) 在build.gradle,新增wechatRecoderVideoLibr

Android Zxing實現掃描條形碼功能仿整合閃光燈生成

最近在做android專案需要用到二維碼條形碼掃描功能,我用的是Eclipse網上原始碼大多是GitHup上的Android studio版本的所以我改了一版整合到專案中去。 效果圖: 左邊版本的掃碼框是自定義的。右邊版本的掃碼框和掃描線是圖片因為太醜所以最終換成左邊

Android實現掃描仿輕量Zxing

前言 要做一個功能,二維碼識別。網上找一堆相關的Demo,但是總不是想要的效果,或者都是多年前的版本,權衡考慮之後,決定親自操刀。不糾結直接選中Zxing框架,https://github.com/zxing/zxing 在網站上直接clone下來,執行,然後就發現問題了.