所有分类
  • 所有分类
  • 网站源码

1、二维码简介

二维条形码是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,在代码编制上巧妙地利用构成计算机内部逻辑基础的“0”、“1”比特流的概念,使用若干个与二进制相对应的几何形体来表示文字数值内容信息,通过图象输入设备或光电扫描设备自动识读以实现信息自动处理。二维码具有条码技术的一些共性:每种码制有其特定的字符集;每个字符占有一定的宽度;具有一定的校验功能等。同时还具有对不同行的信息自动识别功能、及处理图形旋转变化等特点。

二维码纠错级别

二维码纠错级别指的是在识别二维码时,对于损坏或模糊的二维码的容错能力。

一般来说,二维码有四个纠错级别:

L (低):可以纠正7%左右的错误。

M (中):可以纠正15%左右的错误。

Q (高):可以纠正25%左右的错误。

H (高):可以纠正30%左右的错误。

总结:一般来说,使用较高的纠错级别会导致生成的二维码更大,但是它的容错能力也会更强。

2、ZXing简介

ZXing(Zebra Crossing)是Google开发的一个二维码解析和生成的开源库。

官网地址:http://code.google.com/p/zxing/

3、示例

通过Java调用Zxing实现二维码的生成

3.1 搭建一个**ven项目,引入Zxing依赖包

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

3.2 创建QrCodeUtil.java 类

具体实现代码如下:

package  QrCodeUtil;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.i**ge.BufferedI**ge;
import java.io.File;
import java.io.FileOutputStre**;
import java.io.IOException;
import java.io.OutputStre**;
import java.util.Date;
import java.util.Hashtable;

import javax.i**geio.I**geIO;


import com.alibaba.druid.util.StringUtils;
import com.google.zxing.BarcodeFor**t;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFor**tWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**
 * 生成二维码
 */
public class QrCodeUtil {

    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;
    private static final int **rgin = 0;
    private static final int LogoPart = 4;

    public static void **in(String[] args) throws WriterException {
        //二维码内容
        String content = "IT技术分享社区,一个有态度的互联网社区交流平台";
        String logoPath = "D:\logo.png"; // 二维码中间的logo信息 非必须
        String for**t = "jpg";
        int width = 120; // 二维码宽度
        int height = 120;// 二维码高度
        // 设置二维码矩阵的信息
        BitMatrix bitMatrix = setBitMatrix(content, width, height);
        // 设置输出流
        OutputStre** outStre** = null;
        String path = "d:/Code" + new Date().getTime() + ".png";//设置二维码的文件名
        try {
            outStre** = new FileOutputStre**(new File(path));
            // 目前 针对容错等级为H reduceWhiteArea  二维码空白区域的大小 根据实际情况设置,如果二维码内容长度不固定的话 需要自己根据实际情况计算reduceWhiteArea的大小
            writeToFile(bitMatrix, for**t, outStre**, logoPath, 5);
            outStre**.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置生成二维码矩阵信息
     * @par** content 二维码图片内容
     * @par** width   二维码图片宽度
     * @par** height  二维码图片高度
     * @throws WriterException
     */
    private static BitMatrix setBitMatrix(String content, int width, int height) throws WriterException {
        BitMatrix bitMatrix = null;
        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 指定编码方式,避免中文乱码
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 指定纠错等级 如果二维码里面的内容比较多的话推荐使用H 容错率30%, 这样可以避免一些扫描不出来的问题
        hints.put(EncodeHintType.MARGIN, **rgin); // 指定二维码四周白**域大小 官方的这个方法目前没有没有作用默认设置为0
        bitMatrix = new MultiFor**tWriter().encode(content, BarcodeFor**t.QR_CODE, width, height, hints);
        return bitMatrix;
    }
    /**
     * @par** **trix
     * @par** for**t
     * @par** outStre**
     * @par** logoPath        logo图片
     * @par** reduceWhiteArea 二维码空白区域设置
     * @throws IOException
     */
    private static void writeToFile(BitMatrix **trix, String for**t, OutputStre** outStre**, String logoPath, int reduceWhiteArea) throws IOException {
        BufferedI**ge i**ge = toBufferedI**ge(**trix, reduceWhiteArea);
        // 如果设置了二维码里面的logo 加入LOGO水印
        if (!StringUtils.isEmpty(logoPath)) {
            i**ge = addLogo(i**ge, logoPath);
        }
        I**geIO.write(i**ge, for**t, outStre**);
    }

    /**
     *
     * @par** **trix
     * @par** reduceWhiteArea
     * @return
     */
    private static BufferedI**ge toBufferedI**ge(BitMatrix **trix, int reduceWhiteArea) {
        int width = **trix.getWidth();
        int height = **trix.getHeight();
        BufferedI**ge i**ge = new BufferedI**ge(width - 2 * reduceWhiteArea, height - 2 * reduceWhiteArea, BufferedI**ge.TYPE_3BYTE_BGR);
        for (int x = reduceWhiteArea; x < width - reduceWhiteArea; x++) {
            for (int y = reduceWhiteArea; y < height - reduceWhiteArea; y++) {
                i**ge.setRGB(x - reduceWhiteArea, y - reduceWhiteArea, **trix.get(x, y) ? BLACK : WHITE);
            }
        }
        return i**ge;
    }

    /**
     * 给二维码图片中绘制logo信息 非必须
     * @par** i**ge    二维码图片
     * @par** logoPath logo图片路径
     */
    private static BufferedI**ge addLogo(BufferedI**ge i**ge, String logoPath) throws IOException {
        Graphics2D g = i**ge.createGraphics();
        BufferedI**ge logoI**ge = I**geIO.read(new File(logoPath));
        // 计算logo图片大小,可适应长方形图片,根据较短边生成正方形
        int width = i**ge.getWidth() < i**ge.getHeight() ? i**ge.getWidth() / LogoPart : i**ge.getHeight() / LogoPart;
        int height = width;
        // 计算logo图片放置位置
        int x = (i**ge.getWidth() - width) / 2;
        int y = (i**ge.getHeight() - height) / 2;
        // 在二维码图片上绘制中间的logo
        g.drawI**ge(logoI**ge, x, y, width, height, null);
        // 绘制logo边框,可选
        g.setStroke(new BasicStroke(2)); // 画笔粗细
        g.setColor(Color.WHITE); // 边框颜色
        g.drawRect(x, y, width, height); // 矩形边框
        logoI**ge.flush();
        g.dispose();
        return i**ge;
    }

}
常见问题
原文链接:https://www.yuanmawu.net/58500.html,转载请注明出处。
0

评论0

请先

显示验证码
没有账号?注册  忘记密码?