使用zxing生成二维码去除空白区域方法

   使用zxing生成二维码去除空白区域方法使用zxing生成二维码去除空白区域方法使用zxing生成二维码去除空白区域方法

  通常我们生成二维码需要做以下配置

 
  1. Map<EncodeHintType, Object> hints = new HashMap<>();

  2. hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//设置容错率默认为最高

  3. hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");// 字符编码为UTF-8

  4. hints.put(EncodeHintType.MARGIN, 0);//二维码空白区域,最小为0也有白边,只是很小,最小是6像素左右

生成的二维码的空白区域怎么设置都是有

实测试100、1000大小的空白像素如   top:7 right:8 bottom:8 left:7  右边、底部会多一个像素

二维码生成返回的对像 BitMatrix可以直接遍历,可以把它当作一个二维数组,通过BitMatrix.get(x,y)判断是否是一个黑点,然后复制到一个新的二 维码对像即可,实例代码如下

 
  1. import com.google.zxing.BarcodeFormat;

  2. import com.google.zxing.EncodeHintType;

  3. import com.google.zxing.MultiFormatWriter;

  4. import com.google.zxing.client.j2se.MatrixToImageWriter;

  5. import com.google.zxing.common.BitMatrix;

  6. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

  7. import org.slf4j.Logger;

  8. import org.slf4j.LoggerFactory;

  9.  
  10. import java.io.FileOutputStream;

  11. import java.io.OutputStream;

  12. import java.util.HashMap;

  13. import java.util.Map;

  14.  
  15. /**

  16. * Created by zengrenyuan

  17. */

  18. public class QRCodeImageTest {

  19.  
  20. private static final Logger LOG = LoggerFactory.getLogger(QRCodeImageTest.class);

  21.  
  22. public static void main(String[] args) {

  23. String format = "png";// 图像类型

  24. String target = "/data/qrcode.png";

  25. Map<EncodeHintType, Object> hints = new HashMap<>();

  26. hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//设置容错率默认为最高

  27. hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");// 字符编码为UTF-8

  28. hints.put(EncodeHintType.MARGIN, 0);//二维码空白区域,最小为0也有白边,只是很小,最小是6%左右

  29. try (OutputStream os = new FileOutputStream(target)) {

  30. //二维码的宽

  31. int qrCodeWidth = 1000;

  32. BitMatrix bitMatrix = new MultiFormatWriter()

  33. .encode("https://www.baidu.com", BarcodeFormat.QR_CODE, qrCodeWidth, qrCodeWidth, hints);

  34. int height = bitMatrix.getHeight();

  35. int width = bitMatrix.getWidth();

  36.  
  37. try (OutputStream os1 = new FileOutputStream("/data/qrcode.png")) {

  38. MatrixToImageWriter.writeToStream(bitMatrix, format, os1);

  39. }

  40.  
  41.  
  42. boolean isFirstBlankPoint = false;//是否第一个黑点

  43. int left = 0;//开始的宽

  44. int top = 0;//开始的高

  45. for (int y = 0; y < height; y++) {//从左上角开始读

  46. if (isFirstBlankPoint) {

  47. break;

  48. }

  49. for (int x = 0; x < width; x++) {

  50. if (bitMatrix.get(x, y) && !isFirstBlankPoint) {//如果是一个黑点就会返回true

  51. isFirstBlankPoint = true;

  52. left = x;

  53. top = y;

  54. LOG.info("f x:{} y:{}", x, y);

  55. }

  56. }

  57. }

  58.  
  59. int right = 0;//右边空白区域

  60. int bottom = 0;//底部空白区域

  61.  
  62. boolean isRightLastPoint = false;

  63. for (int x = width - 1; x >= 0; x--) {//从右上角开始读

  64. if (isRightLastPoint) {

  65. break;

  66. }

  67. for (int y = 0; y < height; y++) {

  68. if (bitMatrix.get(x, y) && !isRightLastPoint) {//如果是一个黑点就会返回true

  69. isRightLastPoint = true;

  70. right = width - x;

  71. LOG.info("e x:{} y:{} right:{}", x, y, right);

  72. break;

  73. }

  74. }

  75. }

  76. boolean isBottomLastPoint = false;

  77. for (int y = height - 1; y >= 0; y--) {//从左理角开始读

  78. if (isBottomLastPoint) {

  79. break;

  80. }

  81. for (int x = 0; x < width; x++) {

  82. if (bitMatrix.get(x, y) && !isBottomLastPoint) {

  83. isBottomLastPoint = true;

  84. bottom = height - y;

  85. LOG.info("e x:{} y:{} bottom:{}", x, y, bottom);

  86. break;

  87. }

  88. }

  89. }

  90.  
  91. LOG.info("top:{} right:{} bottom:{} left:{} ", top, right, bottom, left);

  92. //最小的空白区域一个像素

  93. int miniPadding = 1;

  94. //原二维码大小减去上下左右的空白区域

  95. int newWidth = qrCodeWidth - left - right + miniPadding * 2;

  96. int newHeight = qrCodeWidth - top - bottom + miniPadding * 2;

  97. BitMatrix newBitMatrix = new BitMatrix(newWidth, newHeight);

  98. //写二维码点的宽度

  99. int pointSize = 1;

  100. for (int y = miniPadding; y < newHeight; y++) {

  101. for (int x = miniPadding; x < newWidth; x++) {

  102. if (bitMatrix.get(x + left, y + top)) {

  103. newBitMatrix.setRegion(x, y, pointSize, pointSize);

  104. }

  105. }

  106. }

  107. MatrixToImageWriter.writeToStream(newBitMatrix, format, os);

  108. } catch (Exception e) {

  109. LOG.error(null, e);

  110. }

  111.  
  112. }

  113.  
  114.  
  115.