Picture scaling and converting

Source: Internet
Author: User
Tags string format

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);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.