Java生成二维码

来源:http://www.imooc.com/learn/531

使用zxing方式

这里写图片描述

zxing方式生成二维码

需要使用的jar包地址:https://github.com/zxing/

CreateQRCode.java

package com.peng.zxing;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;

/**
 * zxing方式生成二维码
 * jar包地址:https://github.com/zxing/
 * Created by Peng
 * Time: 2017/3/29 16:13
 */
public class CreateQRCode {
    public static void main(String[] args) {
        int width = 300;
        int height = 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);
        //边距
        hints.put(EncodeHintType.MARGIN, 2);

        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            Path file = new File("D:/files/img.png").toPath();
            //注意:D:/files 目录必须存在,不会自动创建
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

        }
    }
}

结果:
这里写图片描述

使用zxing方式解析二维码

ReadQRCode.java

package com.peng.zxing;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

/**
 * zxing方式读取二维码
 * Created by Peng
 * Time: 2017/3/29 16:37
 */
public class ReadQRCode {
    public static void main(String[] args) {
        MultiFormatReader formatReader = new
                MultiFormatReader();
        File file = new File("D:/files/img.png");
        try {
            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");
            //纠错等级
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

            Result result = formatReader.decode(binaryBitmap,hints);

            System.out.println("解析结果:"+result.toString());
            System.out.println("二维码的格式类型:"+result.getBarcodeFormat());
            System.out.println("二维码文本内容:"+result.getText());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
    }
}

使用QR Code方式

使用QR Code方式生成二维码

jar包地址:http://www.swetake.com/qrcode/java/qr_java.html

CreateQRCode.java

package com.peng.qrcode;

import com.swetake.util.Qrcode;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * QRCode方式 生成二维码
 * jar包地址: http://www.swetake.com/qrcode/java/qr_java.html
 * Created by Peng
 * Time: 2017/3/29 16:58
 */
public class CreateQRcode {
    public static void main(String[] args) {

        Qrcode x = new Qrcode();
        x.setQrcodeErrorCorrect('M');//纠错等级
        x.setQrcodeEncodeMode('B');//N代表数字,A代表a-Z,B代表其他字符
        x.setQrcodeVersion(7);//版本
        String qrData = "www.baidu.com 百度";
        int width = 67+12*(7-1);
        int height = 67+12*(7-1);
        BufferedImage bufferedImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = bufferedImage.createGraphics();
        graphics2D.setBackground(Color.WHITE);
        graphics2D.setColor(Color.black);
        graphics2D.clearRect(0, 0, width, height);
        int pixoff = 2;//偏移量

        //向画板内存放数据
        byte[] d = new byte[0];
        try {
            d = qrData.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (d.length > 0 && d.length < 120) {
            boolean[][] s = x.calQrcode(d);

            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, 3, 3);
                    }
                }
            }
        }
        graphics2D.dispose();
        bufferedImage.flush();
        try {
            ImageIO.write(bufferedImage,"png",new File("D:/files/qrcode.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

结果:
这里写图片描述

使用QRCode解析二维码

jar包地址:https://osdn.jp/projects/qrcode/

ReadQRCode.java

package com.peng.qrcode;

import jp.sourceforge.qrcode.QRCodeDecoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * QRCode方式 读取二维码
 * jar包地址:https://osdn.jp/projects/qrcode/
 * Created by Peng
 * Time: 2017/3/29 17:15
 */
public class ReadQRCode {
    public static void main(String[] args) {
        File file = new File("D:/files/qrcode.png");
        try {
            BufferedImage bufferedImage = ImageIO.read(file);
            QRCodeDecoder codeDecoder = new QRCodeDecoder();
           String result =  new String (codeDecoder.decode(new MyQRCodeImage(
                   bufferedImage)),"utf-8");
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

MyQRCodeImage.java

package com.peng.qrcode;

import jp.sourceforge.qrcode.data.QRCodeImage;

import java.awt.image.BufferedImage;

/**
 * Created by Peng
 * Time: 2017/3/29 17:17
 */
public class MyQRCodeImage implements QRCodeImage {
    BufferedImage bufferedImage;
    public  MyQRCodeImage(BufferedImage bufferedImage){
        this.bufferedImage = bufferedImage;

    }
    @Override
    public int getWidth() {
        return bufferedImage.getWidth();
    }

    @Override
    public int getHeight() {
        return bufferedImage.getHeight();
    }

    @Override
    public int getPixel(int i, int i1) {
        return bufferedImage.getRGB(i,i1);
    }
}

使用jquery-qrcode方式

这里写图片描述

使用jquery-qrcode生成二维码

jar包下载地址:https://github.com/jeromeetienne/jquery-qrcode

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: Peng
  Date: 2017/3/29
  Time: 17:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html" charset="utf-8"/>
    <title>生成二维码</title>
    <script type="text/javascript" src="<%=request.getContextPath()%>js/jquery.min.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>js/jquery.qrcode.min.js"></script>
  </head>
  <body>
  <%--地址https://github.com/jeromeetienne/jquery-qrcode--%>
    生成的二维码如下:<br>
  <div id="qrcode"></div>

  <script type="text/javascript">

      jQuery('#qrcode').qrcode("www.jxust-nc.cn 南昌");
  </script>
  </body>
</html>

项目通过Tomcat发布后,可以看到结果

结果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值