Java scaling Image

Source: Internet
Author: User

Knowledge Point: The AffineTransform class represents 2D affine transformations. It performs linear ing from 2D coordinates to other 2D coordinates, retaining the line's "Linearity" and "parallelism ". You can use a series of translation, scale, flip, rotation, and shear to construct the affine transformation.
AffineTransformOp uses AffineTransformOp to perform linear ing from 2D coordinates in the source image or Raster to 2D coordinates in the target image or Raster.
BufferedImage
Filter (BufferedImage src, BufferedImage dst)
Convert the source BufferedImage and store the result in the target BufferedImage.
 
 
1. Application 1:
Public void test01 (){
Try {
File fi = new File ("D:/1.jpg"); // large image File
File fo = new File ("D:/2.jpg"); // The small image File to be converted
 
// Create an AffineTransform object
AffineTransform transform = new AffineTransform ();
// Obtain the BufferedImage object from the large image file.
BufferedImage bis = ImageIO. read (fi );
 
Int w = bis. getWidth ();
Int h = bis. getHeight ();
// Double scale = (double) w/h;
 
// Modify the mw value and the zoom size
Int scaling = 120;
 
Int nw = scale;
Int nh = (nw * h)/w;
If (nh> scale ){
Nh = scale;
Nw = (nh * w)/h;
}
 
Double sx = (double) nw/w;
Double sy = (double) nh/h;
 
// Implement scaling. sx scales down the X axis. sy scales down the Y axis.
Transform. setToScale (sx, sy );
 
AffineTransformOp ato = new AffineTransformOp (transform, null );
 
// Recreate a BufferedImage object
BufferedImage bid = new BufferedImage (nw, nh, BufferedImage. TYPE_3BYTE_BGR );
// Execute the conversion
Ato. filter (bis, bid );
// Save the image
ImageIO. write (bid, "jpeg", fo );
} Catch (Exception e ){
E. printStackTrace ();
}
}
 

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.