从Java程序生成QR码图像

如果您精通技术和小工具,则必须了解QR码。 这些天,到处都可以找到它-在博客,网站,甚至在某些公共场所。 这在移动应用程序中非常流行,在移动应用程序中,您可以使用QR Code扫描仪应用程序扫描QR Code,它将显示文本或将您重定向到网页(如果是URL)。 我最近遇到了这个问题,发现它非常有趣。 如果您想了解QR Code,可以在Wikipedia QR Code页面上找到很多有用的信息。当我在许多网站中找到此类图像时,我开始寻找如何使用Java Code生成图像的信息。 我研究了一些在市场上可以作为开源使用的API,发现zxing是最简单和最好的用法。

这是您可以用来通过zxing API创建QR Code图片的程序。

package com.adly.generator;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class GenerateQRCode {

 /**
  * @param args
  * @throws WriterException
  * @throws IOException
  */
 public static void main(String[] args) throws WriterException, IOException {
  String qrCodeText = 'http://www.journaldev.com';
  String filePath = 'D:\\Pankaj\\JD.png';
  int size = 125;
  String fileType = 'png';
  File qrFile = new File(filePath);
  createQRImage(qrFile, qrCodeText, size, fileType);
  System.out.println('DONE');
 }

 private static void createQRImage(File qrFile, String qrCodeText, int size,
   String fileType) throws WriterException, IOException {
  // Create the ByteMatrix for the QR-Code that encodes the given String
  Hashtable hintMap = new Hashtable();
  hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
  QRCodeWriter qrCodeWriter = new QRCodeWriter();
  BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText,
    BarcodeFormat.QR_CODE, size, size, hintMap);
  // Make the BufferedImage that are to hold the QRCode
  int matrixWidth = byteMatrix.getWidth();
  BufferedImage image = new BufferedImage(matrixWidth, matrixWidth,
    BufferedImage.TYPE_INT_RGB);
  image.createGraphics();

  Graphics2D graphics = (Graphics2D) image.getGraphics();
  graphics.setColor(Color.WHITE);
  graphics.fillRect(0, 0, matrixWidth, matrixWidth);
  // Paint and save the image using the ByteMatrix
  graphics.setColor(Color.BLACK);

  for (int i = 0; i < matrixWidth; i++) {
   for (int j = 0; j < matrixWidth; j++) {
    if (byteMatrix.get(i, j)) {
     graphics.fillRect(i, j, 1, 1);
    }
   }
  }
  ImageIO.write(image, fileType, qrFile);
 }

}

这是此程序创建的QR Code图像文件。 您可以使用移动QR码扫描仪应用程序对其进行测试。 它应该指向JournalDev主页URL。

如果您没有移动应用程序进行测试,请不要担心。 您也可以通过命令行使用zxing API对其进行测试。

我在Windows操作系统上,这是对其进行测试的命令。 如果您使用的是Unix / Linux / Mac OS,请相应地进行更改。

D:\Pankaj\zxing>java -cp javase\javase.jar;core\core.jar com.google.zxing.client.j2se.CommandLineRunner D:\Pankaj\JD.png
file:/D:/Pankaj/JD.png (format: QR_CODE, type: URI):
Raw result:

http://www.journaldev.com

Parsed result:

http://www.journaldev.com

Found 4 result points.
  Point 0: (35.5,89.5)
  Point 1: (35.5,35.5)
  Point 2: (89.5,35.5)
  Point 3: (80.5,80.5)


动态QR码生成提示

如果要动态生成QR码,则可以使用Google Charts Tools进行。
对于上述情况,URL为https://chart.googleapis.com/chartchs=125×125&cht=qr&chl=http://www.journaldev.com

祝您编程愉快,别忘了分享!

参考:Developer Recipes博客上,从我们的JCG合作伙伴 Pankaj Kumar的Java程序生成QR Code图像


翻译自: https://www.javacodegeeks.com/2012/10/generate-qr-code-image-from-java-program.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值