使用ZXing(“斑马线”)API在Java中生成QR码

3
下面的代码成功生成了二维码。
由于我们给出的qrCodeData输入不同,所以二维码的高度和宽度也会有所变化。
例如,如果qrCodeData=Hello World!,则生成的二维码的高度和宽度较小;如果qrCodeData=A mobile phone is a portable telephone that can make and receive calls over a radio frequency link while the user is moving within a telephone service area. The radio frequency link establishes a connection to the switching systems of a mobile phone operator, which provides access to the public switched telephone network (PSTN),则生成的二维码的高度和宽度较大。
附上示例二维码。 for less data for large data 我想要生成相同高度和宽度的二维码,无论qrCodeData输入的数据是什么。请给予建议,谢谢。
package com.javapapers.java;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRCode {

public static void main(String[] args) throws WriterException, IOException,
        NotFoundException {
String qrCodeData = "Hello World!";
String filePath = "QRCode.png";
String charset = "UTF-8"; // or "ISO-8859-1"
Map<EncodeHintType, ErrorCorrectionLevel> hintMap = new                                                                      HashMap<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

    createQRCode(qrCodeData, filePath, charset, hintMap, 200, 200);
    System.out.println("QR Code image created successfully!");

    System.out.println("Data read from QR Code: "
            + readQRCode(filePath, charset, hintMap));

}

public static void createQRCode(String qrCodeData, String filePath,
        String charset, Map hintMap, int qrCodeheight, int qrCodewidth)
        throws WriterException, IOException {
    BitMatrix matrix = new MultiFormatWriter().encode(
            new String(qrCodeData.getBytes(charset), charset),
            BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintMap);
    MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath
            .lastIndexOf('.') + 1), new File(filePath));
}

public static String readQRCode(String filePath, String charset, Map hintMap)
        throws FileNotFoundException, IOException, NotFoundException {
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
            new BufferedImageLuminanceSource(
                    ImageIO.read(new FileInputStream(filePath)))));
    Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap,
            hintMap);
    return qrCodeResult.getText();
}
}

如果空间不足,ZXing将无法加密。您是否尝试为ImageView(其中显示QR Code)提供DP的固定大小(即不使用warp_content)?生成的位图将更大,但它将被缩小以适应您的容器。但是,请确保读取器仍然能够读取缩小的QR Code;-) - mithrop
谢谢你的回复,mithrop。 - Parthiban
1个回答

2

你的图片(准确地说是图片的像素部分)大小不同,因为周围有白色边缘。默认情况下,这个所谓的QR码安静区域的宽度为4。因此,要去掉边框,你可以将其设置为0。
修改你的main方法如下:

Map<EncodeHintType, Object> hintMap = new HashMap<EncodeHintType, Object>();
hintMap.put(EncodeHintType.MARGIN, 0);
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

createQRCode(qrCodeData, filePath, charset, hintMap, 200, 200);

然后,生成的图片没有边距,像素部分大小相同。 enter image description here enter image description here

谢谢Thomas。现在我可以获得具有相同高度和宽度的QR码,与数据无关。 - Parthiban

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接