Java 图片格式转换


package img;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;

public class BmptoTJpeg {

	public static void main(String[] args) throws Exception {
		
		FileImageInputStream fiis = new FileImageInputStream(new File(
				"c:/4.jpg"));
		FileImageOutputStream fios = new FileImageOutputStream(new File(
				"c:/4.BMP"));

		ImageReader jpegReader = null;
		Iterator<ImageReader> it1 = ImageIO.getImageReadersByFormatName("jpeg");
		if (it1.hasNext()) {
			jpegReader = it1.next();
		}
		jpegReader.setInput(fiis);

		ImageWriter bmpWriter = null;
		Iterator<ImageWriter> it2 = ImageIO.getImageWritersByFormatName("bmp");
		if (it2.hasNext()) {
			bmpWriter = it2.next();
		}
		bmpWriter.setOutput(fios);
		BufferedImage br = jpegReader.read(0);
		bmpWriter.write(br);
		fiis.close();
		fios.close();
		System.out.println("Jpeg到bmp图片转换完成.");
	}

}


你可能感兴趣的:(java)