jpg转bmp

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
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 Test {

	public static void main(String[] args) throws FileNotFoundException, IOException {
		FileImageInputStream fiis = new FileImageInputStream(new File(
				"c:/img/4.bmp"));
		FileImageOutputStream fios = new FileImageOutputStream(new File(
				"c:/img/4.jpeg"));

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

		ImageWriter bmpWriter = null;
		Iterator<ImageWriter> it2 = ImageIO.getImageWritersByFormatName("jpeg");
		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图片转换完成.");
	}

}

你可能感兴趣的:(jpg)