问答详情
源自:6-3 使用zxing进行二维码解析

扫描二维码显示问号

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

}

}

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

提问者:_迷醉 2018-07-16 10:13

个回答

  • _迷醉
    2018-07-16 10:23:17

    //定义二维码的参数

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

    }

    这样子就可以咯