小白学习使用,直接上代码
public class ImagesTest {
//BufferedImage转流
public ByteArrayOutputStream image2Stream(BufferedImage image) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(image, format, out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return out;
}
//BufferedImage转byte[]
public byte[] image2byte(BufferedImage image) {
ByteArrayOutputStream out = image2Stream(image);
byte[] outToByteArray = out.toByteArray();
return outToByteArray;
}
public static void main(String[] args) throws IOException {
File dir =new File("imgs");
BufferedImage image =ImageIO.read(new File(dir,"jietu.png"));
byte[] res = image2byte(image);
OutputStream out = new FileOutputStream(dir+"\\b.png");
out.write(res);
out.flush();
out.close();
}
}
本文介绍了一种使用Java进行图像处理的方法,具体实现将BufferedImage转换为字节流和字节数组。通过代码示例,展示了如何读取文件系统中的图片,将其转换为字节流,并重新保存为新的图片文件。
1192

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



