QR코드 생성기 및 LOG

5128 단어 이동개발하다
설명 없이 바로 코드 올리기
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();
		}
	}
}

실행에 성공하려면 제3자의 라이브러리를 가져오는 것 외에 그림 경로를 취향에 따라 처리해야 한다

좋은 웹페이지 즐겨찾기