package pic_control;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
public class QR_write {
/**
* @param args
*/
String imagepath=null;
int output_size=0;
Qrcode qrcode=null;
public void setQrcode(int QRcode_size,char QrcodeErrorCorrect,char QrcodeEncodeMode){
output_size=67+12*(QRcode_size-1);
qrcode=new Qrcode();
qrcode.setQrcodeErrorCorrect(QrcodeErrorCorrect);//设置二维码错误修正容量(L,M,Q,H)
qrcode.setQrcodeEncodeMode(QrcodeEncodeMode);
qrcode.setQrcodeVersion(QRcode_size);
}
public void QRwrite(byte[] d){
BufferedImage bi = new BufferedImage(output_size, output_size, BufferedImage.TYPE_INT_RGB);
// createGraphics
Graphics2D g = bi.createGraphics();
// set background
g.setBackground(Color.WHITE);
g.clearRect(0, 0, output_size, output_size);
g.setColor(Color.BLACK);
boolean[][] b = qrcode.calQrcode(d);
for (int i=0;i<b.length;i++){
for (int j=0;j<b.length;j++){
if (b[j][i]) {
g.fillRect(j*3+2,i*3+2,3,3);
}
}
}
g.dispose();
bi.flush();
File filepath = new File(imagepath);
try {
ImageIO.write(bi, "jpg", filepath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); //IOException
}
System.out.println("doned!");
}
public void setimagepath(String _imagepath){
this.imagepath=_imagepath;
}
//测试程序
//********************************************************************//
byte[] getbyte(){
byte[] buff = null;
try {
File file=new File("C:\\Users\\Administrator\\Desktop\\small.jpg");//input
BufferedInputStream bis=new BufferedInputStream(
new FileInputStream(file));
/*****/ System.out.println("file.length"+file.length());
buff=new byte[(int) file.length()];
try {
while(bis.read(buff)!=-1){
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//文件输入
System.out.println("buff.length"+buff.length);
return buff;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int QRcode_size=29;
char QrcodeErrorCorrect='L';
char QrcodeEncodeMode='B';
String FilePath="C:\\Users\\Administrator\\Desktop\\finish.wmf";//output
//
QR_write qr=new QR_write();
qr.setQrcode(QRcode_size, QrcodeErrorCorrect, QrcodeEncodeMode);
qr.setimagepath(FilePath);
qr.QRwrite(qr.getbyte());
}
}