用QRCode生成带有中间logo图的二维码

              1、qrcode.jar下载的地址是:http://download.csdn.net/download/ilovejavas/6767365            

              2、下面是代码实现:

                    2.1、用来设置二维码一些基本参数的pojo类如下:

package kingpoint.qrcode.entity;

import java.io.Serializable;

public class Entity_Qrcode implements Serializable{

	private static final long serialVersionUID = 1L;

	/**
	 * 内容
	 */
	private String content;
	/**
	 * 二维码图片地址
	 */
	private String qrcodePath;
	/**
	 * logo图片地址
	 */
	private String logoPath;
	/**
	 * 二维码图片的宽度
	 */
	private int width;
	/**
	 * 二维码图片的高度
	 */
	private int height;
	/**
	 * 是否添加logo图片
	 */
	private boolean isFlag;
	/**
	 * 生成图片的格式
	 */
	private String format;
	/**
	 * 纠错级别
	 */
	private char qrcodeErrorCorrect;
	/**
	 * 编码模式
	 */
	private char qrcodeEncodeModel;
	/**
	 * 设置二维码版本
	 */
	private int version;
	
	/** getter and setter */
	
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getQrcodePath() {
		return qrcodePath;
	}
	public void setQrcodePath(String qrcodePath) {
		this.qrcodePath = qrcodePath;
	}
	public String getLogoPath() {
		return logoPath;
	}
	public void setLogoPath(String logoPath) {
		this.logoPath = logoPath;
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public boolean isFlag() {
		return isFlag;
	}
	public void setFlag(boolean isFlag) {
		this.isFlag = isFlag;
	}
	public String getFormat() {
		return format;
	}
	public void setFormat(String format) {
		this.format = format;
	}
	public char getQrcodeErrorCorrect() {
		return qrcodeErrorCorrect;
	}
	public void setQrcodeErrorCorrect(char qrcodeErrorCorrect) {
		this.qrcodeErrorCorrect = qrcodeErrorCorrect;
	}
	public char getQrcodeEncodeModel() {
		return qrcodeEncodeModel;
	}
	public void setQrcodeEncodeModel(char qrcodeEncodeModel) {
		this.qrcodeEncodeModel = qrcodeEncodeModel;
	}
	public int getVersion() {
		return version;
	}
	public void setVersion(int version) {
		this.version = version;
	}
	
}

                2.2、解析二维码时要用到的一个pojo类:

package kingpoint.qrcode.test;

import java.awt.image.BufferedImage;

import jp.sourceforge.qrcode.data.QRCodeImage;

public class CodeImage implements QRCodeImage {
	
	private BufferedImage bufferedImage;

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

	@Override
	public int getPixel(int x, int y) {
		return bufferedImage.getRGB(x, y);
	}

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

}

                  2.3、具体代码实现如下:

package kingpoint.qrcode.test;
/**
 * 下载地址:http://swetake.com/qrcode/java/qr_java.html
 * 总结:用QrCode无法解析Zxing生成的二维码,反过来则可以
 */
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;

import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.exception.DecodingFailedException;
import kingpoint.qrcode.entity.Entity_Qrcode;

import com.swetake.util.Qrcode;

public class QrCode {
	/** 
     * 生成二维码(QRCode)图片 
     * @param qrcode
     */  
    public static void createQRCode(Entity_Qrcode qrcode) {  
        try {  
            Qrcode qrcodeHandler = new Qrcode();  
            /* 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小 */ 
        	qrcodeHandler.setQrcodeErrorCorrect(qrcode.getQrcodeErrorCorrect());
            /* N代表数字,A代表字符a-Z,B代表其他字符 */
            qrcodeHandler.setQrcodeEncodeMode(qrcode.getQrcodeEncodeModel()); 
            /* 设置设置二维码版本,取值范围1-40,值越大尺寸越大,可存储的信息越大 */  
            qrcodeHandler.setQrcodeVersion(qrcode.getVersion());  
  
            byte[] contentBytes = qrcode.getContent().getBytes("UTF-8");
            /*二维码的宽与高*/
            int width = qrcode.getWidth();
            int height = qrcode.getHeight();
            BufferedImage bufImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
            Graphics2D gs = bufImg.createGraphics();  
  
            gs.setBackground(Color.WHITE);  
            gs.clearRect(0, 0, width, height);  
  
            /* 设置图像颜色 */
            gs.setColor(Color.BLACK);  
  
            /* 设置偏移量 不设置可能导致解析出错 */  
            int pixoff = 3;  
            /* 输出内容 > 二维码 */  
            if (contentBytes.length > 0 && contentBytes.length 

 

你可能感兴趣的:(JavaEE)