Java生成二维码

Java生成二维码

Maven依赖

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

生成二维码

public static void generateQRCodeImage(String text, int width, int height, String filePath, HashMap hints)
    throws WriterException, IOException {
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints);
    Path path = FileSystems.getDefault().getPath(filePath);
    MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
}

解析二维码

public static void parseQRCodeImage(String filePath,HashMap hints)
    throws NotFoundException, IOException, ChecksumException, FormatException {
    QRCodeReader qrCodeReader = new QRCodeReader();
    File file = new File(filePath);
    BufferedImage bufferedImage = ImageIO.read(file);
    LuminanceSource luminanceSource = new BufferedImageLuminanceSource(bufferedImage);
    Binarizer binarizer = new HybridBinarizer(luminanceSource);
    BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
    Result result = qrCodeReader.decode(binaryBitmap);
    System.out.println(result.getText());
}

Main方法

public static void main(String[] args) {
    int width = 400;
    int height = 400;
    String format = "png";
    String content = "https://www.baidu.com";
    String filePath = "E:/dev/test.png";

    HashMap<EncodeHintType, Object> hints = new HashMap<>();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    //L 水平 7%的字码可被修正
    //M 水平 15%的字码可被修正
    //Q 水平 25%的字码可被修正
    //H 水平 30%的字码可被修正
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
    hints.put(EncodeHintType.MARGIN, 2);

    try {
        generateQRCodeImage(content, width, height, filePath, hints);
        parseQRCodeImage(filePath,hints);
    } catch (WriterException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ChecksumException e) {
        throw new RuntimeException(e);
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    } catch (FormatException e) {
        throw new RuntimeException(e);
    }
}

Controller方法

@Controller
@RequestMapping("qr")
public class QrController {
    @RequestMapping
    public void createQrCode(HttpServletResponse response) throws WriterException, IOException {
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN, 2);
        BitMatrix bitMatrix = new QRCodeWriter().encode("content", BarcodeFormat.QR_CODE, 200, 200, hints);
        MatrixToImageWriter.writeToStream(bitMatrix, "png", response.getOutputStream());
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 简介 ZXing 是一个开源 Java 类库用于解析多种格式的 1D/2D 条形码。目标是能够对QR编码、Data...
    码农突围阅读 2,948评论 0 0
  • 简介 ZXing 是一个开源 Java 类库用于解析多种格式的 1D/2D 条形码。目标是能够对QR编码、Data...
    码农突围阅读 3,496评论 0 0
  • 前言: 先聊聊题外话,话说在1994年,日本的丰田汽车公司独立出来了一个电装公司。由于高精度的汽车零配件需要匹配很...
    贪挽懒月阅读 14,883评论 13 31
  • 这一篇文章我们就用 Java 来生成一下仿金山词霸的海报。 As long as you can still gr...
    沉默王二阅读 5,316评论 0 1
  • 首先需要两个jar :QRCode.jar,Qrcodeen.jar 接下来就是具体实现了下载地址 http://...
    天空中的牛阅读 4,425评论 0 0

友情链接更多精彩内容