Detail the method of image compression in Java using the ImageIO class

  • 2020-05-09 18:36:12
  • OfStack

Need to deal with the image compression project to do recently, online to find methods are mostly used com. sun. image. codec. jpeg. * JPEGImageEncoder classes in this package, the introduction of this package after 1 straight error, various google baidu, tried a variety of methods, including in the manual guide jre rt. jar, as well as the access restrictions in eclipse API tips from ERROR WARNING instead, and so on, these are not make, however, Because later I found that there was no rt. jar in my java-7-openjdk-amd64, com. sun. image.*, it seems that this class has been completely remove in java7, at least not in my version. Then I searched for an alternative that USES the ImageIO class for processing. The code is as follows:
Can be compressed to any size, after compression hd, no deformation (white space), you can change the suffix, you can modify the compression resolution.
There may be a friend also have this need, reference 1, there is a problem please point out!


package cn.com.images; 
 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.math.BigDecimal; 
import java.math.MathContext; 
import java.util.ArrayList; 
 
import javax.imageio.ImageIO; 
 
/*** 
 *  Manipulate the image  
 * 
 * @author chenzheng_java 
 * @since 2011/7/29 
 * 
 */ 
public class ImageHelper { 
 
  private static ImageHelper imageHelper = null; 
 
  public static ImageHelper getImageHelper() { 
    if (imageHelper == null) { 
      imageHelper = new ImageHelper(); 
    } 
    return imageHelper; 
  } 
 
  /*** 
   *  Scale the image to the specified scale  
   * 
   * @param sourceImagePath 
   *       Source address  
   * @param destinationPath 
   *       The address of the image after changing the size  
   * @param scale 
   *       Scale, such as 1.2 
   */ 
  public static void scaleImage(String sourceImagePath, 
      String destinationPath, double scale,String format) { 
 
    File file = new File(sourceImagePath); 
    BufferedImage bufferedImage; 
    try { 
      bufferedImage = ImageIO.read(file); 
      int width = bufferedImage.getWidth(); 
      int height = bufferedImage.getHeight(); 
 
      width = parseDoubleToInt(width * scale); 
      height = parseDoubleToInt(height * scale); 
 
      Image image = bufferedImage.getScaledInstance(width, height, 
          Image.SCALE_SMOOTH); 
      BufferedImage outputImage = new BufferedImage(width, height, 
          BufferedImage.TYPE_INT_RGB); 
      Graphics graphics = outputImage.getGraphics(); 
      graphics.drawImage(image, 0, 0, null); 
      graphics.dispose(); 
 
      ImageIO.write(outputImage, format, new File(destinationPath)); 
    } catch (IOException e) { 
      System.out.println("scaleImage An error occurred while compressing the image "); 
      e.printStackTrace(); 
    } 
 
  } 
 
  /*** 
   *  Scale the image to the specified height or width  
   * @param sourceImagePath  Image source address  
   * @param destinationPath  Compress the address of the image  
   * @param width  The width after scaling  
   * @param height  The height after scaling  
   * @param auto  Whether to automatically maintain the original image height to width ratio  
   * @param format  Picture format   For example,  jpg 
   */ 
  public static void scaleImageWithParams(String sourceImagePath, 
      String destinationPath, int width, int height, boolean auto,String format) { 
     
    try { 
    File file = new File(sourceImagePath); 
    BufferedImage bufferedImage = null; 
    bufferedImage = ImageIO.read(file); 
      if (auto) { 
        ArrayList<Integer> paramsArrayList = getAutoWidthAndHeight(bufferedImage,width,height); 
        width = paramsArrayList.get(0); 
        height = paramsArrayList.get(1); 
        System.out.println(" Automatic scaling, width="+width+" height="+height); 
      } 
     
    Image image = bufferedImage.getScaledInstance(width, height, 
        Image.SCALE_DEFAULT); 
    BufferedImage outputImage = new BufferedImage(width, height, 
        BufferedImage.TYPE_INT_RGB); 
    Graphics graphics = outputImage.getGraphics(); 
    graphics.drawImage(image, 0, 0, null); 
    graphics.dispose(); 
    ImageIO.write(outputImage, format, new File(destinationPath)); 
    } catch (Exception e) { 
      System.out.println("scaleImageWithParams An error occurred while compressing the image "); 
      e.printStackTrace(); 
    } 
     
     
  } 
 
  /** 
   *  will double The data of type is converted to int . 4 Give up 5 The principle of  
   * 
   * @param sourceDouble 
   * @return 
   */ 
  private static int parseDoubleToInt(double sourceDouble) { 
    int result = 0; 
    result = (int) sourceDouble; 
    return result; 
  } 
   
  /*** 
   * 
   * @param bufferedImage  The image object to scale  
   * @param width_scale  The width to be scaled to  
   * @param height_scale  The height you want to scale to  
   * @return 1 One set, one 1 The elements are the width, and the 2 Each element is the height  
   */ 
  private static ArrayList<Integer> getAutoWidthAndHeight(BufferedImage bufferedImage,int width_scale,int height_scale){ 
    ArrayList<Integer> arrayList = new ArrayList<Integer>(); 
    int width = bufferedImage.getWidth(); 
    int height = bufferedImage.getHeight(); 
    double scale_w =getDot2Decimal( width_scale,width); 
     
    System.out.println("getAutoWidthAndHeight width="+width + "scale_w="+scale_w); 
    double scale_h = getDot2Decimal(height_scale,height); 
    if (scale_w<scale_h) { 
      arrayList.add(parseDoubleToInt(scale_w*width)); 
      arrayList.add(parseDoubleToInt(scale_w*height)); 
    } 
    else { 
      arrayList.add(parseDoubleToInt(scale_h*width)); 
      arrayList.add(parseDoubleToInt(scale_h*height)); 
    } 
    return arrayList; 
     
  } 
   
   
  /*** 
   *  Return two Numbers a/b After the decimal point 3 A representation of the  
   * @param a 
   * @param b 
   * @return 
   */ 
  public static double getDot2Decimal(int a,int b){ 
     
    BigDecimal bigDecimal_1 = new BigDecimal(a); 
    BigDecimal bigDecimal_2 = new BigDecimal(b); 
    BigDecimal bigDecimal_result = bigDecimal_1.divide(bigDecimal_2,new MathContext(4)); 
    Double double1 = new Double(bigDecimal_result.toString()); 
    System.out.println(" Division of double To: "+double1); 
    return double1; 
  } 
 
} 


Related articles: