zoukankan      html  css  js  c++  java
  • Android项目实战(五十四):zxing 生成二维码图片去除白色内边距的解决方案

    目录:zxing->encoding->EncodingHandler类 中修改 createQRCode方法

       private static final int BLACK = 0xff000000;
        private static final int WHITE = 0xffffffff;
    
        public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
            String contentsToEncode = str;
            if (contentsToEncode == null) {
                return null;
            }
            Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
            //hints.put(EncodeHintType.CHARACTER_SET, encoding);
            hints.put(EncodeHintType.MARGIN, 0); /* default = 4 */
            MultiFormatWriter writer = new MultiFormatWriter();
            BitMatrix result;
            try {
                result = writer.encode(contentsToEncode, BarcodeFormat.QR_CODE , widthAndHeight, widthAndHeight, hints);
            } catch (Exception e) {
                // Unsupported format
                e.printStackTrace();
                return null;
            }
    
            int width = result.getWidth();
            int height = result.getHeight();
            int[] pixels = new int[width * height];
            for (int y = 0; y < height; y++) {
                int offset = y * width;
                for (int x = 0; x < width; x++) {
                    pixels[offset + x] = result.get(x, y) ? BLACK : Color.WHITE;
                }
            }
    
            Bitmap bitmap = Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;
        }
  • 相关阅读:
    Opencv3.4:显示一张图片
    Windows编译Opencv
    FFmpeg4.0笔记:rtsp2rtmp
    FFmpeg4.0笔记:file2rtmp
    Ubuntu编译安装crtmp-server
    python笔记:#014#综合应用
    python笔记:#012#函数
    Python学习--利用scapy库实现ARP欺骗
    metasploit——(三)渗透攻击之旅
    metasploit——(一)情报收集篇
  • 原文地址:https://www.cnblogs.com/xqxacm/p/10637404.html
Copyright © 2011-2022 走看看