Example of upload image compression in Java

  • 2020-06-15 08:27:08
  • OfStack

I sorted out the documents, searched out 1 Java code for image compression, and slightly sorted out and streamlined 1 for sharing.

First of all, the image format to be compressed cannot be called dynamic image. You can use bmp, png, gif, etc. As for compression quality, it can be specified via BufferedImage.

Place one image pic123.jpg under temp of C disk, and try to find one image with 1 point pixel height. Here I found a 5616*3744 image.


package test; 
import java.io.*; 
import java.util.Date; 
import java.awt.*; 
import java.awt.image.*; 
import javax.imageio.ImageIO; 
import com.sun.image.codec.jpeg.*; 
/** 
 *  Image compression processing  
 * @author  Cui Suqiang  
 */ 
public class ImgCompress { 
  private Image img; 
  private int width; 
  private int height; 
  @SuppressWarnings("deprecation") 
  public static void main(String[] args) throws Exception { 
    System.out.println(" Start: " + new Date().toLocaleString()); 
    ImgCompress imgCom = new ImgCompress("C:\\temp\\pic123.jpg"); 
    imgCom.resizeFix(400, 400); 
    System.out.println(" The end: " + new Date().toLocaleString()); 
  } 
  /** 
   *  The constructor  
   */ 
  public ImgCompress(String fileName) throws IOException { 
    File file = new File(fileName);//  Read the file  
    img = ImageIO.read(file);   //  structure Image object  
    width = img.getWidth(null);  //  Get the source map width  
    height = img.getHeight(null); //  Get the source map length  
  } 
  /** 
   *  Compress by width or by height  
   * @param w int  Maximum width  
   * @param h int  Maximum height  
   */ 
  public void resizeFix(int w, int h) throws IOException { 
    if (width / height > w / h) { 
      resizeByWidth(w); 
    } else { 
      resizeByHeight(h); 
    } 
  } 
  /** 
   *  Take the width as the reference, and scale the picture equally  
   * @param w int  The new width  
   */ 
  public void resizeByWidth(int w) throws IOException { 
    int h = (int) (height * w / width); 
    resize(w, h); 
  } 
  /** 
   *  Take the height as the base and scale the picture equally  
   * @param h int  New height  
   */ 
  public void resizeByHeight(int h) throws IOException { 
    int w = (int) (width * h / height); 
    resize(w, h); 
  } 
  /** 
   *  Mandatory compressed / Enlarge image to fixed size  
   * @param w int  The new width  
   * @param h int  New height  
   */ 
  public void resize(int w, int h) throws IOException { 
    // SCALE_SMOOTH  Thumbnail algorithm of   The smoothness of the thumbnail image is generated   Priority is higher than speed   The resulting images are of better quality   But slowly  
    BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );  
    image.getGraphics().drawImage(img, 0, 0, w, h, null); //  Draw a scaled down diagram  
    File destFile = new File("C:\\temp\\456.jpg"); 
    FileOutputStream out = new FileOutputStream(destFile); //  Output to file stream  
    //  Can be implemented normally bmp , png , gif turn jpg 
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
    encoder.encode(image); // JPEG coding  
    out.close(); 
  } 
} 

After running, generate 1 465.jpg under C disk temp, the pixel size is 600*400, the pixel size is what I specified. That's one or two seconds. Notice that my image is 10M, compressed to 40.5KB.

Refer to the comments in the code for some details.

Note that you may want to test the processing power of larger images. If your JDK does not specify default memory, you may get the following exception because the memory is not large enough:


 Start: 2014-4-14 16:25:11 
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space 
  at java.awt.image.DataBufferByte.<init>(DataBufferByte.java:58) 
  at java.awt.image.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:397) 
  at java.awt.image.Raster.createWritableRaster(Raster.java:938) 
  at javax.imageio.ImageTypeSpecifier.createBufferedImage(ImageTypeSpecifier.java:1169) 
  at javax.imageio.ImageReader.getDestination(ImageReader.java:2879) 
  at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:943) 
  at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:915) 
  at javax.imageio.ImageIO.read(ImageIO.java:1422) 
  at javax.imageio.ImageIO.read(ImageIO.java:1282) 
  at test.ImgCompress.<init>(ImgCompress.java:31) 
  at test.ImgCompress.main(ImgCompress.java:21) 

Solutions:

Choose Window- > Preference- > Installed JREs- > Edit(select jre),

Type -Xms256ES54en-Xmx1024ES56en in Default VM Arguments to indicate the minimum memory 256M and the maximum 1G, and then run it


Related articles: