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

扫描二维码显示问号

public static void main(String[] args) {

int width = 300;

int height = 300;

String format = "png";

//String contents = "http://wx.xingxingbaobei.net/wx/product/index";

String contents = "我是你大爷";

//定义二维码的参数

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(contents, BarcodeFormat.QR_CODE, width, height);

Path file = new File("F:/code/img.png").toPath();

MatrixToImageWriter.writeToPath(bitMatrix, format, file);

 

} catch (WriterException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

这样子生成出来的二维码扫描显示的是问号,有多少个中文就有多少问号,这是怎么回事嘞?

正在回答

1 回答

//定义二维码的参数

HashMap hints = new HashMap(); 

//hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

hints.put(EncodeHintType.MARGIN, 2);

try {

contents = new String(contents.getBytes("UTF-8"),"iso-8859-1");

BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height);

Path file = new File("F:/code/img.png").toPath();

MatrixToImageWriter.writeToPath(bitMatrix, format, file);

 

} catch (WriterException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

这样子就可以咯

2 回复 有任何疑惑可以回复我~

举报

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

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

进入课程

扫描二维码显示问号

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号