项目开发需要生成二维码,并使用扫码枪扫描二维码识别;

扫码枪是型号是Honeywell(YJ-HF600),该扫码枪扫描json字符串和英文会有点问题,可能是设置问题,没有细研究,因此二维码生成使用数字加符号','进行分割;

生成二维码代码,如下

public String getUrlByContent(String content) {
        CreatePlistUtil createPlistUtil = new CreatePlistUtil();
        BufferedImage image = createPlistUtil.getQRCODEBufferedImage(content, 250, 250);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        InputStream input = null;
        try {
            ImageIO.write(image, "jpg", os);
            //ByteArrayOutputStream转成InputStream
            input = new ByteArrayInputStream(os.toByteArray());
            //InputStream转成MultipartFile
            MultipartFile file = new MockMultipartFile("file", "qrCode" + ".jpg", "text/plain", input);
            BaseUpDownloader upDownloader = this.factory.get(UploadStoreTypeEnum.MINIO_SYSTEM);
            UploadResponseInfo responseInfo = upDownloader.doUpload(null, null, "temp", "file", false, file);
            return responseInfo.getDownloadUri();
        } catch (Exception e) {
            log.error("生成二维码失败",e);
            throw new MyRuntimeException("生成二维码失败");
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    log.error("生成二维码input.close()报错",e);
                }
            }
        }
    }

制作二维码代码

 public BufferedImage getQRCODEBufferedImage(String content, int width, int height) {
        ZXingCode zp = ZXingCode.getInstance();
        BarcodeFormat barcodeFormat = BarcodeFormat.QR_CODE;
        Map<EncodeHintType, Object> hints = zp.getDecodeHintType();
        BufferedImage image = null;

        try {
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            BitMatrix bm = multiFormatWriter.encode(content, barcodeFormat, width, height, hints);
            int w = bm.getWidth();
            int h = bm.getHeight();
            image = new BufferedImage(w, h, 1);

            for(int x = 0; x < w; ++x) {
                for(int y = 0; y < h; ++y) {
                    image.setRGB(x, y, bm.get(x, y) ? -16777216 : -1);
                }
            }
        } catch (WriterException var14) {
            var14.printStackTrace();
        }

        return image;
    }