当前位置:网站首页>Barcode generation and decoding, QR code generation and decoding
Barcode generation and decoding, QR code generation and decoding
2022-04-23 05:01:00 【Chen junyang】
Barcode generation and decoding 、 Two dimensional code generation and decoding
Not much to say , Go straight to the code :
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class Test {
/** * Bar code coding * @param contents * @param width * @param height * @param imgPath */
public static void encode(String contents, int width, int height, String imgPath) {
int codeWidth = 3 + // start guard
(7 * 6) + // left bars
5 + // middle guard
(7 * 6) + // right bars
3; // end guard
codeWidth = Math.max(codeWidth, width);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
BarcodeFormat.EAN_13, codeWidth, height, null);
MatrixToImageWriter
.writeToFile(bitMatrix, "png", new File(imgPath));
} catch (Exception e) {
e.printStackTrace();
}
}
/** * Barcode decoding * @param imgPath * @return String */
public static String decode(String imgPath) {
BufferedImage image = null;
Result result = null;
try {
image = ImageIO.read(new File(imgPath));
if (image == null) {
System.out.println("the decode image may be not exit.");
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
result = new MultiFormatReader().decode(bitmap, null);
return result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/** * QR code coding * @param contents * @param width * @param height * @param imgPath */
public static void encode2(String contents, int width, int height, String imgPath) {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
// Specify error correction level
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// Specify the encoding format
hints.put(EncodeHintType.CHARACTER_SET, "GBK");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter
.writeToFile(bitMatrix, "png", new File(imgPath));
} catch (Exception e) {
e.printStackTrace();
}
}
/** * QR code decoding * @param imgPath * @return String */
public static String decode2(String imgPath) {
BufferedImage image = null;
Result result = null;
try {
image = ImageIO.read(new File(imgPath));
if (image == null) {
System.out.println("the decode image may be not exit.");
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "GBK");
result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/** * test * @param args */
public static void main(String[] args) {
// Bar code
String imgPath = "target\\tiaoxm.png"; // Barcode path
String contents = "66666666";
int width = 105, height = 50;
ZxingHandler.encode(contents, width, height, imgPath);
System.out.println("finished zxing EAN-13 encode.");
String decodeContent = ZxingHandler.decode(imgPath);
System.out.println(" The decoding is as follows :" + decodeContent);
System.out.println("finished zxing EAN-13 decode.");
// QR code
String imgPath2 = "target\\erwm.png"; // QR code path
String contents2 = "Hello, world!"
+ "\nBlog [ http:/www.baidu.com ]"
+ "\nEMail [ [email protected] ]";
int width2 = 300, height2 = 300;
ZxingHandler.encode2(contents2, width2, height2, imgPath2);
System.out.println("finished zxing encode.");
String decodeContent2 = ZxingHandler.decode2(imgPath2);
System.out.println(" The decoding is as follows :" + decodeContent2);
System.out.println("finished zxing decode.");
}
}
版权声明
本文为[Chen junyang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220552494996.html
边栏推荐
- [2021] Spatio-Temporal Graph Contrastive Learning
- [winui3] write an imitation Explorer file manager
- Docker installation and mysql5 7 installation
- TypeError: ‘Collection‘ object is not callable. If you meant to call the ......
- MySQL 慢查询
- [WinUI3]编写一个仿Explorer文件管理器
- redis和mysql区别
- Painless upgrade of pixel series
- POI export message list (including pictures)
- 跨境电商 | Facebook 和 Instagram:哪个社交媒体更适合你?
猜你喜欢

使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘
![[winui3] write an imitation Explorer file manager](/img/3e/62794f1939da7f36f7a4e9dbf0aa7a.png)
[winui3] write an imitation Explorer file manager

Deep learning notes - data expansion

POI export message list (including pictures)

Leetcode 1547: minimum cost of cutting sticks
![View, modify and delete [database] table](/img/a2/fcb38f2006772a1ec45cab520620ba.png)
View, modify and delete [database] table

《2021多多阅读报告》发布,95后、00后图书消费潜力攀升

独立站运营 | FaceBook营销神器——聊天机器人ManyChat

Deep learning notes - semantic segmentation and data sets

Details related to fingerprint payment
随机推荐
独立站运营 | FaceBook营销神器——聊天机器人ManyChat
[database] MySQL single table query
Introduction to raspberry pie 3B - system installation
DIY is an excel version of subnet calculator
Special topic of data intensive application system design
What are instruction cycles, machine cycles, and clock cycles?
Mac 进入mysql终端命令
【数据库】MySQL基本操作(基操~)
Thoughts on a small program
Graduation project
C. Tree infection (simulation + greed)
mysql5. 7. X data authorization leads to 1141
Learning Android from scratch -- baseactivity and activitycollector
多线程基本概念(并发与并行、线程与进程)和入门案例
TypeError: ‘Collection‘ object is not callable. If you meant to call the ......
Teach you how to build the ruoyi system by Tencent cloud
C list field sorting contains numbers and characters
Deep learning notes - fine tuning
DIY 一个 Excel 版的子网计算器
The difference between static pipeline and dynamic pipeline