国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java生成讀取條形碼和二維碼的簡單示例

Java生成讀取條形碼和二維碼的簡單示例

2021-10-03 14:17少年啊! Java教程

條形碼(barcode)是將寬度不等的多個黑條和空白,按照一定的規(guī)則排列,用來表示一組信息的圖形標(biāo)識符,而二維碼大家應(yīng)該都很熟悉了,這篇文章主要給大家介紹了關(guān)于Java生成讀取條形碼和二維碼的相關(guān)資料,需要的朋友可以參考下

條形碼

將寬度不等的多個黑條和白條,按照一定的編碼規(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

延伸 · 閱讀

精彩推薦
  • Java教程小米推送Java代碼

    小米推送Java代碼

    今天小編就為大家分享一篇關(guān)于小米推送Java代碼,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧...

    富貴穩(wěn)中求8032021-07-12
  • Java教程Java BufferWriter寫文件寫不進(jìn)去或缺失數(shù)據(jù)的解決

    Java BufferWriter寫文件寫不進(jìn)去或缺失數(shù)據(jù)的解決

    這篇文章主要介紹了Java BufferWriter寫文件寫不進(jìn)去或缺失數(shù)據(jù)的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望...

    spcoder14552021-10-18
  • Java教程升級IDEA后Lombok不能使用的解決方法

    升級IDEA后Lombok不能使用的解決方法

    最近看到提示IDEA提示升級,尋思已經(jīng)有好久沒有升過級了。升級完畢重啟之后,突然發(fā)現(xiàn)好多錯誤,本文就來介紹一下如何解決,感興趣的可以了解一下...

    程序猿DD9332021-10-08
  • Java教程Java8中Stream使用的一個注意事項

    Java8中Stream使用的一個注意事項

    最近在工作中發(fā)現(xiàn)了對于集合操作轉(zhuǎn)換的神器,java8新特性 stream,但在使用中遇到了一個非常重要的注意點(diǎn),所以這篇文章主要給大家介紹了關(guān)于Java8中S...

    阿杜7472021-02-04
  • Java教程Java使用SAX解析xml的示例

    Java使用SAX解析xml的示例

    這篇文章主要介紹了Java使用SAX解析xml的示例,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下...

    大行者10067412021-08-30
  • Java教程Java實(shí)現(xiàn)搶紅包功能

    Java實(shí)現(xiàn)搶紅包功能

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)搶紅包功能,采用多線程模擬多人同時搶紅包,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙...

    littleschemer13532021-05-16
  • Java教程xml與Java對象的轉(zhuǎn)換詳解

    xml與Java對象的轉(zhuǎn)換詳解

    這篇文章主要介紹了xml與Java對象的轉(zhuǎn)換詳解的相關(guān)資料,需要的朋友可以參考下...

    Java教程網(wǎng)2942020-09-17
  • Java教程20個非常實(shí)用的Java程序代碼片段

    20個非常實(shí)用的Java程序代碼片段

    這篇文章主要為大家分享了20個非常實(shí)用的Java程序片段,對java開發(fā)項目有所幫助,感興趣的小伙伴們可以參考一下 ...

    lijiao5352020-04-06
主站蜘蛛池模板: 国产一级黄片毛片 | 成人男女啪啪免费观软件 | 亚洲每日更新 | 免费一区二区三区 | 91麻豆精品国产91久久久更新资源速度超快 | av中文字幕在线播放 | 精品伊人 | 日韩欧美一区视频 | 色爱亚洲| 欧美性猛交一区二区三区精品 | 一区二区三区日韩 | 国产毛片毛片 | 久久久久久久国产 | 黄色一级视频在线观看 | 91视视频在线观看入口直接观看 | 欧美久久久网站 | 中文字幕精品一区二区精品 | www久草 | 亚洲高清久久 | 欧美激情视频一区二区三区不卡 | 毛片网站大全 | 欧美日韩一区精品 | 波多野结衣一区二区三区免费视频 | 午夜精品一区二区三区在线播放 | 亚洲伦理影院 | 精品亚洲一区二区三区四区五区 | 中文字幕在线影院 | 免费观看黄色 | 欧美精品v国产精品v日韩精品 | 亚洲一区二区中文字幕 | 精品久久国产 | 亚洲欧美日韩电影 | 午夜在线电影 | 日比毛片| 天堂一区| 国外成人在线视频网站 | 亚洲成av人片在线观看无码 | 91久久久久久久久 | 日本三级视频在线观看 | 欧美精品一区二区三区一线天视频 | 亚洲日本在线观看视频 |