Java生成讀取條形碼和二維碼的簡單示例
條形碼
將寬度不等的多個黑條和白條,按照一定的編碼規(guī)則排序,用以表達一組信息的圖像標識符
通常代表一串數(shù)字 / 字母,每一位有特殊含義
一般數(shù)據(jù)容量30個數(shù)字 / 字母
二維碼
用某種特定幾何圖形按一定規(guī)律在平面(二維方向上)分布的黑白相間的圖形記錄數(shù)據(jù)符號信息
比一維條形碼能存儲更多信息,表示更多數(shù)據(jù)類型
能夠存儲數(shù)字 / 字母 / 漢字 / 圖片等信息
可存儲幾百到幾十KB字符
Zxing
Zxing主要是Google出品的,用于識別一維碼和二維碼的第三方庫
主要類:
- BitMatrix位圖矩陣
- MultiFormatWriter位圖編寫器
- MatrixToImageWriter寫入圖片
Maven導入Zxing
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
生成一維碼java
public static void main(String[] args) {
generateCode(new File("1dcode.png"), "1390351289", 500, 250);
}
/**
* @param file 生成的文件名稱
* @param code 一維碼存儲的數(shù)據(jù)信息
* @param width 生成圖片的寬度
* @param height 生成圖片的高度
* @return void
* */
public static void generateCode(File file, String code, int width, int height){
// 定義位圖矩陣BitMatrix
BitMatrix matrix = null;
try {
// 使用code_128格式進行編碼生成100*25的條形碼
MultiFormatWriter writer = new MultiFormatWriter();
matrix = writer.encode(code, BarcodeFormat.CODE_128, width, height, null);
} catch (WriterException e) {
e.printStackTrace();
}
// 將位圖矩陣BitMatrix保存為圖片
try {
FileOutputStream outputStream = new FileOutputStream(file);
ImageIO.write(MatrixToImageWriter.toBufferedImage(matrix), "png", outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
注意:一維碼只能存儲數(shù)字和字母,其他數(shù)據(jù)會報Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project MavenDemo: Command execution failed.錯誤java
讀取一維碼
public static void main(String[] args) {
readCode(new File("1dcode.png"));
}
/**
* @param readImage 讀取一維碼圖片名
* @return void
* */
public static void readCode(File readImage) {
try {
BufferedImage image = ImageIO.read(readImage);
if (image == null) {
return;
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "gbk");
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
Result result = new MultiFormatReader().decode(bitmap, hints);
System.out.println(result.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
注意:當使用String類進行轉碼時,要使用Java.lang包的,Maven導包的時候會導入第三方Apache的String類
生成二維碼
/** 定義二維碼的寬度 */
private final static int WIDTH = 300;
/** 定義二維碼的高度 */
private final static int HEIGHT = 300;
/** 定義二維碼的格式 */
private final static String FORMAT = "png";
/**
* @param file
* @param content
* @return void
* */
public static void generateQRCode(File file, String content) {
// 定義二維碼參數(shù)
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
// 設置編碼
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 設置容錯等級
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
// 設置邊距,默認為5
hints.put(EncodeHintType.MARGIN, 2);
try {
BitMatrix bitMatrix = new MultiFormatWriter()
.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
Path path = file.toPath();
// 保存到項目跟目錄中
MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
generateQRCode(new File("smt.png"), "淑玫唐家居網(wǎng)");
}
讀取二維碼
/**
* @param file 讀取二維碼的文件名
* @return void
* */
public static void readQRCode(File file) {
MultiFormatReader reader = new MultiFormatReader();
try {
BufferedImage image = ImageIO.read(file);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
Map<DecodeHintType, Object> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = reader.decode(binaryBitmap, hints);
System.out.println("解析結果: " + new String(result.toString().getBytes("GBK"), "GBK"));
System.out.println("二維碼格式: " + result.getBarcodeFormat());
System.out.println("二維碼文本內(nèi)容: " + new String(result.getText().getBytes("GBK"), "GBK"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
readQRCode(new File("smt.png"));
}
注意: Maven打印的控制臺中會出現(xiàn)中文亂碼,在IDEA Setting->maven->runner VMoptions:-Dfile.encoding=GB2312;即可解決
總結
到此這篇關于Java生成讀取條形碼和二維碼的文章就介紹到這了,更多相關Java生成讀取條形碼二維碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用kotlin集成springboot開發(fā)的超詳細教程
目前大多數(shù)都在使用java集成 springboot進行開發(fā),本文演示僅僅將 java換成 kotlin,其他不變的情況下進行開發(fā),需要的朋友可以參考下2021-09-09
Spring?@Configuration?proxyBeanMethods=false問題
這篇文章主要介紹了Spring?@Configuration?proxyBeanMethods=false問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
高并發(fā)環(huán)境下安全修改同一行數(shù)據(jù)庫數(shù)據(jù)的策略分享
隨著互聯(lián)網(wǎng)技術的發(fā)展,越來越多的應用需要在高并發(fā)環(huán)境中運行,數(shù)據(jù)庫的并發(fā)控制成為了業(yè)務的關鍵,本文將介紹如何在高并發(fā)情況下,安全地修改數(shù)據(jù)庫中的同一行數(shù)據(jù),需要的可以參考一下2023-06-06
Java實戰(zhàn)之課程在線學習系統(tǒng)的實現(xiàn)
本文將采用SpringBoot+Spring+Mybatis+Thyeleaf實現(xiàn)一個課程在線學習系統(tǒng),采用SpringBoot框架實現(xiàn)?前臺模板用的thymeleaf數(shù)據(jù)庫層采用mybatis框架注解模式,感興趣的可以了解一下2022-04-04
mybatis plus開發(fā)過程中遇到的問題記錄及解決
這篇文章主要介紹了mybatis plus開發(fā)過程中遇到的問題記錄及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

