带LOG的二维码生成器

不加描述了,直接上代码

Property_Qrcode.java

package leo;

import java.io.Serializable;

public class Property_Qrcode implements Serializable {
	private static final long serialVersionUID = 1L;  
	//要转换成二维码的内容
	private String content;
	//二维码图片地址
	private String qrcodePath;
	//LOG图片路径
	private String logoPath;
	//生成图片的宽度
	private int width;
	//高度
	private int height;
	//是否添加LOG图片
	private boolean isFiag; 
	//生成图片格式
	private String format;
	//二维码纠错级别
	private char qrcodeErrorCorrect;
	//二维码编码方式
	private char qrcodeEncodeModel;
	//二维码版本
	private int version;
	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 isFiag() {
		return isFiag;
	}
	public void setFiag(boolean isFiag) {
		this.isFiag = isFiag;
	}
	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;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	
}

QrCode.java

package leo;

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 com.swetake.util.Qrcode;

public class QrCode {
	public static void main(String[] args) {
		String content = "谭护士LOVe西子";
		Property_Qrcode qrcode = new Property_Qrcode();
		qrcode.setContent(content);
		//设置生成的二维码图片保存的路径
		qrcode.setQrcodePath("F:/new_example/QrcodeImage/");
		//LOG路径
		qrcode.setLogoPath("F:/new_example/QrcodeImage/DSC05666.jpg");
		qrcode.setFormat("png");
		//设置是否带LOG true表示带log
		qrcode.setFiag(true);
		qrcode.setQrcodeEncodeModel('M');
		qrcode.setWidth(140);
		qrcode.setHeight(140);
		qrcode.setVersion(7);
		qrcode.setQrcodeEncodeModel('B');
		QrCode.createQRCode(qrcode);
		System.out.println("encode qrcode success!");
	}

	private static void createQRCode(Property_Qrcode qrcode) {
		// TODO Auto-generated method stub
		try {
		Qrcode handler = new Qrcode();
		handler.setQrcodeEncodeMode(qrcode.getQrcodeEncodeModel());
		handler.setQrcodeErrorCorrect(qrcode.getQrcodeErrorCorrect());
		handler.setQrcodeVersion(qrcode.getVersion());
		
		byte[] contentBytes = qrcode.getContent().getBytes("gb2312");
		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;
		System.out.println("contentBytes.length  = "+contentBytes.length );
		if (contentBytes.length > 0 && contentBytes.length < width ) {
			boolean[][] codeOut = handler.calQrcode(contentBytes);
			for (int i = 0; i < codeOut.length; i++) {
				for (int j = 0; j < codeOut.length; j++) {
					if (codeOut[i][j]) {
						gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
					}
				}
			}
		} else {
			System.out.println("QRCode content byte length "
					+ contentBytes.length + " not in 200");
		}
		
		if(qrcode.isFiag()){
			int width_4 = width / 4;
			int width_8 = width_4 /2;
			int height_4 = height /4;
			int height_8 = height_4 / 2;
			
			Image img = ImageIO.read(new File(qrcode.getLogoPath()));
			gs.drawImage(img, width_4 + width_8, height_4 + height_8,width_4,height_4,null);
			
			gs.dispose();
			bufImg.flush();
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName = sdf.format(new Date())+"."+qrcode.getFormat();
		System.out.println(fileName);
		String url = qrcode.getQrcodePath() + fileName;
		File imgFile = new File(url);
		if(!imgFile.exists()){
			imgFile.mkdirs();
		}
		
			ImageIO.write(bufImg, qrcode.getFormat(), imgFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

需要运行成功的话,除了要导入第三方的库之外,还要把图片路径按你的喜好来处理一下

你可能感兴趣的:(移动,开发)