package iis.utils;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.Base64;
public class ImageUtil {
/**
* 解密
*
* @param imageString
* @return
*/
public static BufferedImage decodeToImage(String imageString) {
BufferedImage image = null;
byte[] imageByte;
try {
imageByte = Base64.getDecoder().decode(imageString);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
/**
* 加密
*
* @param image
* @param type
* @return
*/
public static String encodeToString(BufferedImage image, String type) {
String imageString = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(image, type, bos);
byte[] imageBytes = bos.toByteArray();
imageString = Base64.getEncoder().encodeToString(imageBytes);
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageString;
}
}
base64加密解密图片(利用java.util包)
最新推荐文章于 2025-11-15 11:49:11 发布
本文介绍了一种使用Base64进行图像编码和解码的方法。通过将图像转换为Base64字符串,可以方便地在网络上传输图像数据。文章详细展示了如何利用Java的Base64工具类和ImageIO来实现这一过程。
1万+

被折叠的 条评论
为什么被折叠?



