There are not many articles on the content of Java picture format on the Internet, nor are they very complete. The editor has collected three Java picture format conversion codes and shared them with you:
The first paragraph: Java picture format conversion code
import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.Scanner; import javax.imageio.*;public class FormatConversion { public static final String JPG = "jpg"; public static final String GIF = "gif"; public static final String PNG = "png"; public static final String BMP = "bmp"; public static void main(String[] args) { String src = "E://2."; new FormatConversion().Conversion(JPG,PNG,src);//JPG converts to PNG new FormatConversion().Conversion(JPG,GIF,src);//JPG converts to GIF new FormatConversion().Conversion(JPG,BMP,src);//JPG converts to BMP //Converts to other formats as long as you call the Conversion function} //inputFormat represents the original format, outputFormat represents the converted format public void Conversion(String inputFormat,String outputFormat,String src){ try { File input = new File(src+inputFormat); BufferedImage bim = ImageIO.read(input); File output = new File(src+outputFormat); ImageIO.write(bim, outputFormat, output); } catch (IOException e) { e.printStackTrace(); } } }Second paragraph: Java picture format conversion code
import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;public class ConverterUtil { // JGP format public static final String JPG = "jpeg"; // GIF format public static final String GIF = "gif"; // PNG format public static final String PNG = "png"; // BMP format public static final String BMP = "bmp"; public static void converter(File imgFile,String format,File formatFile) throws IOException{ BufferedImage bIMG =ImageIO.read(imgFile); ImageIO.write(bIMG, format, formatFile); } public static void main(String[] args) { try { // Convert to JGP ConverterUtil.converter(new File("c://psb.jpg"),JPG, new File("c://psb2.jpg")); // Convert to GIF ConverterUtil.converter(new File("c://psb.jpg"),GIF, new File("c://psb2.gif")); // Convert to PNG ConverterUtil.converter(new File("c://psb.jpg"),PNG, new File("c://psb2.png")); // Convert to BMP ConverterUtil.converter(new File("c://psb.jpg"),BMP, new File("c://psb2.bmp")); } catch (IOException e) { e.printStackTrace(); } }}Paragraph 3: Java image format conversion code
package cn.xsbiz.servlet.test; import java.io.FileOutputStream; import java.io.OutputStream; import javax.media.jai.JAI; import javax.media.jai.RenderedOp; import com.sun.media.jai.codec.BMPEncodeParam; import com.sun.media.jai.codec.ImageCodec; import com.sun.media.jai.codec.ImageEncoder; import com.sun.media.jai.codec.JPEGEncodeParam; /* * Can convert the formats between jpg/tif/bmp and other images to each other*/ public class Test { public static void main(String[] args) throws Exception { /* tif to jpg format*/ String input2 = "d:/img/a.tif"; String output2 = "d:/img/a.jpg"; RenderedOp src2 = JAI.create("fileload", input2); OutputStream os2 = new FileOutputStream(output2); JPEGEncodeParam param2 = new JPEGEncodeParam(); //Specify the format type, jpg belongs to JPEG type ImageEncoder enc2 = ImageCodec.createImageEncoder("JPEG", os2, param2); enc2.encode(src2); os2.close(); /*tif convert to bmp format*/ String inputFile = "d:/img/b.tif"; String outputFile = "d:/img/b.bmp"; RenderedOp src = JAI.create("fileload", inputFile); OutputStream os = new FileOutputStream(outputFile); BMPEncodeParam param = new BMPEncodeParam(); ImageEncoder enc = ImageCodec.createImageEncoder("BMP", os,param); enc.encode(src); os.close();//Close stream//Convert in other ways in the same way} }The above three codes are written in order not to represent the performance of the code. You can distinguish the pros and cons of each code during the learning process, summarize experience from it, and further master it.
Methods for converting Java image format.