Java image scaling and java image cropping Tool

Source: Internet
Author: User

Java image scaling and java image cropping Tool

In the image upload function of the system, we cannot control the size of images uploaded by users. Users may upload images that are dozens of MB to 1 kb. On the one hand, the image size occupies too much space, on the other hand, we cannot display images of the same size on the page. Therefore, we need to scale and crop the images uploaded by users. The scaling and common compression here are not a meaning, because small images will be enlarged and large images will be reduced, in addition, the image will not show the effect of extrusion when the proportion is changed. This operation can be fully implemented in java. The following describes the java tool for scaling and cropping images.

I. First, let's look at the effect:

1.jpg is the source image, and yasuo.jpg is the final image. caijian.jpgis obtained by cutting 10 pixels on both sides of yasuo.jpg.

Examples/tlt6wuujuw.vcd4ncjxomibpzd0 = ""> 2. Tool classes:

Import java. awt. color; import java. awt. graphics2D; import java. awt. image; import java. awt. geom. affineTransform; import java. awt. image. affineTransformOp; import java. awt. image. bufferedImage; import java. io. file; import java. io. IOException; import javax. imageio. imageIO; /*** cropping and scaling image tool class * @ author CSDN has no dream-Why? */public class ImgUtils {/*** Image Scaling Method * @ param srcImageFile the image path to be scaled * @ param result: the scaled image path * @ p Aram height Target height pixel * @ param width target width pixel * @ param bb fill white */public final static void scale (String srcImageFile, String result, int height, int width, boolean bb) {try {double ratio = 0.0; // zoom ratio File f = new File (srcImageFile); BufferedImage bi = ImageIO. read (f); Image itemp = bi. getScaledInstance (width, height, bi. SCALE_SMOOTH); // bi. SCALE_SMOOTH: select an Image Scaling Algorithm with a higher priority than the image smoothness. // Calculate the ratio if (bi. getHeight ()> height) | (bi. getWidth ()> width) {double ratioHeight = (new Integer (height )). doubleValue ()/bi. getHeight (); double ratioWhidth = (new Integer (width )). doubleValue ()/bi. getWidth (); if (ratioHeight> ratioWhidth) {ratio = ratioHeight;} else {ratio = ratioWhidth;} AffineTransformOp op = new transform. getScaleInstance (ratio, ratio), null );// Returns the transform itemp = op. filter (bi, null) of the cut transform. // converts the source BufferedImage and stores the result in the target BufferedImage.} If (bb) {// whitelist BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); // construct a BufferedImage of one of the predefined image types. Graphics2D g = image. createGraphics (); // create a Graphics2D, which can be drawn to this BufferedImage. G. setColor (Color. white); // controls the Color g. fillRect (0, 0, width, height); // uses the Graphics2D context settings to fill the internal area of the Shape. If (width = itemp. getWidth (null) g. drawImage (itemp, 0, (height-itemp. getHeight (null)/2, itemp. getWidth (null), itemp. getHeight (null), Color. white, null); else g. drawImage (itemp, (width-itemp. getWidth (null)/2, 0, itemp. getWidth (null), itemp. getHeight (null), Color. white, null); g. dispose (); itemp = image;} ImageIO. write (BufferedImage) itemp, "JPEG", new File (result); // output compressed image} catch (IOException e) {e. printStackTrace ();}} /*** cropping image method ** @ param bufferedImage image source * @ param startX crop start x coordinate * @ param startY crop start y coordinate * @ param endX crop end x coordinate * @ param endY crop end y coordinate * @ return */public static BufferedImage cropImage (BufferedImage bufferedImage, int startX, int startY, int endX, int endY) {int width = bufferedImage. getWidth (); int height = bufferedImage. getHeight (); if (startX =-1) {startX = 0;} if (startY =-1) {startY = 0;} if (endX =-1) {endX = width-1;} if (endY =-1) {endY = height-1;} BufferedImage result = new BufferedImage (endX-startX, endY-startY, 4); for (int x = startX; x <endX; ++ x) {for (int y = startY; y <endY; ++ y) {int rgb = bufferedImage. getRGB (x, y); result. setRGB (x-startX, y-startY, rgb) ;}} return result ;}}
Iii. Test class:
Import java. awt. image. bufferedImage; import java. io. file; import java. io. IOException; import javax. imageio. imageIO; import com. etoak. util. imgUtils; public class Test {public static void main (String [] args) throws IOException {String path = "C:/1.jpg"; // enter an image in the C drive to Test the image 1.jpg ImgUtils. scale ("C:/1.jpg"," C:/yasuo.jpg ", 180,240, true); // proportional scaling output zooming image File newfile = new File (" C: /yasuo.jpg "); BufferedImage bufferedimage = ImageIO. read (newfile); int width = bufferedimage. getWidth (); int height = bufferedimage. getHeight (); // The target image is cropped to a width of 240 and a height of 160 if (width> 240) {/* start x coordinate start y coordinate end x coordinate end y coordinate */bufferedimage = ImgUtils. cropImage (bufferedimage, (int) (width-240)/2), 0, (int) (width-240)/2), (int) (height); if (height> 160) {bufferedimage = ImgUtils. cropImage (bufferedimage, 0, (int) (height-160)/2), 240, (int) (height-160)/2 ));}} else {if (height & gt; 160) {bufferedimage = ImgUtils. cropImage (bufferedimage, 0, (int) (height-160)/2), (int) (width), (int) (height-160) /2);} ImageIO. write (bufferedimage, "jpg", new File ("C:/caijian.jpg"); // output cropping image }}

Related Article

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.