三种方式实现二维码(java)

一. 通过使用zxing方式实现: 
jar准备: https://github.com/zxing/zxing 下载源代码,将core/src/main/Java/下的所有文件和javase/src/main/java/下的所有文件一起打成jar文件zxing.jar

创建二维码:

@SuppressWarnings({"rawtypes", "unchecked"})
    private static void createZxing() throws WriterException, IOException {
        int width=300;
        int hight=300;
        String format="png";
        String content="www.baidu.com";
        HashMap hints=new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//纠错等级L,M,Q,H
        hints.put(EncodeHintType.MARGIN, 2); //边距
        BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, hight, hints);
        Path file=new File("D:/download/imag.png").toPath();
        MatrixToImageWriter.writeToPath(bitMatrix, format, file);
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

读取二维码:

private static void readZxing() throws IOException, NotFoundException {
        MultiFormatReader read = new MultiFormatReader();
        File file=new File("D:/download/imag.png");
        BufferedImage image=ImageIO.read(file);
        Binarizer binarizer=new HybridBinarizer(new BufferedImageLuminanceSource(image));
        BinaryBitmap binaryBitmap=new BinaryBitmap(binarizer);
        Result res=read.decode(binaryBitmap);
        System.out.println(res.toString());
        System.out.println(res.getBarcodeFormat());
        System.out.println(res.getText());
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

二. 使用QRCode方式实现二维码

jar包准备:Qrcode_A.jar,qrcode_B.jar 
创建二维码:

    private static void createORcode() throws UnsupportedEncodingException, IOException {
        Qrcode qrcode=new Qrcode();
        qrcode.setQrcodeErrorCorrect('M'); //纠错等级L M Q H
        qrcode.setQrcodeEncodeMode('B'); //N:数字,A:a-Z ,B:其他字符
        int version = 7;
        qrcode.setQrcodeVersion(version);
        String qrData="www.baidu.com";
        byte data[]=qrData.getBytes("gb2312");
        int width = 67+12*(version-1);
        int hight=67+12*(version-1);
        BufferedImage bufferedImage=new BufferedImage(width, hight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2d=bufferedImage.createGraphics();
        graphics2d.setBackground(Color.white);
        graphics2d.setColor(Color.black);
        graphics2d.clearRect(0, 0, width, hight);
        int pixoff=2; //偏移量
        if (data.length>0 && data.length<120) {
            boolean[][] s=qrcode.calQrcode(data);
            for (int i = 0; i < s.length; i++) {
                for (int j = 0; j < s.length; j++) {
                    if (s[j][i]) {
                        graphics2d.fillRect(j*3+pixoff, i*3+pixoff, 3, 3);
                    }
                }
            }
        }
        graphics2d.dispose();
        bufferedImage.flush();
        ImageIO.write(bufferedImage, "png", new File("D:/download/imag.png"));
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

读取二维码:两种方式:1.上诉readZxing()的方式,2.如下

   private static void readQRcode() throws IOException, NotFoundException {
        File file=new File("D:/download/imag.png");
        BufferedImage bufferedImage=ImageIO.read(file);
        QRCodeDecoder codeDecoder=new QRCodeDecoder();
        String res=new String(codeDecoder.decode(new MyQRCodeImage(bufferedImage)),"gb2312");
        System.out.println(res);
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

三. 使用js插件实现二维码

js文件准备:jQuery.min.js,jquery.qrcode.min.js 
导入js文件,然后编写如下代码,访问该页面。

    <div id="qrcode"></div>
    <script type="text/javascript">
        $("#qrcode").qrcode("www.baidu.com");
    </script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值