二维码的简单实现

一、二维码的介绍:

  二维码是用某种特定的几何图形,按照一定的规律在平面(二维方向上)、分布的条、空(黑白)相间的图形记录数据的符号信息的图形。(二进制1) 代表黑点,二进制0代表空白。

二、二维码分类:

  二维条码有许多不同的码制,就码制的原理编码而言,通常分为三类:

  1:线性堆叠式二维码:

  2:矩阵式二维码:

  3:邮政码:


三、优点:

  1、高密度的编码,信息容量大(汉字)

  2、编码范围广。

  3、容错能力强。

  4、译码可靠性高

  5、可以引入加密措施。

  6、成本低、易制作、持久耐用。

  

  缺点:容易成为病毒......

四、开发过程:借助第三方jar,zxing(谷歌类库)和 qrcode.jar(java开发的)还有jquery.qrcode.js。

  开发工具(eclipse)

五、源码

(1)生成二维码

package com.zXing_code;

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

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;

public class CreateQRCode {
    

    public static void main(String[] args) throws IOException {
        //声名二维码图片的宽 、高及图片格式,二维码内容;
        int width = 300;
        int height = 300;
        String format = "jpg";
        String contents = "好好学习!天天向上!";
        //定义二维码参数(字符集,纠错等级,二维码边框的空度)
        HashMap params = new HashMap();
    
        params.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        //纠错等级(L:7%, M:15%, Q:25%, H:30%)  纠错等级越高,容纳的内容越少,二维码清晰度越低
        params.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        //边框空度
        params.put(EncodeHintType.MARGIN, 1);//非负数
        
        
        
        //现在进行二维码的生成
        //需要把上面的进行转码
        try {
            BitMatrix bitMatrix= new MultiFormatWriter().encode
            (contents, BarcodeFormat.QR_CODE, width, height,params);
            Path file = new File("F:/img/two.png").toPath();
            
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}

 效果如下:

扫描二维码关注公众号,回复: 7441971 查看本文章

(2)解析二维码

package com.jieMa;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.sun.prism.Image;
/**
 * 解析二维码
 * @author CRAZY&HACKER
 *
 */
public class ParseQRCode {
    
    public static void main(String[] args) throws NotFoundException, Exception {
        
        MultiFormatReader formatReader = new MultiFormatReader();
        //加载文件进来
        File file =new File("F:/img");
        //把上面的文件读取缓冲区
        BufferedImage image = ImageIO.read(file);
        //然后把他封装起来
        BinaryBitmap bitmap = new BinaryBitmap(
                    new HybridBinarizer(
                            new BufferedImageLuminanceSource(image)));
        HashMap params = new HashMap();
        
        params.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        //解码
         Result result=formatReader.decode(bitmap, params);
    }
    
}

猜你喜欢

转载自www.cnblogs.com/kingming/p/11645300.html