1、比如說(shuō)二維碼圖片數(shù)據(jù)data,不想落地生成jpg文件,通過(guò)java緩存文件轉(zhuǎn)換base64輸出到頁(yè)面展示,那么java后端處理寫(xiě)法參考如下: 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、反過(guò)來(lái),把頁(yè)面上base64展示的圖片,傳遞到j(luò)ava端解析出來(lái)的寫(xiě)法如下: request.setCharacterEncoding("utf-8"); //設(shè)置編碼 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,)轉(zhuǎn)換base4 原文地址:https://www.cnblogs.com/city-light/p/9528818.html |
|
來(lái)自: jackeyqing > 《學(xué)習(xí)整理360doc》