java框架---->zxing框架的使用

  今天我们就来学习一下zxing的用法,这里只涉及到java se的部分。

zxing的学习

如果在java se环境中使用zxing,需要在maven的pom.xml中添加依赖:

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

一、根据内容生成二维码

public void encodeNoReturn(String content) {
    try {
        Path path = new File("file/huhx.png").toPath();
        BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300);
        MatrixToImageWriter.writeToPath(matrix, "png", path);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

 

二、根据二维码读取内容

public static void main(String[] args) {
    File file = new File("file/huhx.jpg");
    BufferedImage bufferedImage = null;
    try {
        bufferedImage = ImageIO.read(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
    hints.put(DecodeHintType.CHARACTER_SET, "GBK");
    Result result = null;
    try {
        result = new MultiFormatReader().decode(bitmap, hints);
    } catch (NotFoundException e) {
        e.printStackTrace();
    }
    String url = result.getText();
    System.out.println(url);
}

 

友情链接

 

转载于:https://www.cnblogs.com/huhx/p/baseusejavazxing1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值