By redrawing the picture to achieve the image scaling, compression coding conversion function.
Java code
Import Java.awt.Image;
Import Java.awt.image.BufferedImage;
Import Java.awt.image.RenderedImage;
Import Java.io.File;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Javax.imageio.ImageIO;
/**
*
* @author Liangdong
* @version 1.0
* @since 1.0
*/
Public abstract class Imageutils {
/**
* Zoom Picture
*
* @param width
* Output width
* @param height
* Output Height
* @param input
* Input Stream
* @param output
* Output stream
* @param format
* Output format
* @return
* @throws Exception
*/
public static Boolean convert (int width, int height, inputstream input,
OutputStream output, String format) throws Exception {
Input
BufferedImage inputimage = imageio.read (input);
Transformation
RenderedImage im = (renderedimage) convert (height, height, inputimage);
Output
return Imageio.write (IM, format, output);
}
/**
* Conversion compression algorithm
*
* @param input
* Input File
* @param output
* Output File
* @return
* @throws Exception
*/
public static Boolean convert (file input, file output) throws Exception {
Input
BufferedImage inputimage = imageio.read (input);
Transformation
int width = inputimage.getwidth ();
int height = inputimage.getheight ();
RenderedImage im = (renderedimage) convert (width, height, inputimage);
String outputfilename = Output.getname ();
String format = outputfilename.substring (outputfilename
. LastIndexOf ('. ') + 1);
Output
return Imageio.write (IM, format, output);
}
/**
* Zoom Picture
*
* @param width
* Output width
* @param height
* Output Height
* @param input
* Input File
* @param output
* Output File
* @return
* @throws Exception
*/
public static Boolean convert (int width, int height, file input, file output)
Throws Exception {
Input
BufferedImage inputimage = imageio.read (input);
Transformation
RenderedImage im = (renderedimage) convert (width, height, inputimage);
String outputfilename = Output.getname ();
String format = outputfilename.substring (outputfilename
. LastIndexOf ('. ') + 1);
Output
return Imageio.write (IM, format, output);
}
/**
* Zoom Picture
*
* @param width
* Output width
* @param height
* Output Height
* @param input
* Input Path
* @param output
* Output Path
* @return
* @throws Exception
*/
public static Boolean convert (int width, int height, String inputpath,
String OutputPath) throws Exception {
Return convert (width, height, new file (InputPath), new file (OutputPath));
}
/**
* Conversion
*
* @param width
* Output width
* @param height
* Output Height
* @param input
* BufferedImage
* @return BufferedImage
* @throws Exception
*/
private static BufferedImage convert (int width, int height,
BufferedImage input) throws Exception {
Initialize output picture
BufferedImage output = new BufferedImage (width, height,
BUFFEREDIMAGE.TYPE_INT_RGB);
Re-drawing
Image image = Input.getscaledinstance (Output.getwidth (), Output
. GetHeight (), Output.gettype ());
Output.creategraphics (). DrawImage (image, NULL, NULL);
return output;
}
/**
* Scaling picture with equal ratio
*
* @param width
* Output width
* @param height
* Output Height
* @param input
* Input Stream
* @param output
* Output stream
* @return
* @throws Exception
*/
public static Boolean Equimultipleconvert (int width, int height,
string input, string output) throws Exception {
Return Equimultipleconvert (width, height, new file (input), new file (
Output));
}
/**
* Scaling picture with equal ratio
*
* @param width
* Output width
* @param height
* Output Height
* @param input
* Input Stream
* @param output
* Output stream
* @return
*
* @throws Exception
*/
public static Boolean Equimultipleconvert (int width, int height,
File input, file output) throws Exception {
Input
BufferedImage image = Imageio.read (input);
Re-accounting dimensions
if (image.getwidth () > 0 && image.getheight () > 0) {
if ((Image.getwidth ()/Image.getheight ()) >= (width/height)) {
if (Image.getwidth () > width) {
Height = (image.getheight () * width)/image.getwidth ();
} else {
width = Image.getwidth ();
Height = image.getheight ();
}
} else {
if (Image.getheight () > height) {
width = (image.getwidth () * height)/image.getheight ();
} else {
width = Image.getwidth ();
Height = image.getheight ();
}
}
}
Transformation output
Return convert (width, height, input, output);
}
}