如何在Java中使用com.google.zxing库在QR码下方添加文本。

huangapple go评论65阅读模式
英文:

how to add text below QR code using com.google.zxing libraby in JAVA

问题

我使用com.google.zxing库生成了QR码。
QR码生成工作正常,但我想要在QR码下方显示QR码的数据。

我希望生成如下所示的QR码。

如何在Java中使用com.google.zxing库在QR码下方添加文本。

这是我的代码。

QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(data, BarcodeFormat.QR_CODE, width, height);
ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
byte[] pngData = pngOutputStream.toByteArray();
英文:

I have generated QR code using com.google.zxing this library.
QRCode generations work fine but I want to display Data of QRcode below QRcode.

I want to generate QR code like attached below.

如何在Java中使用com.google.zxing库在QR码下方添加文本。

Here is my code.

QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(data, BarcodeFormat.QR_CODE, width, height);
        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
        byte[] pngData = pngOutputStream.toByteArray();

答案1

得分: 4

以下是生成带有文本或不带文本的完整QR码的代码。

public byte[] generateQRCode(String data, Integer width, Integer height, String[] text) {

    try {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(data, BarcodeFormat.QR_CODE, width, height);

        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
        byte[] pngData = pngOutputStream.toByteArray();

        // 如果需要显示文本
        if (text.length > 0) {
            int totalTextLineToadd = text.length;
            InputStream in = new ByteArrayInputStream(pngData);
            BufferedImage image = ImageIO.read(in);

            BufferedImage outputImage = new BufferedImage(image.getWidth(), image.getHeight() + 25 * totalTextLineToadd, BufferedImage.TYPE_INT_ARGB);
            Graphics g = outputImage.getGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, outputImage.getWidth(), outputImage.getHeight());
            g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
            g.setFont(new Font("Arial Black", Font.BOLD, 12));
            Color textColor = Color.BLACK;
            g.setColor(textColor);
            FontMetrics fm = g.getFontMetrics();
            int startingYposition = height + 5;
            for(String displayText : text) {
                g.drawString(displayText, (outputImage.getWidth() / 2) - (fm.stringWidth(displayText) / 2), startingYposition);
                startingYposition += 20;
            }
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(outputImage, "PNG", baos);
            baos.flush();
            pngData = baos.toByteArray();
            baos.close();
        }

        return pngData;
    } catch (WriterException | IOException ex) {
        throw new ImtechoUserException(ex.getMessage(), 0);
    }
}

这将返回带有文本或不带文本的新生成QR码的字节数组(Byte[])。

英文:

Here I have Provided complete code for generating QRcode with Text or not.

public byte[] generateQRCode(String data, Integer width, Integer height, String[] text) {
try {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(data, BarcodeFormat.QR_CODE, width, height);
ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
byte[] pngData = pngOutputStream.toByteArray();
//        If text is needed to display
if (text.length > 0) {
int totalTextLineToadd = text.length;
InputStream in = new ByteArrayInputStream(pngData);
BufferedImage image = ImageIO.read(in);
BufferedImage outputImage = new BufferedImage(image.getWidth(), image.getHeight() + 25 * totalTextLineToadd, BufferedImage.TYPE_INT_ARGB);
Graphics g = outputImage.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, outputImage.getWidth(), outputImage.getHeight());
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
g.setFont(new Font("Arial Black", Font.BOLD, 12));
Color textColor = Color.BLACK;
g.setColor(textColor);
FontMetrics fm = g.getFontMetrics();
int startingYposition = height + 5;
for(String displayText : text) {
g.drawString(displayText, (outputImage.getWidth() / 2)   - (fm.stringWidth(displayText) / 2), startingYposition);
startingYposition += 20;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(outputImage, "PNG", baos);
baos.flush();
pngData = baos.toByteArray();
baos.close();
}
return pngData;
} catch (WriterException | IOException ex) {
throw new ImtechoUserException(ex.getMessage(), 0);
}
}

This will return Byte[] of newly generated QR code with text or without text.

