zoukankan      html  css  js  c++  java
  • zxing生成二维码设置边框颜色

    真是研究了很久很久,满满的泪啊


    zxing生成二维码,默认是可以增加空白边框的,但是并没有可以设置边框颜色的属性。

    其中增加空白边框的属性的一句话是:

    Map hints = new HashMap();
    hints.put(EncodeHintType.MARGIN, 1);


    使用这句话设置边框,留出来的边框的颜色,就是二维码中主颜色的以外的那个颜色。通常主颜色都是黑色,背景色都是白色。如下二维码的边框的颜色,就是除了绿色以外的那个颜色。

     


    所以并没有设置边框颜色的属性,那该怎么办?
    比如:要求使用POI把二维码放到Excel中,但是不能占了Excel的边框,而是要内嵌进去。
    刚开始做出来的效果是:

    最后需要修改成的效果是:


    最后找到的解决方案是,重写它的方法:MatrixToImageWriter.toBufferedImage(bitMatrix);

    页面的调用点为:

    1 MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    2 Map hints = new HashMap();
    3 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    4 hints.put(EncodeHintType.MARGIN, "0");
    5 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    6 BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 100, 100, hints);
    7 BufferedImage qrPic = null;
    8 qrPic = MatrixToImageWriter.toBufferedImage(bitMatrix);

    初始方法为:

     1 public static BufferedImage toBufferedImage(BitMatrix matrix) {
     2   int width = matrix.getWidth();
     3   int height = matrix.getHeight();
     4   BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
     5   for (int x = 0; x < width; x++) {
     6     for (int y = 0; y < height; y++) {
     7       image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
     8     }
     9   }
    10   return image;
    11 }

    重写后为: 

     1 public static BufferedImage toBufferedImageCustom(BitMatrix matrix) {
     2 //二维码边框的宽度,默认二维码的宽度是100,经过多次尝试后自定义的宽度
     3   int left = 3;
     4   int right = 4;
     5   int top = 2;
     6   int bottom = 2;
     7 
     8 //1、首先要自定义生成边框
     9   int[] rec = matrix.getEnclosingRectangle();   //获取二维码图案的属性
    10   int resWidth = rec[2] + left + right;
    11   int resHeight = rec[3] + top + bottom;
    12 
    13   BitMatrix matrix2 = new BitMatrix(resWidth, resHeight); // 按照自定义边框生成新的BitMatrix
    14   matrix2.clear();
    15   for(int i=left+1; i < resWidth-right; i++){   //循环,将二维码图案绘制到新的bitMatrix中
    16     for(int j=top+1; j < resHeight-bottom; j++){
    17       if(matrix.get(i-left + rec[0], j - top + rec[1])){
    18         matrix2.set(i,j);
    19       }
    20     }
    21   }
    22 
    23   int width = matrix2.getWidth();
    24   int height = matrix2.getHeight();
    25 
    26 //2、为边框设置颜色
    27   BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    28   for (int x = 0; x < width; x++) {
    29     for (int y = 0; y < height; y++) {
    30       if(x<left || x> width-right|| y< top || y>height-bottom){
    31         image.setRGB(x, y,BLACK);    //为了与Excel边框重合,设置二维码边框的颜色为黑色
    32       }else{
    33         image.setRGB(x, y, matrix2.get(x, y) ? BLACK : WHITE);
    34       }
    35     }
    36   }
    37   return image;
    38 }

     原创文章,欢迎转载,转载请注明出处!

  • 相关阅读:
    ELK学习总结(2-1)mavel -》sense 和 索引初始化
    ELK学习总结(1-3)倒排索引
    java基础总结(1)安装jdk
    ELK学习总结(1-2)安装ElasticSearch
    T410升级笔记
    CURL学习总结(1)
    restful架构风格设计准则(二)以资源为中心,一个url
    GIT入门笔记(18)- 标签创建和管理
    GIT入门笔记(17)- 创建分支dev_lsq, 提交到代码
    GIT入门笔记(16)- 分支创建和管理
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/zxing.html
Copyright © 2011-2022 走看看