方法1:
网上有相关的一些分析资料,百度贴吧里也有原因,因为创建的时候预先插入了白边。 这里给出不修改源代码的方案。 测试PDF_418和QR_CODE有效 其他的同理,需要研究源代码
- public 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;
- }
方法2:
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 1);
try {
matrix = new MultiFormatWriter().encode(contents,
BarcodeFormat.QR_CODE, 300, 300, hints);
} catch (WriterException e) {
e.printStackTrace();
}
测试可以~
本文介绍了解决二维码生成时出现白边问题的两种方法。一种是通过Java代码实现的矩阵裁剪方法,另一种是在生成二维码时设置边距的方法。这两种方法均可有效地去除多余的白边。

3898

被折叠的 条评论
为什么被折叠?



