Recently do projects need picture compression processing, online Find methods mostly used com.sun.image.codec.jpeg.* This package of JPEGImageEncoder class, the introduction of this package has been an error, a variety of Google Baidu, tried a variety of methods, This includes manual rt.jar in the JRE, and changing the access-Restricted API hints from error to warning in eclipse, and so on, but these are not. Because later I found that my java-7-openjdk-amd64 in the Rt.jar inside there is no com.sun.image.*, seemingly this class in Java7 has been completely removed, at least I this version is not. Then search for a use of the ImageIO class to deal with the alternative, the code is as follows:
can be compressed to any size, compressed HD, do not deform (white), you can change the suffix name, you can modify the compression resolution.
may have friends also have this need, refer to it, have a problem also please testify!
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; /*** * to operate the picture * * @author Chenzheng_java * @since 2011/7/29 * */public class Imagehelper {private STA
Tic Imagehelper imagehelper = null;
public static Imagehelper Getimagehelper () {if (Imagehelper = null) {Imagehelper = new imagehelper ();
return imagehelper; /*** * Scale the picture by the specified scale * * @param sourceimagepath * Source Address * @param destinationpath * Change big Small post-image address * @param scale * Zoom ratio, such as 1.2/public static void Scaleimage (String sourceimagepath, Str
ing 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 ("error when Scaleimage method compresses picture");
E.printstacktrace (); /*** * To zoom the picture to the specified height or width * @param sourceimagepath picture Source Address * @param destinationpath the address of the image compressed @p Aram width After zoom * @param height after height * @param auto automatically maintains the original height ratio of the picture * @param format Chart picture format for example JPG * p Ublic static void Scaleimagewithparams (String sourceimagepath, string destinationpath, int width, int height, boolean a
uto,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 adjustment ratio, 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 ("error when Scaleimagewithparams method compresses picture");
E.printstacktrace (); /** * Converts data of double type to int, rounding principle * * @param sourcedouble * @return/privat
e static int parsedoubletoint (double sourcedouble) {int result = 0;
result = (int) sourcedouble;
return result;
/*** * * @param bufferedimage the Picture object to scale @param width_scale the width to be scaled * @param height_scale the height to zoom to * @return A set, the first element is width, the second element is a height/private static arraylist<integer> Getautowidthandheight (Bufferedimag E 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; /*** * Returns two number of A/b decimal point three-digit representation * @param A * @param b * @return/public static double getd
Ot2decimal (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 ("Double as:" +double1);
return double1;
}
}