zoukankan      html  css  js  c++  java
  • 二维码解码编码

    pom引入包:

    <dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.4.0</version>
    </dependency>
    <dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.4.0</version>
    </dependency>
    <dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.4.0</version>
    </dependency>


    /**
    * 解析图片文件上的二维码
    *
    * @param imageFile 图片文件
    * @return 解析的结果,null表示解析失败
    */
    private static String decode(File imageFile) {
    try {
    BufferedImage image = ImageIO.read(imageFile);
    LuminanceSource luminanceSource = new BufferedImageLuminanceSource(image);
    Binarizer binarizer = new HybridBinarizer(luminanceSource);

    BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);

    Map<DecodeHintType, Object> hints = new HashMap<>();
    hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
    //优化精度
    hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
    //复杂模式
    hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
    Result result = new QRCodeReader().decode(binaryBitmap, hints);

    return result.getText();
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    }

    /**
    * Zxing图形码生成工具
    *
    * @param contents 内容
    * @param barcodeFormat BarcodeFormat对象
    * @param format 图片格式,可选[png,jpg,bmp]
    * @param width 宽
    * @param height 高
    * @param margin 边框间距px
    * @param saveImgFilePath 存储图片的完整位置,包含文件名
    * @return {boolean}
    */
    public static boolean encode(String contents, BarcodeFormat barcodeFormat, Integer margin,
    ErrorCorrectionLevel errorLevel, String format, int width, int height, String saveImgFilePath) {
    Boolean bool = false;
    BufferedImage bufImg;
    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
    // 指定纠错等级
    hints.put(EncodeHintType.ERROR_CORRECTION, errorLevel);
    hints.put(EncodeHintType.MARGIN, margin);
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    try {
    // contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1");
    BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, barcodeFormat, width, height, hints);
    MatrixToImageConfig config = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);
    bufImg = MatrixToImageWriter.toBufferedImage(bitMatrix, config);
    bool = writeToFile(bufImg, format, saveImgFilePath);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return bool;
    }

    测试方法:
    public static void main(String[] args) {
    // 解码
    String result = decode(new File("D:\\微信二维码.jpg"));
    System.out.println("解码二维码:" + result);
    //生成二维码
    String newQrCode = "D://newQrCode.png";
    encode("http://outer.qtoubao.cn/work/card/index.html?sev_no=10002895", BarcodeFormat.QR_CODE, 3, ErrorCorrectionLevel.H, "png", 200, 200,
    newQrCode);
    }



  • 相关阅读:
    phpmyadmin和网页上面的乱码问题
    整理: Android HAL
    warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]-给char* 形参 传入 宏定义字符串
    ubuntu 输入法莫名其妙变繁体
    linux内核版本号添加字符/为何有时会自动添加“+”号以及怎么去掉
    Unable to handle kernel NULL pointer dereference at virtual address 00000000
    Ubuntu 18.04 安装 Samba 服务器及配置
    linux查看版本信息
    图解:电压掉电监测电路如何实现检测工作?
    精密全波整流+一阶RC滤波器检测市电电压
  • 原文地址:https://www.cnblogs.com/hanwuxing/p/13516765.html
Copyright © 2011-2022 走看看