java 将base64转换成BufferedImage,将代码写出来
时间: 2024-05-12 08:16:11 浏览: 71
我可以帮您提供 Java 代码将 base64 转换为 BufferedImage:
```
import java.awt.image.BufferedImage;
import java.util.Base64;
import javax.imageio.ImageIO;
public class Base64ToBufferedImage {
public static BufferedImage convert(String base64String) {
BufferedImage image = null;
try {
byte[] imageBytes = Base64.getDecoder().decode(base64String);
image = ImageIO.read(new ByteArrayInputStream(imageBytes));
} catch (IOException e) {
System.out.println("Error occurred while converting base64 to BufferedImage: " + e.getMessage());
}
return image;
}
}
```
使用方法:
```
String base64String = "iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAIAAABvYTE8AAAAAXNSR0IArs4c6QAAAARzQklUCAgI" + // 例子 Base64 字符串
"YWPJALi/AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAxQTFRF////AAAA" +
"tpMq3gAAABV0Uk5T////////////////////////////AP//////////////////////////////////////////////////////////////ADiGhanaAAAAlwSFlzAAALEwAACxMBAJqcGAAAABZ0RVh0U29mdHdhcmUATWFjcm9tZWRpYSBGaXJld29ya3Mg"
BufferedImage image = Base64ToBufferedImage.convert(base64String);
```
阅读全文