異常處理部分的錯(cuò)誤
控制臺(tái)內(nèi)容如下:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
?? ?at com.google.zxing.client.j2se.MatrixToImageWriter.writeToPath(MatrixToImageWriter.java:99)
?? ?at java1.QRcode.main(QRcode.java:34)
在指定路徑?jīng)]能生成二維碼
在導(dǎo)入jar包中沒錯(cuò)誤
具體代碼如下:
package java1;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class QRcode {
?? ?public static void main(String[] args)
?? ?{
?? ??? ?int width=300;
?? ??? ?int height=300;
?? ??? ?String format = "png";
?? ??? ?String content = "idcbgp.cn";
?? ??? ?
?? ??? ?//定義二維碼參數(shù)
?? ??? ?HashMap hints= new HashMap();
?? ??? ?hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
?? ??? ?hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
?? ??? ?hints.put(EncodeHintType.MARGIN,2);
?? ??? ?
?? ??? ?try {
?? ??? ??? ?
?? ??? ??? ?BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, width, height,hints);
?? ??? ??? ?
?? ??? ??? ?
?? ??? ??? ?Path file = new File("C:/Users/13208/Pictures/code.png").toPath();
?? ??? ??? ?
?? ??? ??? ?MatrixToImageWriter.writeToPath(bitMatrix, content, file);
?? ??? ??? ?//.writeToPath(bitMatrix, format, filePath);
?? ??? ?} catch (Exception e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?
?? ??? ?
?? ?}
}
2020-02-02
MatrixToImageWriter.writeToPath(bitMatrix, content, file);
第二個(gè)參數(shù)是format就ok啦