亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2021年07月09日 16:10:51   作者:少年啊!  
條形碼(barcode)是將寬度不等的多個(gè)黑條和空白,按照一定的規(guī)則排列,用來表示一組信息的圖形標(biāo)識(shí)符,而二維碼大家應(yīng)該都很熟悉了,這篇文章主要給大家介紹了關(guān)于Java生成讀取條形碼和二維碼的相關(guān)資料,需要的朋友可以參考下

條形碼

將寬度不等的多個(gè)黑條和白條,按照一定的編碼規(guī)則排序,用以表達(dá)一組信息的圖像標(biāo)識(shí)符

通常代表一串?dāng)?shù)字 / 字母,每一位有特殊含義

一般數(shù)據(jù)容量30個(gè)數(shù)字 / 字母

二維碼

用某種特定幾何圖形按一定規(guī)律在平面(二維方向上)分布的黑白相間的圖形記錄數(shù)據(jù)符號(hào)信息

比一維條形碼能存儲(chǔ)更多信息,表示更多數(shù)據(jù)類型

能夠存儲(chǔ)數(shù)字 / 字母 / 漢字 / 圖片等信息

可存儲(chǔ)幾百到幾十KB字符

Zxing

Zxing主要是Google出品的,用于識(shí)別一維碼和二維碼的第三方庫

主要類:

  • BitMatrix位圖矩陣
  • MultiFormatWriter位圖編寫器
  • MatrixToImageWriter寫入圖片

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    生成的文件名稱
 * @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類進(jìn)行轉(zhuǎn)碼時(shí),要使用Java.lang包的,Maven導(dǎo)包的時(shí)候會(huì)導(dǎo)入第三方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>();
    // 設(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)文章

  • 詳解Java的閉包

    詳解Java的閉包

    這篇文章主要介紹了詳解Java的閉包,作者從Lambda和默認(rèn)方法等重要特性深入講解,極力推薦!需要的朋友可以參考下
    2015-07-07
  • Mybatis多數(shù)據(jù)源切換實(shí)現(xiàn)代碼

    Mybatis多數(shù)據(jù)源切換實(shí)現(xiàn)代碼

    這篇文章主要介紹了Mybatis多數(shù)據(jù)源切換實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • springboot整合token的實(shí)現(xiàn)代碼

    springboot整合token的實(shí)現(xiàn)代碼

    這篇文章主要介紹了springboot整合token的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • springboot整合curator實(shí)現(xiàn)分布式鎖過程

    springboot整合curator實(shí)現(xiàn)分布式鎖過程

    這篇文章主要介紹了springboot整合curator實(shí)現(xiàn)分布式鎖過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • MyBatis Plus更新對(duì)象無法設(shè)空值解決方案

    MyBatis Plus更新對(duì)象無法設(shè)空值解決方案

    這篇文章主要介紹了MyBatis Plus更新對(duì)象無法設(shè)空值解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • java日期處理工具類

    java日期處理工具類

    這篇文章主要為大家詳細(xì)介紹了java日期處理工具類,其次還介紹了日期處理的基礎(chǔ)知識(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Java如何通過屬性名獲取Object對(duì)象屬性值

    Java如何通過屬性名獲取Object對(duì)象屬性值

    這篇文章主要介紹了Java如何通過屬性名獲取Object對(duì)象屬性值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 使用ServletInputStream()輸入流讀取圖片方式

    使用ServletInputStream()輸入流讀取圖片方式

    這篇文章主要介紹了使用ServletInputStream()輸入流讀取圖片方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java編寫計(jì)算器的常見方法實(shí)例總結(jié)

    Java編寫計(jì)算器的常見方法實(shí)例總結(jié)

    這篇文章主要介紹了Java編寫計(jì)算器的常見方法,結(jié)合實(shí)例形式總結(jié)分析了Java實(shí)現(xiàn)計(jì)算器功能的常用方法,需要的朋友可以參考下
    2016-04-04
  • 從java面試題了解你所模糊的數(shù)組

    從java面試題了解你所模糊的數(shù)組

    這篇文章主要介紹了從java面試題了解你所模糊的數(shù)組,數(shù)組用來存儲(chǔ)一系列的數(shù)據(jù)項(xiàng),其中的每一項(xiàng)具有相同的基本數(shù)據(jù)類型、類或相同的父類。通過使用數(shù)組,可以在很大程度上縮短和簡化程序代碼,從而提高應(yīng)用程序的效率。,需要的朋友可以參考下
    2019-06-06

最新評(píng)論