zoukankan      html  css  js  c++  java
  • Android生成二维码、去除白边

    package cn.areful.qrcode;
    
    import android.graphics.Bitmap;
    import android.graphics.Color;
    
    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.BinaryBitmap;
    import com.google.zxing.EncodeHintType;
    import com.google.zxing.LuminanceSource;
    import com.google.zxing.MultiFormatReader;
    import com.google.zxing.RGBLuminanceSource;
    import com.google.zxing.Reader;
    import com.google.zxing.Result;
    import com.google.zxing.common.BitMatrix;
    import com.google.zxing.common.HybridBinarizer;
    import com.google.zxing.qrcode.QRCodeWriter;
    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
    
    import java.util.Hashtable;
    
    /**
     * Author: areful
     * Date: 2019/3/4
     */
    public class QrCodeGenerator {
        public static Bitmap generateBitmap(String content, int width, int height, boolean needDeleteWhiteBorder) {
            try {
                Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
                hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
                hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
                hints.put(EncodeHintType.MARGIN, 1);
    
                BitMatrix matrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
                if (needDeleteWhiteBorder) {
                    matrix = deleteWhite(matrix);//删除白边
                }
    
                width = matrix.getWidth();
                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] = Color.BLACK;
                        } else {
                            pixels[y * width + x] = Color.WHITE;
                        }
                    }
                }
    
                Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
                return bitmap;
            } catch (Exception e) {
                return null;
            }
        }
    
        private static BitMatrix deleteWhite(BitMatrix matrix) {
            int[] rec = matrix.getEnclosingRectangle();
            int resWidth = rec[2] + 1;
            int resHeight = rec[3] + 1;
    
            BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
            resMatrix.clear();
            for (int i = 0; i < resWidth; i++) {
                for (int j = 0; j < resHeight; j++) {
                    if (matrix.get(i + rec[0], j + rec[1]))
                        resMatrix.set(i, j);
                }
            }
            return resMatrix;
        }
    
        public static String decodeQrCode(Bitmap bm) {
            String contents = null;
    
            int[] intArray = new int[bm.getWidth() * bm.getHeight()];
            bm.getPixels(intArray, 0, bm.getWidth(), 0, 0, bm.getWidth(), bm.getHeight());
    
            LuminanceSource source = new RGBLuminanceSource(bm.getWidth(), bm.getHeight(), intArray);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    
            Reader reader = new MultiFormatReader();// use this otherwise ChecksumException
            try {
                Result result = reader.decode(bitmap);
                contents = result.getText();
                //byte[] rawBytes = result.getRawBytes();
                //BarcodeFormat format = result.getBarcodeFormat();
                //ResultPoint[] points = result.getResultPoints();
            } catch (Exception ignored) {
            }
            return contents;
        }
    }
    

      使用:

    Bitmap bm = QrCodeGenerator.generateBitmap("Hello, areful!", 256, 256, true);
    if (bm != null) {
        mImageView.setImageBitmap(bm);
    }
    

      

     

  • 相关阅读:
    DIY树莓派之随身工具箱
    经验分享 | Burpsuite抓取非HTTP流量
    版本控制工具:SVN和Maven的区别
    Dicom Conformance
    从Script到Code Blocks、Code Behind到MVC、MVP、MVVM
    MVC,MVP 和 MVVM 的图示
    DB2 触发器的写法及表主键结构的修改
    数据表增加列的时候赋默认值
    Mysql数据库乱码总结
    又在字符集上浪费时间
  • 原文地址:https://www.cnblogs.com/areful/p/10494231.html
Copyright © 2011-2022 走看看