标签:落地 str 转换 pos ima width string buffere phi
1、比如说二维码图片数据data,不想落地生成jpg文件,通过java缓存文件转换base64输出到页面展示,那么java后端处理写法参考如下:
InputStream is = new ByteArrayInputStream(data); BufferedImage img = ImageIO.read(is); BufferedImage bi = new BufferedImage(IMG_WIDTH, IMG_WIDTH, BufferedImage.TYPE_INT_RGB); Graphics g = bi.getGraphics(); g.drawImage(img, 0, 0, IMG_WIDTH, IMG_WIDTH, Color.LIGHT_GRAY, null); g.dispose(); is.close(); // bufferImage->base64 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(img, "jpg", outputStream); String base64Img = Base64Util.encode2Str(outputStream.toByteArray()); res = "data:image/jpg;base64," + base64Img.toString(); outputStream.close();
2、反过来,把页面上base64展示的图片,传递到java端解析出来的写法如下:
request.setCharacterEncoding("utf-8"); //设置编码 String s[] = src.split(","); byte[] src1 = Base64.decodeBase64(s[1]); ByteArrayInputStream inputStream = new ByteArrayInputStream(src1); BufferedImage bi = ImageIO.read(inputStream); String str = MatrixUtils.decode(bi);
BufferedImage缓存图片(data:image/jpg;base64,)转换base64输出与解析
标签:落地 str 转换 pos ima width string buffere phi
原文地址:https://www.cnblogs.com/city-light/p/9528818.html