沒有報紅,運行時出錯
? int w = 300;
? ? ? ?int h = 300;
? ? ? ?String formt = ".png";
? ? ? ?String content = "www.baidu.com";
// ? ? ? ?定義二維碼參數(shù)
? ? ? ?HashMap hints = new HashMap();
? ? ? ?hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//字符集
? ? ? ?hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//出錯等級
? ? ? ?hints.put(EncodeHintType.MARGIN, 2);//邊距,生成的二維碼的空白部分,默認是5
// ? ? ? ?生成
? ? ? ?try {
? ? ? ? ? ?BitMatrix encode = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, w, h, hints);
// ? ? ? ? ? ?生成二維碼
? ? ? ? ? ?Path file = new File("G:/img.png").toPath();
? ? ? ? ? ?MatrixToImageWriter.writeToPath(encode,formt,file);
? ? ? ?} catch (Exception e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}
運行后:
java.io.IOException: Could not write an image of format .png to G:\img.png
2018-03-16
已經(jīng)解決了,定義類型的時候多寫了個.