版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
最近的项目中遇到ImageIO,因此记录下这个类的用法
一、ImageIO:
这个类中的方法都是静态方法,可以用来进行简单的图片IO操作
1、读入的三种方法
public static BufferedImage read(File input)
File file = new File("/Users/xixi/Documents/aaa.png");
BufferedImage bu = ImageIO.read(file);
- 1
- 2
public static BufferedImage read(InputStream input)
public static BufferedImage read(ImageInputStream stream)
2、RenderedImage接口的子类是BufferedImage,因此在这里可以直接出传入BufferedImage的实例化对象,将BufferedImage对象直接写出指定输出流
public static boolean write(RenderedImage im,String formatName,File output)
public static boolean write(RenderedImage im, String formatName,OutputStream output)
public static boolean write(RenderedImage im, String formatName,ImageOutputStream output)
复制图片代码
public class Test {
public static void main(String[] args) {
File out = new File("/Users/wangjue/DownLoads/1.jpg");
//将图片写入ImageIO流
try {
BufferedImage img = ImageIO.read(out);
//将图片写出到指定位置(复制图片)
OutputStream ops = new FileOutputStream(new File("/Users/wangjue/DownLoads/1(1).jpg"));
ImageIO.write(img, "jpg", ops);
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-095d4a0b23.css" rel="stylesheet">
</div>