/**
* BufferedImage对象转bytes数组
*
* @param bImage BufferedImage对象
* @param extName 文件扩展名 jpg png
* @return 图片的bytes数组
* @date 2020/8/3 15:08
*/
public static byte[] imageToBytes(BufferedImage bImage, String extName) {
ByteArrayOutputStream out = null;
try {
out = new ByteArrayOutputStream();
ImageIO.write(bImage, extName, out);
} catch (IOException e) {
ExceptionUtils.wrapException(e);
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
ExceptionUtils.wrapException(e);
}
}
return out.toByteArray();
}
/**
* @param bufferedImage Buffered对象
* @return 图片的byte数组
* @param extName 文件扩展名 jpg png
* BufferedImage 转 byte数组
* @date 2020/8/3 15:45
*/
public static byte[] bytesToImage(BufferedImage bufferedImage, String extName) {
ByteArrayOutputStream out = null;
try {
out = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, extName, out);
} catch (Exception e) {
ExceptionUtils.wrapException(e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
ExceptionUtils.wrapException(e);
}
}
}
return out.toByteArray();
}
byte[] 和BufferedImage互转
最新推荐文章于 2023-07-23 13:51:47 发布