_迷醉
2018-07-16 10:13
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();
}
}
这样子生成出来的二维码扫描显示的是问号,有多少个中文就有多少问号,这是怎么回事嘞?
//定义二维码的参数
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();
}
这样子就可以咯
Java生成二维码
84564 学习 · 224 问题
相似问题