Springboot生成二维码,怎么搞?( 二 )

hints)*contents:条形码/二维码内容*format:编码类型 , 如 条形码 , 二维码 等*width:码的宽度*height:码的高度*hints:码内容的编码类型* BarcodeFormat:枚举该程序包已知的条形码格式 , 即创建何种码 , 如 1 维的条形码 , 2 维的二维码 等* BitMatrix:位(比特)矩阵或叫2D矩阵 , 也就是需要的二维码*/MultiFormatWriter multiFormatWriter = new MultiFormatWriter();BitMatrix bitMatrix = multiFormatWriter.encode(codeContent, BarcodeFormat.QR_CODE, CODE_WIDTH, CODE_HEIGHT, hints);/** java.awt.image.BufferedImage:具有图像数据的可访问缓冲图像 , 实现了 RenderedImage 接口* BitMatrix 的 get(int x, int y) 获取比特矩阵内容 , 指定位置有值 , 则返回true , 将其设置为前景色 , 否则设置为背景色* BufferedImage 的 setRGB(int x, int y, int rgb) 方法设置图像像素*x:像素位置的横坐标 , 即列*y:像素位置的纵坐标 , 即行*rgb:像素的值 , 采用 16 进制,如 0xFFFFFF 白色*/BufferedImage bufferedImage = new BufferedImage(CODE_WIDTH, CODE_HEIGHT, BufferedImage.TYPE_INT_BGR);for (int x = 0; x < CODE_WIDTH; x++) {for (int y = 0; y < CODE_HEIGHT; y++) {bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? FRONT_COLOR : BACKGROUND_COLOR);}}return bufferedImage;}/*** 根据本地二维码图片解析二维码内容 注:图片必须是二维码图片 , 但也可以是微信用户二维码名片 , 上面有名称、头像也是可以的)** @param file 本地二维码图片文件,如 E:\\logs\\2.jpg* @return* @throws Exception*/public static String parseQRCodeByFile(File file) {String resultStr = null;if (file == null || file.isDirectory() || !file.exists()) {return resultStr;}try {/** ImageIO的BufferedImage read(URL input)方法用于读取网络图片文件转为内存缓冲图像* 同理还有:read(File input)、read(InputStream input)、、read(ImageInputStream stream)*/BufferedImage bufferedImage = ImageIO.read(file);/** com.google.zxing.client.j2se.BufferedImageLuminanceSource:缓冲图像亮度源* 将 java.awt.image.BufferedImage 转为 zxing 的 缓冲图像亮度源* 关键就是下面这几句:HybridBinarizer 用于读取二维码图像数据 , BinaryBitmap 二进制位图*/BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Hashtable hints = new Hashtable();hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");/** 如果图片不是二维码图片 , 则 decode 抛异常:com.google.zxing.NotFoundException* MultiFormatWriter 的 encode 用于对内容进行编码成 2D 矩阵* MultiFormatReader 的 decode 用于读取二进制位图数据*/Result result = new MultiFormatReader().decode(bitmap, hints);resultStr = result.getText();} catch (IOException e) {e.printStackTrace();} catch (NotFoundException e) {e.printStackTrace();log.error("图片非二维码图片, 路径是: {}!", file.getPath());}return resultStr;}/*** 根据网络二维码图片解析二维码内容, 区别仅仅在于 ImageIO.read(url); 这一个重载的方法)** @param url 二维码图片网络地址 , 如* @return* @throws Exception*/public static String parseQRCodeByUrl(URL url) {String resultStr = null;if (url == null) {return resultStr;}try {/** ImageIO 的 BufferedImage read(URL input) 方法用于读取网络图片文件转为内存缓冲图像* 同理还有:read(File input)、read(InputStream input)、、read(ImageInputStream stream)* 如果图片网络地址错误 , 比如不能访问 , 则 read 抛异常:javax.imageio.IIOException: Can't get input stream from URL!*/BufferedImage bufferedImage = ImageIO.read(url);/** com.google.zxing.client.j2se.BufferedImageLuminanceSource:缓冲图像亮度源* 将 java.awt.image.BufferedImage 转为 zxing 的 缓冲图像亮度源* 关键就是下面这几句:HybridBinarizer 用于读取二维码图像数据 , BinaryBitmap 二进制位图*/BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Hashtable hints = new Hashtable();/** 如果内容包含中文 , 则解码的字符集格式应该和编码时一致*/hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");/** 如果图片不是二维码图片 , 则 decode 抛异常:com.google.zxing.NotFoundException* MultiFormatWriter 的 encode 用于对内容进行编码成 2D 矩阵* MultiFormatReader 的 decode 用于读取二进制位图数据*/Result result = new MultiFormatReader().decode(bitmap, hints);resultStr = result.getText();} catch (IOException e) {e.printStackTrace();log.error("二维码图片地址错误, 地址是: {}!", url);} catch (NotFoundException e) {e.printStackTrace();log.error("图片非二维码图片, 地址是: {}!", url);}return resultStr;}添加Controllerpublic class QRCodeController {@GetMapping("qrCode")public void getQRCode(String codeContent, HttpServletResponse response) {System.out.println("codeContent=" + codeContent);try {/** 调用工具类生成二维码并输出到输出流中*/QRCodeUtil.createCodeToOutputStream(codeContent, response.getOutputStream());log.info("成功生成二维码!");} catch (IOException e) {log.error("发生错误 ,错误信息是:{}!", e.getMessage());}}}