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

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

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

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

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

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

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

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

页面的调用点为:

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

初始方法为:

 public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}

重写后为:

 public static BufferedImage toBufferedImageCustom(BitMatrix matrix) {
//二维码边框的宽度,默认二维码的宽度是100,经过多次尝试后自定义的宽度
int left = 3;
int right = 4;
int top = 2;
int bottom = 2; //1、首先要自定义生成边框
int[] rec = matrix.getEnclosingRectangle(); //获取二维码图案的属性
int resWidth = rec[2] + left + right;
int resHeight = rec[3] + top + bottom; BitMatrix matrix2 = new BitMatrix(resWidth, resHeight); // 按照自定义边框生成新的BitMatrix
matrix2.clear();
for(int i=left+1; i < resWidth-right; i++){ //循环,将二维码图案绘制到新的bitMatrix中
for(int j=top+1; j < resHeight-bottom; j++){
if(matrix.get(i-left + rec[0], j - top + rec[1])){
matrix2.set(i,j);
}
}
} int width = matrix2.getWidth();
int height = matrix2.getHeight(); //2、为边框设置颜色
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if(x<left || x> width-right|| y< top || y>height-bottom){
image.setRGB(x, y,BLACK); //为了与Excel边框重合,设置二维码边框的颜色为黑色
}else{
image.setRGB(x, y, matrix2.get(x, y) ? BLACK : WHITE);
}
}
}
return image;
}

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

最新文章

  1. OJ-Triangle
  2. MongoDB (七) MongoDB 数据类型
  3. 权限控制框架Shiro简单介绍及配置实例
  4. 怎么找到MyEclipse-&gt;add struts capabilities
  5. javascript基础学习(二)
  6. 浅谈angular中的promise
  7. (@WhiteTaken)设计模式学习——享元模式
  8. Android之PhotoView使用
  9. centos7使用kubeadm安装部署kubernetes 1.14
  10. C# 使用默认浏览器打开链接
  11. Linux Spi驱动移植小结
  12. 苹果笔记本安装windows正版操作系统
  13. sublime 3的破解和安装
  14. 深入详解美团点评CAT跨语言服务监控(三)CAT客户端原理
  15. MFC CHotKeyCtrl控件
  16. DotNetty 使用ByteToMessageDecoder 国家部标808协议封装
  17. UFLDL学习笔记 ---- 主成分分析与白化
  18. docker跨主机互联
  19. C# winform 禁止窗体移动
  20. Transaction And Lock--事务中使用return会回滚事务吗?

热门文章

  1. NOIP2002-2017提高组题解
  2. HihoCoder1192 简单的树嵌入 dfs、构造
  3. maven 单独构建多模块项目中的单个模块
  4. anglarjs1.6.3+owin 实现验证之一:统一拒绝非登录访问。
  5. kubectl客户端工具远程连接k8s集群
  6. Luogu P1494 [国家集训队]小Z的袜子
  7. 通用漏洞评估方法CVSS3.0简表
  8. 如何使用串口来给STM32下载程序
  9. JQuery_自带的动画效果
  10. Authorize的Forms认证