学习Java6(五) 图片格式的转换
1 import java.awt.image.BufferedImage;
2 import java.io.File;
3 import java.io.IOException;
4 import javax.imageio.ImageIO;
5
6 public class ImageIOTest {
7 public static void main(String args[]) throws IOException {
8 File inputFile = new File("c://test.bmp");
9 BufferedImage input = ImageIO.read(inputFile);
10
11
12 File outputFile = new File("c://test.gif");
13 ImageIO.write(input, "GIF", outputFile);
14
15 outputFile = new File("c://test.jpeg");
16 ImageIO.write(input, "JPEG", outputFile);
17
18 outputFile = new File("c://test.jpg");
19 ImageIO.write(input, "JPG", outputFile);
20
21 outputFile = new File("c://test.png");
22 ImageIO.write(input, "PNG", outputFile);
23 }
24 }
一个512*384 纯黑的BMP格式图片是576KB2 import java.io.File;
3 import java.io.IOException;
4 import javax.imageio.ImageIO;
5
6 public class ImageIOTest {
7 public static void main(String args[]) throws IOException {
8 File inputFile = new File("c://test.bmp");
9 BufferedImage input = ImageIO.read(inputFile);
10
11
12 File outputFile = new File("c://test.gif");
13 ImageIO.write(input, "GIF", outputFile);
14
15 outputFile = new File("c://test.jpeg");
16 ImageIO.write(input, "JPEG", outputFile);
17
18 outputFile = new File("c://test.jpg");
19 ImageIO.write(input, "JPG", outputFile);
20
21 outputFile = new File("c://test.png");
22 ImageIO.write(input, "PNG", outputFile);
23 }
24 }
转换以后:
gif格式为790字节
jpeg格式为3.61KB
jpg格式为3.61KB
png格式为652字节
目前支持一下格式:
BMP, JPG,PNG, JPEG, WBMP, GIF