如何使用zxing库文件生成65 * 65 Qr代码?

时间:2015-04-06 10:24:42

标签: java zxing qr-code

我可以使用zxing库文件(版本1.7.0)生成90 * 90 Qr代码。但是当我给图像提供小于90的尺寸时,Qrcode数据填充区域变得太小,请看下面的例子 enter image description here

保持尺寸为90 * 90,

enter image description here

保持尺寸为89 * 89 ..

我想生成65 * 65 Qr代码,请帮助我。

String myCodeText = "Success";
String filePath = "D:/CrunchifyQR.png";
int size = 90;  
String fileType = "png"

Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

QRCodeWriter qrCodeWriter = new QRCodeWriter();
ByteMatrix byteMatrix = qrCodeWriter.encode(myCodeText,BarcodeFormat.QR_CODE, size, size, hintMap);
int CrunchifyWidth = byteMatrix.getWidth() ;
BufferedImage image = new BufferedImage(CrunchifyWidth, CrunchifyWidth, BufferedImage.TYPE_INT_RGB);
image.createGraphics();

Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.BLACK);
graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth);
graphics.setColor(Color.GRAY);

byte[][] byteArr = byteMatrix.getArray();

for (int i = 0; i < CrunchifyWidth; i++) {
    for (int j = 0; j < CrunchifyWidth; j++) {
        int grayValue = byteArr[i][j] & 0xff; 
        if (grayValue != 0) {
            graphics.fillRect(i, j, 1, 1);
        }
    }
}

ImageIO.write(image, fileType, myFile);

1 个答案:

答案 0 :(得分:0)

尝试将MARGIN属性设置为1(默认情况下等于4),如下所示:

Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

hintMap.put(EncodeHintType.MARGIN, 1);

我希望它有所帮助!