二维码的实现(QRcord.jar)

来源:互联网 发布:数学模型方法与算法 编辑:程序博客网 时间:2024/06/04 08:30
package com.my.test;

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.io.UnsupportedEncodingException;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;

public class QRMyCord {
private String content;
private String imgpath;
public QRMyCord(String content,String imgpath){
this.content=content;
this.imgpath=imgpath;
}
void getImg(){
  Qrcode qrcode=new Qrcode();
          int pixoff = 2; 
qrcode.setQrcodeErrorCorrect('M');
  //设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小  
qrcode.setQrcodeEncodeMode('B');
//N代表数字,A代表字符a-Z,B代表其他字符
qrcode.setQrcodeVersion(8);
// 设置设置二维码版本,取值范围1-40,值越大尺寸越大,可存储的信息越大  
BufferedImage  bufferedImage=new BufferedImage(150,150,BufferedImage.TYPE_INT_RGB);
Graphics2D gr=bufferedImage.createGraphics();//创建画笔
gr.setBackground(Color.WHITE);
gr.clearRect(0, 0, 150, 150);  //通过使用当前绘图表面的背景色进行填充来清除指定的矩形。此操作不使用当前绘图模式
gr.setColor(Color.BLACK); // 将此图形上下文的当前颜色设置为指定颜色。使用此图形上下文的所有后续图形操作均使用这个指定的颜色。
byte[] contentByte = null;
try {
      contentByte=content.getBytes("gb2312");//根据内容编码得到二进制数组
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(contentByte.length>0&&contentByte.length<150)
{
boolean[][] boo=qrcode.calQrcode(contentByte);
for (int i = 0; i < boo.length; i++) {
for (int j = 0; j < boo.length; j++) {
if (boo[j][i]) {
                     gr.fillRect(j * 3+pixoff, i*3+pixoff, 3,3); 
                     //填充指定的矩形。该矩形左边缘和右边缘分别位于 x 和 x + width - 1。上边缘和下边缘分别位于 y 和 y + height - 1。得到的矩形覆盖 width 像素宽乘以 height 像素高的区域。使用图形上下文的当前颜色填充该矩形。                 
                    // x - 要填充矩形的 x 坐标。
                    // y - 要填充矩形的 y 坐标。
                    // width - 要填充矩形的宽度。
                    // height - 要填充矩形的高度。

}
}
  }
}
else{
System.out.println("溢出");
}
//Image img = ImageIO.read(new File(ccbPath));//实例化一个Image对象。
        //gs.drawImage(img, 50, 50, null);
            gr.dispose(); //释放此图形的上下文以及它使用的所有系统资源。调用 dispose 之后,就不能再使用 Graphics 对象
           bufferedImage.flush(); 
            File imgFile = new File(imgpath); 
            // 生成二维码QRCode图片 
            try {
             //该类包含一些用来查找 ImageReader 和 ImageWriter 以及执行简单编码和解码的静态便捷方法。 


ImageIO.write(bufferedImage, "png", imgFile);
//使用支持给定格式的任意 ImageWriter 将一个图像写入 File。如果已经有一个 File 存在,则丢弃其内容。 
     // im - 要写入的 RenderedImage。
//   formatName - 包含格式非正式名称的 String。
//   output - 将在其中写入数据的 File。
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
 * @param args
 */
public static void main(String[] args) {
QRMyCord qrMyCord=new QRMyCord("www.baidu.com","c://1234.png");
qrMyCord.getImg();
}

}