二維碼和背景尺寸不搭 謝謝大家
package QrCode;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
我照著老師的代碼寫的,為什么老師的格式對而我的不好看
大家幫忙看一下 代碼哪里不對? 謝謝大家 代碼在下面
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
public class CreatQrCode {
public static void main(String[] args) throws IOException {
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeEncodeMode('B'); //N代表字符? A代表a-z? B代表其他字符
qrcode.setQrcodeErrorCorrect('M');//糾錯等級? L M Q H
qrcode.setQrcodeVersion('7'); //版本號
String qrData ="www.xianlaiwan.cn";
int width = 67 + 12 * (7 - 1); //67 + 12 * (版本號 -1) 設置的是白框的背景大小
int height = 67 + 12 * (7 - 1);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //將圖片加載到內存中
//Graphics2D是Graphics的拓展類,用于在Java平臺上展示二維形狀,文本和圖像的基礎類。
Graphics2D gs = bufferedImage.createGraphics();
gs.setBackground(Color.WHITE); //背景顏色設置
gs.setColor(Color.BLACK); //
gs.clearRect(0, 0, width, height);
int pixoff = 2; //偏移量? 二維碼相對于背景的位置
byte[] b = qrData.getBytes("gb2312"); //將需要展示的內容轉變成字節格式?
if(b.length>0 && b.length<120) {
boolean[][] s = qrcode.calQrcode(b);
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < s.length; j++) {
if(s[i][j]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3); //設置的是二維碼的大小
}
}
}
}
gs.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File("D:/code/qrcode.png"));
System.out.println("111111111");
}
}
2019-05-10
你把版本號的單引號去了試試