在本教程中,我们向您展示如何使用“ javax.imageio.ImageIO ”加载和写入图像。
加载图像:
BufferedImage originalImage = ImageIO.read(new File("c:\\image\\mypic.jpg"));
写图像:
ImageIO.write(originalImage, "jpg", new File("c:\\image\\mypic_new.jpg"));
例
从“ c:\\image\\mypic.jpg
”加载图像并将其写入位于“ c:\\image\\mypic_new.jpg
”的新图像的完整示例
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageTest {
public static void main(String[] args) {
try {
BufferedImage originalImage = ImageIO.read(new File(
"c:\\image\\mypic.jpg"));
ImageIO.write(originalImage, "jpg", new File(
"c:\\image\\mypic_new.jpg"));
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}