Java實現(xiàn) 二維碼生成和解析

開發(fā)工具: IDEA

使用類庫下載地址:https://mvnrepository.com/

下載文件:
https://mvnrepository.com/artifact/com.google.zxing/core/3.2.1

https://mvnrepository.com/artifact/com.google.zxing/core/3.3.0

生成 二維碼

public class TestOR_Code_Create {

    public static void main(String[] args) throws WriterException, IOException {
        //保存的位置
        String path = "d:/info.png";
        //生成的圖片大小
        int width = 400, height = 400;
        //信息
        String name = "哈羅沃德";
        String company = "千鋒教育";
        String city = "重慶";
        String job = "java 老司機";
        StringBuilder info = new StringBuilder();
        info.append(name);
        info.append(company);
        info.append(job);
        info.append(city);
        //解決亂碼
        byte[] bytes = info.toString().getBytes(Charset.defaultCharset());
        String text = new String(bytes,Charset.forName("ISO-8859-1"));
        generateQRCodeImage(text,width,height,path);
        //提示,可不要
        System.out.println("生成完畢...");
    }

    /**
     * 封裝一個 生成 二維碼圖片的工具方法
     * @param text  生成的信息
     * @param width  圖片寬度
     * @param height 圖片高度
     * @param filePath 生成后保存的位置
     * @throws WriterException
     * @throws IOException
     */
    public static void generateQRCodeImage(String text, int width, int height, String filePath)
            throws WriterException, IOException {
        //創(chuàng)建一個圖片生成器
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        //創(chuàng)建一個BitMatrix 比特矩陣 對象 ,數(shù)據(jù)填充到矩陣上
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        //創(chuàng)建路徑對象
        Path path = FileSystems.getDefault().getPath(filePath);
        //輸出像素矩陣到圖片
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    }
}

解析 二維碼

public class TestOR_Code_Read {

    public static void main(String[] args) throws Exception {
        //指定二維碼圖片的位置
        String filepath = "d:/info.png";
        //調(diào)用封裝好的解析方法
        String s = parseORcode(filepath);
        //展示解析結(jié)果
        System.out.println(s);
    }
   /**
     * 封裝一個解析圖片二維碼的方法,用于提取二維碼中的內(nèi)容
     * @param filepath  圖片所在位置
     * @return 提取的信息
     * @throws Exception
     */
    public static String parseORcode(String filepath) throws Exception{
        //創(chuàng)建一個圖片對象接收
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filepath));
        //獲得圖片光線源
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        //獲得圖片二維源
        Binarizer binarizer = new HybridBinarizer(source);
        //獲得位圖
        BinaryBitmap bitmap = new BinaryBitmap(binarizer);
        //創(chuàng)建圖片配置信息map
        HashMap<DecodeHintType, Object> decodeHints = new HashMap<DecodeHintType, Object>();
        //設(shè)置編碼信息
        decodeHints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        //獲得結(jié)果
        Result result = new MultiFormatReader().decode(bitmap, decodeHints);
        //返回結(jié)果
        return result.getText();
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容