遇到了一个需要java生成二维码,支持扫码枪扫描的需求
1,需求是生产二维码,加入到正文和表单中
2,首先能生成二维码,其次就是把二维码插入到表单和正文了,插入到表单设计到一些前端知识暂不分享
3,把二维码插入正文,正文是用的word文档,后续分享如何操作
生成二维码的样式应该是pdf417这种码,而不是qrcode普通的二维码

# 需要引入依赖jar包
<!-- https://mvnrepository.com/artifact/com.lowagie/itext --> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency>
直接生成到本地
// 创建pdf417二维码 text是需要生成的内容 path 是本地存储的路径 public static void createPdf417ForOutDoc(String text, String path) { BarcodePDF417 pdf = new BarcodePDF417(); OutputStream os = null; try { byte[] buff = text.getBytes(CHARSET); pdf.setText(buff); // 9列数据列最长字节数为504,大于504字节数的字符串不限制长度,不然扫不出来 if (buff.length <= 504) { //下面两行很重要,用于定宽 固定中间的列数为10列 pdf.setOptions(BarcodePDF417.PDF417_FIXED_COLUMNS); //设置codeColumns,主要用于指定生成二维码图片的长度,公文二维码的长度一般是189px,所以codeColumns = 7 pdf.setCodeColumns(7); // 设置宽窄比例 pdf.setYHeight(3F); } Image pdfImg = pdf.createAwtImage(Color.black, Color.white); // 缩放会失真,所以不用 Image pdfImgScal = pdfImg.getScaledInstance(189, 46, Image.SCALE_REPLICATE); BufferedImage img = new BufferedImage(pdfImg.getWidth(null), 45, BufferedImage.TYPE_INT_RGB); Graphics graphics = img.getGraphics(); graphics.drawImage(pdfImg, 0, 0, img.getWidth(null), img.getHeight(null), Color.white, null); os = new BufferedOutputStream(new FileOutputStream(path)); ImageIO.write(img, "PNG", os); } catch (IOException e) { e.printStackTrace(); } finally { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } }
#把生产的二维码直接转成base64码
/** * 生成base64图片 * @param 图片内容 * @return */ public static String base64img(String Str) { // 返回生成内容 String str = ""; // 创建二维码 BarcodePDF417 pdf = new BarcodePDF417(); ByteArrayOutputStream outputStream = null; String strImg = ""; try { byte[] buff = str.getBytes(CHARSET); pdf.setText(buff); // 9列数据列最长字节数为504,大于504字节数的字符串不限制长度,不然扫不出来 if (buff.length <= 504) { //下面两行很重要,用于定宽 固定中间的列数为10列 pdf.setOptions(BarcodePDF417.PDF417_FIXED_COLUMNS); //设置codeColumns,主要用于指定生成二维码图片的长度,公文二维码的长度一般是189px,所以codeColumns = 7 pdf.setCodeColumns(7); // 设置宽窄比例 pdf.setYHeight(3F); } Image pdfImg = pdf.createAwtImage(Color.black, Color.white); // 缩放会失真,所以不用 Image pdfImgScal = pdfImg.getScaledInstance(189, 46, Image.SCALE_REPLICATE); BufferedImage img = new BufferedImage(pdfImg.getWidth(null), 45, BufferedImage.TYPE_INT_RGB); Graphics graphics = img.getGraphics(); graphics.drawImage(pdfImg, 0, 0, img.getWidth(null), img.getHeight(null), Color.white, null); // os = new BufferedOutputStream(new FileOutputStream(path)); outputStream = new ByteArrayOutputStream(); ImageIO.write(img, "PNG", outputStream); strImg = Base64.getEncoder().encodeToString(outputStream.toByteArray()).replace("\r\n", "").trim(); } catch (IOException e) { e.printStackTrace(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return strImg; }
#生成inputStream流的二维码
/** * 生成二维码,并且上minio * @param barCodeParam */ public static InputStream createBarCodeUploadMinio(String Str) { // 返回生成内容 String str = ""; // 创建二维码 BarcodePDF417 pdf = new BarcodePDF417(); // OutputStream os = null; ByteArrayOutputStream outputStream = null; InputStream byteInputStream = null; try { byte[] buff = str.getBytes(CHARSET); pdf.setText(buff); // 9列数据列最长字节数为504,大于504字节数的字符串不限制长度,不然扫不出来 if (buff.length <= 504) { //下面两行很重要,用于定宽 固定中间的列数为10列 pdf.setOptions(BarcodePDF417.PDF417_FIXED_COLUMNS); //设置codeColumns,主要用于指定生成二维码图片的长度,公文二维码的长度一般是189px,所以codeColumns = 7 pdf.setCodeColumns(7); // 设置宽窄比例 pdf.setYHeight(3F); } Image pdfImg = pdf.createAwtImage(Color.black, Color.white); // 缩放会失真,所以不用 Image pdfImgScal = pdfImg.getScaledInstance(189, 46, Image.SCALE_REPLICATE); BufferedImage img = new BufferedImage(pdfImg.getWidth(null), 45, BufferedImage.TYPE_INT_RGB); Graphics graphics = img.getGraphics(); graphics.drawImage(pdfImg, 0, 0, img.getWidth(null), img.getHeight(null), Color.white, null); // os = new BufferedOutputStream(new FileOutputStream(path)); outputStream = new ByteArrayOutputStream(); ImageIO.write(img, "PNG", outputStream); String s = Base64.getEncoder().encodeToString(outputStream.toByteArray()); byteInputStream = new ByteInputStream(outputStream.toByteArray(),outputStream.size()); } catch (IOException e) { e.printStackTrace(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return byteInputStream; }

浙公网安备 33010602011771号