Android開發 QRCode二維碼開發第三方框架


前言

  Android開發里二維碼開發經常用到,這里簡單的介紹下Android開發里的二維碼.

最廣泛使用的二維碼庫zxing

  zxing是最廣泛的二維碼庫各個平台都可以適用它,但是Android平台使用它好像需要進行JNI處理.但是,github上大神已經幫我們做好了,下面我會介紹一個好用的二維碼框架.這里提zxing是讓你知道很多二維碼框架的封裝源頭都是它.

github地址: https://github.com/zxing/zxing

implementation 'com.google.zxing:core:3.4.0'

barcodescanner 第三方二維碼框架

github地址:https://github.com/dm77/barcodescanner

這個框架已經將zxing打包封裝好了,你直接使用即可,甚至還能使用依賴它后獨立使用zxing的功能.(比如生成二維碼圖片)

ps: 這個框架也支持條形碼

依賴

repositories {
   jcenter()
}
implementation 'me.dm7.barcodescanner:zxing:1.9.13'

掃碼識別內容

這里偷懶引用作者的代碼...

activity里

public class SimpleScannerActivity extends Activity implements ZXingScannerView.ResultHandler {
        private ZXingScannerView mScannerView;

        @Override
        public void onCreate(Bundle state) {
            super.onCreate(state);
            mScannerView = new ZXingScannerView(this);   // 初始化掃描儀視圖
            setContentView(mScannerView);               
        }

        @Override
        public void onResume() {
            super.onResume();
            mScannerView.setResultHandler(this); // 將自己注冊為掃描結果的處理程序。
            mScannerView.startCamera();          // 在啟動相機
        }

        @Override
        public void onPause() {
            super.onPause();
            mScannerView.stopCamera();           // 暫停相機
        }

        @Override
        public void handleResult(Result rawResult) {
            // 掃描結果
            Log.v(TAG, rawResult.getText()); // Prints scan results
            Log.v(TAG, rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)

            // If you would like to resume scanning, call this method below:
            mScannerView.resumeCameraPreview(this);
        }

其他API

        // 設置閃光燈
        void setFlash(boolean);

        // 切換自動對焦
        void setAutoFocus(boolean);

        // 指定感興趣的條形碼格式
        void setFormats(List<BarcodeFormat> formats);

        // 指定cameraId
        void startCamera(int cameraId);

QRCodeReaderView 第三方二維碼框架

缺點,無法掃描條形碼

優點,可以自定義頁面布局

github地址:https://github.com/dlazaro66/QRCodeReaderView

基於zxing的庫生成二維碼圖片

利用第三方庫里的zxing庫生成二維碼,請注意!圖片生成是耗時工作需要在子線程中運行

/** 
 * 二維碼工具類
 * @version 創建時間:2014年12月5日 下午5:15:47 
 */

public class QrCodeUtils {
    
    /**
     * 傳入字符串生成二維碼
     * @param str
     * @return
     * @throws WriterException
     */
    public static Bitmap Create2DCode(String str) throws WriterException {
        // 生成二維矩陣,編碼時指定大小,不要生成了圖片以后再進行縮放,這樣會模糊導致識別失敗
        BitMatrix matrix = new MultiFormatWriter().encode(str,
                BarcodeFormat.QR_CODE, 300, 300);
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        // 二維矩陣轉為一維像素數組,也就是一直橫着排了
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * width + x] = 0xff000000;
                }else{
                    pixels[y * width + x] = 0xffffffff;
                }

            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        // 通過像素數組生成bitmap,具體參考api
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }
}

基於zxing庫解析二維碼圖片文件

請注意!二維碼圖片解析是耗時工作需要在子線程中運行

/**
     * zxing解碼二維碼圖片
     *
     * @param bitmap
     * @return
     * @throws NotFoundException
     * @throws IOException
     */
    private String zxingScanQrcodePicture(Bitmap bitmap) throws NotFoundException, IOException {
        Map<DecodeHintType, Object> hints = new EnumMap<DecodeHintType, Object>(DecodeHintType.class);
        Collection<BarcodeFormat> decodeFormats = EnumSet.noneOf(BarcodeFormat.class);
        decodeFormats.addAll(EnumSet.of(BarcodeFormat.QR_CODE));
        hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");

        int lWidth = bitmap.getWidth();
        int lHeight = bitmap.getHeight();
        int[] lPixels = new int[lWidth * lHeight];
        bitmap.getPixels(lPixels, 0, lWidth, 0, 0, lWidth, lHeight);
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new RGBLuminanceSource(lWidth, lHeight, lPixels)));

        Result lResult = new MultiFormatReader().decode(binaryBitmap, hints);
        return lResult.getText();
    }

 

 

 

 

End


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM