java 实现base64图片转BufferedImage
时间: 2023-05-23 08:05:36 浏览: 1503
A:
可以使用Java的Base64类将Base64编码的图片字符串转换为字节数组,然后使用ImageIO类将字节数组转换为BufferedImage。以下是示例代码:
```
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Base64;
import javax.imageio.ImageIO;
public class Base64ToBufferedImage {
public static BufferedImage convert(String base64Image) throws IOException {
String[] parts = base64Image.split(",");
String imageString = parts[1];
byte[] decodedBytes = Base64.getDecoder().decode(imageString);
ByteArrayInputStream bis = new ByteArrayInputStream(decodedBytes);
BufferedImage image = ImageIO.read(bis);
bis.close();
return image;
}
}
```
使用方法:
```
String base64Image = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
BufferedImage image = Base64ToBufferedImage.convert(base64Image);
```
阅读全文