當前位置:編程學習大全網 - 源碼下載 - java 生產二維碼報錯

java 生產二維碼報錯

妳好,妳可以參考我的這段代碼,記得導入Zxing1.6.jar即可:

public static void main(String[] args) {

String myCodeText = "";

String filePath = "d:/testqr/myQR.png";

int size = 125;

String fileType = "png";

File myFile = new File(filePath);

try {

Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();

hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

QRCodeWriter qrCodeWriter = new QRCodeWriter();

BitMatrix byteMatrix = qrCodeWriter.encode(myCodeText,BarcodeFormat.QR_CODE, size, size, hintMap);

int CrunchifyWidth = byteMatrix.getWidth();

BufferedImage image = new BufferedImage(CrunchifyWidth, CrunchifyWidth,

BufferedImage.TYPE_INT_RGB);

image.createGraphics();

Graphics2D graphics = (Graphics2D) image.getGraphics();

graphics.setColor(Color.WHITE);

graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth);

graphics.setColor(Color.BLACK);

for (int i = 0; i < CrunchifyWidth; i++) {

for (int j = 0; j < CrunchifyWidth; j++) {

if (byteMatrix.get(i, j)) {

graphics.fillRect(i, j, 1, 1);

}

}

}

ImageIO.write(image, fileType, myFile);

} catch (WriterException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

System.out.println("\n\nYou have successfully created QR Code.");

}

記得采納哦

  • 上一篇:王者榮耀魯班大師和楊戩誰厲害呢?
  • 下一篇:外星能源代碼
  • copyright 2024編程學習大全網