qr码生成_从Java程序生成QR码图像
生活随笔
收集整理的這篇文章主要介紹了
qr码生成_从Java程序生成QR码图像
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 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);}}這是此程序創建的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.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)
動態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
qr碼生成
總結
以上是生活随笔為你收集整理的qr码生成_从Java程序生成QR码图像的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 您真的需要instanceof吗?
- 下一篇: 苹果下架iPhone 14 Pro /