二维码制作与解析

文章目录


前言

扫码跳转地址,解析二维码,连接wifi。


一、加入依赖

<dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>android-core</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>android-integration</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.0</version>
        </dependency>

二、二维码制作

//图片的宽度和高度
int width = 300;
int height = 300;
//图片的格式
String format = "png";
//内容,此处放你想要放的内容(例如网页链接),若是网址链接,要加上 "http://" 才能被微信识别,
//地址:"http://baidu.com"
//扫码连接wife需要特定的字符串,自行搜索
String content = "这里填你想输入的信息";
//定义二维码的参数
HashMap hints = new HashMap();
//编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//容错等级,与二维码存储的大小有关,一般选择M
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
//边距
hints.put(EncodeHintType.MARGIN, 2);
//生成二维码
try {
    BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
    //存放二维码路径
    Path file = new File("E:/img.png").toPath();
    MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (Exception e) {
    e.printStackTrace();
}

三、二维码解析

try {
    MultiFormatReader multiFormatReader = new MultiFormatReader();
    //二维码的存放位置
    File file = new File("E:/img.png");
    //创建一个图片缓冲区
    BufferedImage image = ImageIO.read(file);
    //对图片进行解析
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
    //定义二维码的参数
    HashMap hints = new HashMap();
    //编码
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    //得到解析后的result
    Result result = multiFormatReader.decode(binaryBitmap, hints);
    System.out.println("解析结果:" + result.toString());
    System.out.println("二维码格式类型·:" + result.getBarcodeFormat());
} catch (NotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

 把代码拷贝到mian方法里面直接跑就行。


总结

以上就是今天要讲的内容,本文仅仅简单介绍了二维码的使用。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

予风北

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值