织梦CMS - 轻松建站从此开始!

欧博ABG官网-欧博官方网址-会员登入

欧博allbetHow to generate QR code with some text usi

时间:2025-11-23 13:10来源: 作者:admin 点击: 2 次
To generate a QR code in Java, we need to use a third-party library named ZXing (Zebra Crossing). It is a popular API that allows us to process with Q

To generate a QR code in Java, we need to use a third-party library named ZXing (Zebra Crossing). It is a popular API that allows us to process with QR code. With the help of the library, we can easily generate and read the QR code. Before moving towards the Java program, we need to add the ZXing library to the project. We can download it from the official site.

zxing core-3.3.0.jar

zxing javase-3.3.0.jar

After downloading, add it to the classpath. Or add the following dependency in pom.xml file.

<dependencies> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.0</version> </dependency> </dependencies>

Let's create a Java program that generates a QR code.

GenerateQrCode.java

import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.NotFoundException; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class GenerateQRCode { //static function that creates QR Code public static void generateQRcode(String data, String path, String charset, Map map, int h, int w) throws WriterException, IOException { //the BitMatrix class represents the 2D matrix of bits //MultiFormatWriter is a factory class that finds the appropriate Writer subclass for the BarcodeFormat requested and encodes the barcode with the supplied contents. BitMatrix matrix = new MultiFormatWriter().encode(new String(data.getBytes(charset), charset), BarcodeFormat.QR_CODE, w, h); MatrixToImageWriter.writeToFile(matrix, path.substring(path.lastIndexOf('.') + 1), new File(path)); } //main() method public static void main(String args[]) throws WriterException, IOException, NotFoundException { //data that we want to store in the QR code String str= "THE HABIT OF PERSISTENCE IS THE HABIT OF VICTORY."; //path where we want to get QR Code String path = "C:\\Users\\Anubhav\\Desktop\\QRDemo\\Quote.png"; //Encoding charset to be used String charset = "UTF-8"; Map<EncodeHintType, ErrorCorrectionLevel> hashMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>(); //generates QR code with Low level(L) error correction capability hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); //invoking the user-defined method that creates the QR code generateQRcode(str, path, charset, hashMap, 200, 200);//increase or decrease height and width accodingly //prints if the QR code is generated System.out.println("QR Code created successfully."); } }

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-11-23 21:11 最后登录:2025-11-23 21:11
栏目列表
推荐内容