控制台内容如下:
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)
在指定路径没能生成二维码
在导入jar包中没错误
具体代码如下:
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 = "www.imooc.com";
//定义二维码参数
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();
}
}
}
MatrixToImageWriter.writeToPath(bitMatrix, content, file);
第二个参数是format就ok啦