Java生成讀取條形碼和二維碼的簡(jiǎn)單示例
條形碼
將寬度不等的多個(gè)黑條和白條,按照一定的編碼規(guī)則排序,用以表達(dá)一組信息的圖像標(biāo)識(shí)符
通常代表一串?dāng)?shù)字 / 字母,每一位有特殊含義
一般數(shù)據(jù)容量30個(gè)數(shù)字 / 字母
二維碼
用某種特定幾何圖形按一定規(guī)律在平面(二維方向上)分布的黑白相間的圖形記錄數(shù)據(jù)符號(hào)信息
比一維條形碼能存儲(chǔ)更多信息,表示更多數(shù)據(jù)類(lèi)型
能夠存儲(chǔ)數(shù)字 / 字母 / 漢字 / 圖片等信息
可存儲(chǔ)幾百到幾十KB字符
Zxing
Zxing主要是Google出品的,用于識(shí)別一維碼和二維碼的第三方庫(kù)
主要類(lèi):
- BitMatrix位圖矩陣
- MultiFormatWriter位圖編寫(xiě)器
- MatrixToImageWriter寫(xiě)入圖片
Maven導(dǎo)入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 生成的文件名稱(chēng)
* @param code 一維碼存儲(chǔ)的數(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格式進(jìn)行編碼生成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();
}
}
注意:一維碼只能存儲(chǔ)數(shù)字和字母,其他數(shù)據(jù)會(huì)報(bào)Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project MavenDemo: Command execution failed.錯(cuò)誤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();
}
}
注意:當(dāng)使用String類(lèi)進(jìn)行轉(zhuǎn)碼時(shí),要使用Java.lang包的,Maven導(dǎo)包的時(shí)候會(huì)導(dǎo)入第三方Apache的String類(lèi)
生成二維碼
/** 定義二維碼的寬度 */
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>();
// 設(shè)置編碼
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 設(shè)置容錯(cuò)等級(jí)
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
// 設(shè)置邊距,默認(rèn)為5
hints.put(EncodeHintType.MARGIN, 2);
try {
BitMatrix bitMatrix = new MultiFormatWriter()
.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
Path path = file.toPath();
// 保存到項(xiàng)目跟目錄中
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("解析結(jié)果: " + 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打印的控制臺(tái)中會(huì)出現(xiàn)中文亂碼,在IDEA Setting->maven->runner VMoptions:-Dfile.encoding=GB2312;即可解決
總結(jié)
到此這篇關(guān)于Java生成讀取條形碼和二維碼的文章就介紹到這了,更多相關(guān)Java生成讀取條形碼二維碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用kotlin集成springboot開(kāi)發(fā)的超詳細(xì)教程
目前大多數(shù)都在使用java集成 springboot進(jìn)行開(kāi)發(fā),本文演示僅僅將 java換成 kotlin,其他不變的情況下進(jìn)行開(kāi)發(fā),需要的朋友可以參考下2021-09-09
使用@Validated注解進(jìn)行校驗(yàn)卻沒(méi)有效果的解決
這篇文章主要介紹了使用@Validated注解進(jìn)行校驗(yàn)卻沒(méi)有效果的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
關(guān)于各種排列組合java算法實(shí)現(xiàn)方法
這篇文章介紹了幾種用JAVA實(shí)現(xiàn)的排列組合算法,有需要的朋友可以參考一下2013-06-06
Spring?@Configuration?proxyBeanMethods=false問(wèn)題
這篇文章主要介紹了Spring?@Configuration?proxyBeanMethods=false問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
高并發(fā)環(huán)境下安全修改同一行數(shù)據(jù)庫(kù)數(shù)據(jù)的策略分享
隨著互聯(lián)網(wǎng)技術(shù)的發(fā)展,越來(lái)越多的應(yīng)用需要在高并發(fā)環(huán)境中運(yùn)行,數(shù)據(jù)庫(kù)的并發(fā)控制成為了業(yè)務(wù)的關(guān)鍵,本文將介紹如何在高并發(fā)情況下,安全地修改數(shù)據(jù)庫(kù)中的同一行數(shù)據(jù),需要的可以參考一下2023-06-06
Java實(shí)戰(zhàn)之課程在線(xiàn)學(xué)習(xí)系統(tǒng)的實(shí)現(xiàn)
本文將采用SpringBoot+Spring+Mybatis+Thyeleaf實(shí)現(xiàn)一個(gè)課程在線(xiàn)學(xué)習(xí)系統(tǒng),采用SpringBoot框架實(shí)現(xiàn)?前臺(tái)模板用的thymeleaf數(shù)據(jù)庫(kù)層采用mybatis框架注解模式,感興趣的可以了解一下2022-04-04
mybatis plus開(kāi)發(fā)過(guò)程中遇到的問(wèn)題記錄及解決
這篇文章主要介紹了mybatis plus開(kāi)發(fā)過(guò)程中遇到的問(wèn)題記錄及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

