二維碼和背景尺寸不搭 謝謝大家
package QrCode;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
我照著老師的代碼寫(xiě)的,為什么老師的格式對(duì)而我的不好看
大家?guī)兔匆幌?代碼哪里不對(duì)? 謝謝大家 代碼在下面
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');//糾錯(cuò)等級(jí)? L M Q H
qrcode.setQrcodeVersion('7'); //版本號(hào)
String qrData ="idcbgp.cn";
int width = 67 + 12 * (7 - 1); //67 + 12 * (版本號(hào) -1) 設(shè)置的是白框的背景大小
int height = 67 + 12 * (7 - 1);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //將圖片加載到內(nèi)存中
//Graphics2D是Graphics的拓展類(lèi),用于在Java平臺(tái)上展示二維形狀,文本和圖像的基礎(chǔ)類(lèi)。
Graphics2D gs = bufferedImage.createGraphics();
gs.setBackground(Color.WHITE); //背景顏色設(shè)置
gs.setColor(Color.BLACK); //
gs.clearRect(0, 0, width, height);
int pixoff = 2; //偏移量? 二維碼相對(duì)于背景的位置
byte[] b = qrData.getBytes("gb2312"); //將需要展示的內(nèi)容轉(zhuǎn)變成字節(jié)格式?
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); //設(shè)置的是二維碼的大小
}
}
}
}
gs.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File("D:/code/qrcode.png"));
System.out.println("111111111");
}
}
2019-05-10
你把版本號(hào)的單引號(hào)去了試試