答案2

得分: 0

这里我生成了QR码文件,将其保存在内存中,然后使用图形库进行操作。
使用这个库可以向内存中添加文本,然后再次保存。

public static void main(String[] args) {        
   try {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode("Hello world", BarcodeFormat.QR_CODE, 300, 300);
        Path path = FileSystems.getDefault().getPath("test.png");
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);     
        
        
       final BufferedImage image = ImageIO.read(new File(path.toString()));
       JFrame frame = new JFrame();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       Graphics g = image.getGraphics();
       g.setFont(g.getFont().deriveFont(22f));
       Color textColor = Color.BLACK;
       g.setColor(textColor);
       g.drawString("Hello world", 15, 300);
       g.dispose();

       ImageIO.write(image, "png", new File("test45.png"));
        
        
    } catch (WriterException | IOException ex) {
        
    }  
}
英文:

Here I Generated QR Code file and took in memory and then used Graphics Lib.
Using this Library could add Text to that Memory and saved it again.

public static void main(String[] args) {        
try {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode("Hello world", BarcodeFormat.QR_CODE, 300, 300);
Path path = FileSystems.getDefault().getPath("test.png");
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);     
final BufferedImage image = ImageIO.read(new File(path.toString()));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Graphics g = image.getGraphics();
g.setFont(g.getFont().deriveFont(22f));
Color textColor = Color.BLACK;
g.setColor(textColor);
g.drawString("Hello world", 15, 300);
g.dispose();
ImageIO.write(image, "png", new File("test45.png"));
} catch (WriterException | IOException ex) {
}  
}

答案3

得分: 0

@Ashish Khokhariya,谢谢你,我使用了你的代码。
对不起,我无法“反馈”:
感谢反馈!那些声望低于15的投票会被记录下来,但不会改变公开显示的帖子分数。

英文:

@Ashish Khokhariya thank you i do it withe your code
I am sorry I cant " feedback " :
Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score.

答案4

得分: 0

以下是翻译好的代码部分:

public BufferedImage getQRCodeWithText(BitMatrix qrcodeBitMatrix, String text) throws IOException {
    BufferedImage image = MatrixToImageWriter.toBufferedImage(qrcodeBitMatrix);
    if (text.length() > 0) {
        int totalTextLineToAdd = text.length();
        int newWidth = image.getWidth();
        int newHeight = image.getHeight() + 10 * totalTextLineToAdd;
        BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics graphics = newImage.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, newImage.getWidth(), newImage.getHeight());
        graphics.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
        graphics.setFont(new Font("Arial Black", Font.BOLD, 12));
        Color textColor = Color.BLACK;
        graphics.setColor(textColor);
        FontMetrics fontMetrics = graphics.getFontMetrics();
        int startingYPosition = image.getHeight() + 5;
        graphics.drawString(text, (newImage.getWidth() / 2) - (fontMetrics.stringWidth(text) / 2), startingYPosition);
        image = newImage;
    }
    return image;
}
英文:

The below code worked for me.

public BufferedImage getQRCodeWithText(BitMatrix qrcodeBitMatrix, String text) throws IOException {
BufferedImage image = MatrixToImageWriter.toBufferedImage(qrcodeBitMatrix);
if (text.length() > 0) {
int totalTextLineToAdd = text.length();
int newWidth = image.getWidth();
int newHeight = image.getHeight() + 10 * totalTextLineToAdd;
BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = newImage.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, newImage.getWidth(), newImage.getHeight());
graphics.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
graphics.setFont(new Font("Arial Black", Font.BOLD, 12));
Color textColor = Color.BLACK;
graphics.setColor(textColor);
FontMetrics fontMetrics = graphics.getFontMetrics();
int startingYPosition = image.getHeight() + 5;
graphics.drawString(text, (newImage.getWidth() / 2) - (fontMetrics.stringWidth(text) / 2), startingYPosition);
image = newImage;
}
return image;
}

huangapple
  • 本文由 发表于 2020年9月14日 20:27:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63884335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定