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

原文地址:https://www.cnblogs.com/guanxinjing/p/11950397.html