当前位置:网站首页>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
边栏推荐
- Independent station operation | Facebook marketing artifact - chat robot manychat
- Thoughts on a small program
- Wine (COM) - basic concept
- Spell it! Two A-level universities and six B-level universities have abolished master's degree programs in software engineering!
- Innovation training (V) configuration information
- 信息学奥赛一本通 1212:LETTERS | OpenJudge 2.5 156:LETTERS
- Mac 进入mysql终端命令
- Sword finger offer: the path with a certain value in the binary tree (backtracking)
- redis和mysql区别
- Perfect test of coil in wireless charging system with LCR meter
猜你喜欢
AQS source code reading
Deep learning notes - semantic segmentation and data sets
Transaction isolation level of MySQL transactions
[winui3] write an imitation Explorer file manager
深度学习笔记 —— 数据增广
Use model load_ state_ Attributeerror appears when dict(): 'STR' object has no attribute 'copy‘
Innovation training (IX) integration
[WinUI3]編寫一個仿Explorer文件管理器
MySQL -- execution process and principle of a statement
Perfect test of coil in wireless charging system with LCR meter
随机推荐
Basic concepts of multithreading (concurrency and parallelism, threads and processes) and entry cases
【数据库】MySQL多表查询(一)
How can continuous integration (CI) / continuous delivery (CD) revolutionize automated testing
Docker installation and mysql5 7 installation
A trinomial expression that causes a null pointer
Deep learning notes - object detection and dataset + anchor box
Innovation training (10)
[WinUI3]编写一个仿Explorer文件管理器
Progress of innovation training (III)
Pixel mobile phone brick rescue tutorial
JS engine loop mechanism: synchronous, asynchronous, event loop
Wechat payment function
redis和mysql区别
和谐宿舍(线性dp / 区间dp)
Mac 进入mysql终端命令
Custom switch control
Leetcode 1547: minimum cost of cutting sticks
【数据库】表的查看、修改和删除
Opencv + clion face recognition + face model training
[winui3] write an imitation Explorer file manager