从Java程序生成QR码图像
這是您可以用來(lái)通過(guò)zxing API創(chuàng)建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 StringHashtable 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 QRCodeint 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 ByteMatrixgraphics.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);}}這是此程序創(chuàng)建的QR Code圖像文件。 您可以使用移動(dòng)QR碼掃描儀應(yīng)用程序?qū)ζ溥M(jìn)行測(cè)試。 它應(yīng)該指向JournalDev主頁(yè)URL。
如果您沒(méi)有移動(dòng)應(yīng)用程序進(jìn)行測(cè)試,請(qǐng)不要擔(dān)心。 您也可以通過(guò)命令行使用zxing API對(duì)其進(jìn)行測(cè)試。
我在Windows操作系統(tǒng)上,這是對(duì)其進(jìn)行測(cè)試的命令。 如果您使用的是Unix / Linux / Mac OS,請(qǐng)相應(yīng)地進(jìn)行更改。
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.comParsed result:http://www.journaldev.comFound 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)
動(dòng)態(tài)QR碼生成提示
如果要?jiǎng)討B(tài)生成QR碼,則可以使用Google Charts Tools進(jìn)行。
對(duì)于上述情況,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
總結(jié)
以上是生活随笔為你收集整理的从Java程序生成QR码图像的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Google Guava并发– List
- 下一篇: Java 7:使用NIO.2进行文件过滤