java生成二维码

本页所用的jar包
其它例子:Zxing和QR CODE 生成与解析二维码实例(带logo篇)
生成QRCode式二维码,三种方式(ZXing,QRcode,jQueryQRCode)

ZXing


先亮结果

java生成二维码_第1张图片

ZXing生成二维码(可用微信扫一扫)

package com.wang.demo;

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

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

/**
 * 生成二维码
 * @author wangxy
 *
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class CreateQRCode {

    public static void main(String[] args) {

        int width = 300;
        int height = 300;
        String format = "png";
        String content = "你咋不上天呢?";
        String urlcontent = "http://www.baidu.com";//链接的写法

        //定义二维码参数
        Map hints = new HashMap();
//      hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//文字编码 
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//容错级别
        hints.put(EncodeHintType.MARGIN, 2);//图片边距

        //生成二维码
        try {
            content = new String(content.getBytes("UTF-8"), "ISO-8859-1");//微信,UC可识别的编码格式
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
            Path file = new File("D:/code/img.png").toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }



}

ZXing解析二维码

package com.wang.demo;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

/**
 * 解析二维码
 * @author wangxy
 *
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class ReadQRCode {

    public static void main(String[] args) {
        MultiFormatReader formatReader = new MultiFormatReader();

        try {
            File file = new File("D:/code/img.png");

            BufferedImage image = ImageIO.read(file);

            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
                    new BufferedImageLuminanceSource(image)));

            // 定义二维码参数
            Map hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 文字编码

            Result result;

            result = formatReader.decode(binaryBitmap, hints);

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


    }

}

QRCode

亮结果

java生成二维码_第2张图片

生成

package com.wang.demo;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;

/**
 * http://www.swetake.com/qrcode/index-e.html 
 * https://osdn.jp/projects/qrcode/
 *
 * @author wangxy
 *
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class CreateQRCode {

    public static void main(String[] args) throws IOException {
        Qrcode qrcode = new Qrcode();
        qrcode.setQrcodeErrorCorrect('M');// 纠错等级(H L M Q)
        qrcode.setQrcodeEncodeMode('B');// N:数字 A:a-Z B:其它字符
        qrcode.setQrcodeVersion(7);// 版本 至49
        String qrData = "小样,你咋不上天啊!";
        int width = 67 + 12 * (7 - 1);// 公式 67+12*(版本号-1)
        int height = 67 + 12 * (7 - 1);

        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);

        // 定义画板
        Graphics2D gs = bufferedImage.createGraphics();
        gs.setBackground(Color.WHITE);
        gs.setColor(Color.BLACK);
        gs.clearRect(0, 0, width, height);

        int pixoff = 2;// 偏移量

        // 要绘制的内容
        byte[] d = qrData.getBytes("gb2312");

        if (d.length > 0 && d.length < 120) {
            boolean[][] s = qrcode.calQrcode(d);

            for (int i = 0; i < s.length; i++) {
                for (int j = 0; j < s.length; j++) {
                    if (s[j][i]) {
                        gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
                    }
                }
            }
        }

        gs.dispose();
        bufferedImage.flush();

        ImageIO.write(bufferedImage, "png", new File("D:/code/qrcode.png"));
    }
}

编译

package com.wang.demo;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.exception.DecodingFailedException;

/**
 * http://www.swetake.com/qrcode/index-e.html 
 * https://osdn.jp/projects/qrcode/
 *
 * @author wangxy
 *
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class ReadQRCode {

    public static void main(String[] args) throws DecodingFailedException, IOException {
        File file = new File("D:/code/qrcode.png");

        BufferedImage bufferedImage = ImageIO.read(file);

        QRCodeDecoder codeDecoder = new QRCodeDecoder();

        String result = new String(codeDecoder.decode(new MyQRCodeImage(bufferedImage)), "gb2312");

        System.out.println(result);
    }

}

实现接口

package com.wang.demo;

import java.awt.image.BufferedImage;

import jp.sourceforge.qrcode.data.QRCodeImage;

public class MyQRCodeImage implements QRCodeImage{

    BufferedImage bufferedImage;

    public MyQRCodeImage(BufferedImage bufferedImage) {
        this.bufferedImage = bufferedImage;
    }
    @Override
    public int getHeight() {
        return bufferedImage.getHeight();
    }

    @Override
    public int getPixel(int arg0, int arg1) {
        return bufferedImage.getRGB(arg0, arg1);
    }

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

}

jspQRCode

亮结果

java生成二维码_第3张图片

qrcode.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<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>
生成的二维码:<br>
<div id="qrcode">div>

<script type="text/javascript">
    jQuery('#qrcode').qrcode("www.baidu.com");
/*  jQuery('#qrcode').qrcode({width: 64,height: 64,text: "小样儿,你还真飞啊!"}); */
script>
<a href="https://github.com/jeromeetienne/jquery-qrcode">网站a>

body>
html>

你可能感兴趣的:(java基础)