为了账号安全,请及时绑定邮箱和手机立即绑定

异常处理部分的错误

控制台内容如下:

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();
        }
        
        
    }
}

http://img1.sycdn.imooc.com//5e1688390001341b19201039.jpg

正在回答

1 回答

MatrixToImageWriter.writeToPath(bitMatrix, content, file);

第二个参数是format就ok啦

1 回复 有任何疑惑可以回复我~
#1

慕容6438698

可以教教我吗,求求了
2021-11-28 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java生成二维码
  • 参与学习       84388    人
  • 解答问题       203    个

二维码无处不在,自己动手用Java生成二维码,三种生成方式任你选

进入课程

异常处理部分的错误

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信