QRCode和zxing的实例

二维码的分类: //线性堆叠式二维码 //矩阵式二维码 //邮政码

  • 目前流行的三大国际标准

  • PDF417:不支持中文

  • DM:专利未公开,需支付专利费用

  • QR code:专利公开,支持中文

  • Quick Response Code

  • QR code比其他二维码相比,具有识读速度快,数据密度大,占用空间小的优势。

  • QR Code是有日本Demso公司于1994年研制的一种矩阵二维码符号码

  • 纠错能力:

  • L级:约可纠错7%的数据码字

  • M级:约可纠错15%的数据码字

  • Q级:约可纠错25%的数据码字

  • H级:约可纠错30%的数据码字

  • 纠错能力越高,存储的数据就越少

  • 实例下载地址:http://download.csdn.net/detail/u010342038/9826681

  • Zxing的实例

  • package com.lohas.codedemo.zxing;

    import java.io.File;
    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.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) {
    //设置二维码的宽
    int width = 300;
    //设置二维码的长
    int height = 300;
    //设置二维码图片的格式
    String format = "png";
    //设置的连接地址
    String contents = "http://blog.csdn.net/u010342038";
    //二维码参数
    HashMap hints = new HashMap();
    //设置支持中文编码
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    //纠错等级
    // L级:约可纠错7%的数据码字
    // M级:约可纠错15%的数据码字
    // Q级:约可纠错25%的数据码字
    // H级:约可纠错30%的数据码字
    // 纠错能力越高,存储的数据就越少
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
    //边框
    hints.put(EncodeHintType.MARGIN, 2);

    try {
    BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height);
    File files = new File("F:/QRCode.png");
    Path file = files.toPath();
    MatrixToImageWriter.writeToPath(bitMatrix, format, file);
    } catch (Exception e) {
    e.printStackTrace();
    }

    System.out.println("生成二维码成功....");
    }


    }

  • ============================================

  • package com.lohas.codedemo.zxing;

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.util.Date;
    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.Result;
    import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
    import com.google.zxing.common.HybridBinarizer;
    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;


    //解析二维码
    public class ReadQRCode {

    public static void main(String[] args) {

    try {
    MultiFormatReader formatReader = new MultiFormatReader();
    File file = new File("F:/QRCode.png");
    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);
    hints.put(EncodeHintType.MARGIN, 2);
    Result result = formatReader.decode(binaryBitmap,hints);

    System.out.println("解析结果: " + result.toString());
    System.out.println("二维码格式: " + result.getBarcodeFormat());
    System.out.println("二维码URL: " + result.getText());
    System.out.println("解析二维码时间: " + new Date(result.getTimestamp()));

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

    }
    }



QRCode的实例

package com.lohas.qrcode;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;


//生成二维码
public class CreateQRCode {
public static void main(String[] args) {
Qrcode x = new Qrcode();
//纠错等级
//设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
x.setQrcodeErrorCorrect('M');
//N代表数字;A代表a—Z;B代表其他字符
x.setQrcodeEncodeMode('B');
//版本号(1-40)
//设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
x.setQrcodeVersion(7);
//设置的连接地址
String qrData = "http://blog.csdn.net/u010342038";
// 图片尺寸 
int width = 150;
int height = 150;
//设置输出图片的目录
File file = new File("F:/lohas.png");
//图片的格式
String format = "png";
//依托javaGUI的画图工具实现的
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufferedImage.createGraphics();
//设置图片的颜色
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLACK);
gs.clearRect(0, 0, width, height);
//偏移量
int pixoff = 2;
try {
// 获得内容的字节数组,设置编码格式  
byte[] d = qrData.getBytes("utf-8");
// 输出内容> 二维码 
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[i][j]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
//输出图片
ImageIO.write(bufferedImage, format, file);
System.out.println("生成二维码成功...");
} catch (Exception e) {
e.printStackTrace();
}
}
}

=======================================================

package com.lohas.qrcode;

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;
}

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

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

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

====================================================
package com.lohas.qrcode;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;

public class ReadQRCode {

public static void main(String[] args) {
File file = new File("F:/lohas.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();
}
}
}

你可能感兴趣的:(java,JAVA全面解析)