Java image format conversion of three pieces of code

  • 2020-04-01 04:26:58
  • OfStack

There are not many articles about the content of Java image format on the Internet, and it is not very complete. This site collected three pieces of Java image format conversion code, to share with you:

First paragraph: Java image 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 to PNG
    new FormatConversion().Conversion(JPG,GIF,src);//JPG into GIF
    new FormatConversion().Conversion(JPG,BMP,src);//JPG into BMP
    //The rest of the format Conversion is as long as the Conversion function is called
  }
   
  //InputFormat represents the original format, and 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 image 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";
  //The 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 {
      //Converted to JGP
      ConverterUtil.converter(new File("c:\psb.jpg"),JPG, new File("c:\psb2.jpg"));
      //Convert GIF
      ConverterUtil.converter(new File("c:\psb.jpg"),GIF, new File("c:\psb2.gif"));
      //Converted to PNG
      ConverterUtil.converter(new File("c:\psb.jpg"),PNG, new File("c:\psb2.png"));
      //Converted into 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 be implemented jpg/tif/bmp  Such as the format of the pictures have to be converted to each other  
 */ 
public class Test { 
 
  public static void main(String[] args) throws Exception { 
 
     
    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(); 
    //Specifies the format type, and JPG is of the JPEG type
    ImageEncoder enc2 = ImageCodec.createImageEncoder("JPEG", os2, param2); 
    enc2.encode(src2); 
    os2.close(); 
     
     
     
    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 the stream
     
    //Everything else is converted the same way
 
  } 
 
} 

The above three pieces of code to write the order, does not represent the performance of the code, you can in the learning process, to distinguish the advantages and disadvantages of each code, from the summary of experience, further master

Java image format conversion method.


Related articles: