import com.swetake.util.Qrcode;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
* 2 * @Author:
* 3 * @Date: 2020/2/26 上午 10:17
* 4
*/
public class MatrixToImageWriter {
public static void main(String[] args) {
try {
qrcodeCom();
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
}
private static void qrcodeCom() throws IOException {
int height=200; //定義圖片高度
int width=200;
String qrContent="140429199709061617";
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeEncodeMode('B');//N代表數字,A代表a-Z,B代表其它字符
qrcode.setQrcodeErrorCorrect('Q'); // 設置二維碼排錯率,可選L(7%)、M(15%)、Q(25%)、H(30%),排錯率越高可存儲的信息越少,但對二維碼清晰度的要求越小
qrcode.setQrcodeVersion(12);//版本,取值范圍1-40,值越大尺寸越大,可存儲的信息越大
byte[] contentBytes = qrContent.getBytes("utf-8"); //設置編碼
BufferedImage bfimg =new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D grap=bfimg.createGraphics();
grap.setBackground(Color.white);//設置背景色
grap.setColor(Color.black);//設置二維碼顏色
grap.clearRect(0, 0, width, height);//清除下畫板內容
int pixoff = 2;// 設置偏移量,不設置可能導致解析出錯
if (contentBytes.length>0&&contentBytes.length<800) {
boolean[][] codeout=qrcode.calQrcode(contentBytes);//讓字符串生成二維碼
for (int i = 0; i < codeout.length; i++) {
for (int j = 0; j < codeout.length; j++) {
if (codeout[i][j]) {
grap.fillRect(i*3+pixoff, j*3+pixoff, 3, 3);
}
}
}
}
/***帶logo開始***//*
Image img = ImageIO.read(new File("D://logo.png"));// 實例化一個Image對象。
grap.drawImage(img, 70, 70, 60, 60, null);// 70,70是距離grap兩個邊的距離,60,60是中間logo的大小
*//****logo結束******//*
grap.dispose();
bfimg.flush();*/
ImageIO.write(bfimg, "png", new File("D://zhangsan04.png"));
}
}