Recently, I need image compression processing to do projects. Most of the methods I searched for online use the JPEGImageEncoder class in the package com.sun.image.codec.jpeg.*. After introducing this package, I have reported errors. Various Google Baidu have tried various methods, including manually rt.jar in jre, and changing access-restricted API prompts from ERROR to WARNING in eclipse, etc. However, these are not good, because later I found that there is no com.sun.image.* in rt.jar in java-7-openjdk-amd64. It seems that this class has been completely removed in java7, at least I have no version. Then I searched for an alternative solution to use the ImageIO class for processing, the code is as follows:
It can be compressed to any size, high definition after compression, without deformation (white space), the suffix name can be changed, and the compression resolution can be modified.
Maybe some friends also need this. Please refer to it. If you have any questions, please give me some evidence!
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; /*** * Operation on images* * @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 at the specified scale* * @param sourceImagePath * Source address* @param destinationPath * 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("The error occurred while compressing the image"); e.printStackTrace(); } } /*** * Scale the image to the specified height or width* @param sourceImagePath Image source address* @param destinationPath Address of the image after compressing the image* @param width Scale width* @param height Scale height* @param auto Whether to automatically maintain the original height and width ratio of the image* @param format Picture format such as 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("Auto-adjust the scale, width="+width+"scaleImageWithParams method errored when compressing images"); e.printStackTrace(); } } /** * Convert double type data to int, rounding principle* * @param sourceDouble * @return */ private static int parseDoubleToInt(double sourceDouble) { int result = 0; result = (int) sourceDouble; return result; } /*** * * @param bufferedImage The image object to be scaled* @param width_scale Width to scale to * @param height_scale Height to scale to scale to * @return A collection, the first element is width and the second element is 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("getAutoWidthAndHeightscale_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 the representation of the three decimal points of the two numbers a/b* @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("The double after division is: "+double1); return double1; } }