條形碼
將寬度不等的多個黑條和白條,按照一定的編碼規則排序,用以表達一組信息的圖像標識符
通常代表一串數字 / 字母,每一位有特殊含義
一般數據容量30個數字 / 字母
二維碼
用某種特定幾何圖形按一定規律在平面(二維方向上)分布的黑白相間的圖形記錄數據符號信息
比一維條形碼能存儲更多信息,表示更多數據類型
能夠存儲數字 / 字母 / 漢字 / 圖片等信息
可存儲幾百到幾十kb字符
zxing
zxing主要是google出品的,用于識別一維碼和二維碼的第三方庫
主要類:
- bitmatrix位圖矩陣
- multiformatwriter位圖編寫器
- matrixtoimagewriter寫入圖片
maven導入zxing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
public static void main(string[] args) { generatecode( new file( "1dcode.png" ), "1390351289" , 500 , 250 ); } /** * @param file 生成的文件名稱 * @param code 一維碼存儲的數據信息 * @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(); } } |
注意:一維碼只能存儲數字和字母,其他數據會報failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project mavendemo: command execution failed.錯誤java
讀取一維碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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類
生成二維碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
/** 定義二維碼的寬度 */ 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) { // 定義二維碼參數 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" ), "淑玫唐家居網" ); } |
讀取二維碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/** * @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( "二維碼文本內容: " + new string(result.gettext().getbytes( "gbk" ), "gbk" )); } catch (exception e) { e.printstacktrace(); } } public static void main(string[] args) { readqrcode( new file( "smt.png" )); } |
注意: maven打印的控制臺中會出現中文亂碼,在idea setting->maven->runner vmoptions:-dfile.encoding=gb2312;即可解決
總結
到此這篇關于java生成讀取條形碼和二維碼的文章就介紹到這了,更多相關java生成讀取條形碼二維碼內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/weixin_43730516/article/details/117927316