條形碼
將寬度不等的多個黑條和白條,按照一定的編碼規(guī)則排序,用以表達(dá)一組信息的圖像標(biāo)識符
通常代表一串?dāng)?shù)字 / 字母,每一位有特殊含義
一般數(shù)據(jù)容量30個數(shù)字 / 字母
二維碼
用某種特定幾何圖形按一定規(guī)律在平面(二維方向上)分布的黑白相間的圖形記錄數(shù)據(jù)符號信息
比一維條形碼能存儲更多信息,表示更多數(shù)據(jù)類型
能夠存儲數(shù)字 / 字母 / 漢字 / 圖片等信息
可存儲幾百到幾十kb字符
zxing
zxing主要是google出品的,用于識別一維碼和二維碼的第三方庫
主要類:
- bitmatrix位圖矩陣
- multiformatwriter位圖編寫器
- matrixtoimagewriter寫入圖片
maven導(dǎo)入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 一維碼存儲的數(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(); } } |
注意:一維碼只能存儲數(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
讀取一維碼
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(); } } |
注意:當(dāng)使用string類進(jìn)行轉(zhuǎn)碼時,要使用java.lang包的,maven導(dǎo)包的時候會導(dǎo)入第三方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) { // 定義二維碼參數(shù) map<encodehinttype, object> hints = new hashmap<encodehinttype, object>(); // 設(shè)置編碼 hints.put(encodehinttype.character_set, "utf-8" ); // 設(shè)置容錯等級 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(); // 保存到項目跟目錄中 matrixtoimagewriter.writetopath(bitmatrix, format, path); } catch (exception e) { e.printstacktrace(); } } public static void main(string[] args) { generateqrcode( new file( "smt.png" ), "淑玫唐家居網(wǎng)" ); } |
讀取二維碼
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( "解析結(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打印的控制臺中會出現(xiàn)中文亂碼,在idea setting->maven->runner vmoptions:-dfile.encoding=gb2312;即可解決
總結(jié)
到此這篇關(guān)于java生成讀取條形碼和二維碼的文章就介紹到這了,更多相關(guān)java生成讀取條形碼二維碼內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/weixin_43730516/article/details/117927316