Thumbnailator to achieve image compression, rotation, add watermark

Source: Internet
Author: User

Let's look at the effect chart first:



The main code is as follows:

Package sea;
Import Net.coobird.thumbnailator.Thumbnails;

Import net.coobird.thumbnailator.geometry.Positions;
Import Javax.imageio.ImageIO;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import java.util.ArrayList;

Import java.util.List; /** * User:Greta.Wang * date:12-11-3 * Read Nine pictures from file system, and compose them to one, in the process,
 Each of the picture would be added a watermark. */public class Composepicture {public static void main (String args[]) throws IOException {//From Project To read nine pictures, and to do further processing of the picture (reduce the proportion, rotate 30 degrees, add watermark), and save the picture in memory for further processing list<bufferedimage> bufferedimagelist = Readpicturesto
        Memory ();
        In memory, merge nine pictures into a picture bufferedimage Bufferedimageninebynine = Composeninepicturestoone (bufferedimagelist);
        Writes the picture in memory to the specified file Fileoutput = new file (System.getproperty ("User.dir") + "/handledpictures/compose.jpg"); Imageio.write (bufferedimageninebynine, "JPG",Fileoutput); public static list<bufferedimage> Readpicturestomemory () throws IOException {string[] Pat
        Harray = new String[9];
        list<bufferedimage> bufferedimagelist = new arraylist<bufferedimage> (); Thumbnail reads the watermark file into memory bufferedimage watermarkbufferedimage = Thumbnails.of (ComposePicture.class.getClassLoader (). g
                Etresource ("5.png"). GetFile ())//thumbnail method, according to the proportional reduction, 0.4f meaning is reduced to the original image of 40%. Scale (0.4f)
        Read as a BufferedImage object. Asbufferedimage (); Read 9 pictures to be processed into memory for (int i = 0; i < 9; i++) {//Get the path of the picture Patharray[i] = composepicture. Class.getclassloader (). GetResource ("img_000" + i +).
            JPG "). GetFile ();
                    BufferedImage image = Thumbnails.of (Patharray[i])//Scaling down. Scale (0.05f) Watermark, 0.5f means transparency. Watermark (Positions.center, WatermarkbuffereDIMAGE, 0.5f)//Rotate 30 degrees. Rotate (30)//read as BufferedImage object
            . Asbufferedimage ();
        Bufferedimagelist.add (image);
    return bufferedimagelist; public static BufferedImage Composeninepicturestoone (list<bufferedimage> bufferedimagelist) {//after processing
        The width and height of the image, the 9 pictures to be processed have the same height and width int width = bufferedimagelist.get (0). GetWidth ();
        int height = bufferedimagelist.get (0). GetHeight ();
        list<int[]> imagearraylist = new arraylist<int[]> ();
            for (BufferedImage bufferedimage:bufferedimagelist) {int[] Oneimagearray = new int[width * height];
            Read the picture into a byte array oneimagearray = BUFFEREDIMAGE.GETRGB (0, 0, width, height, oneimagearray, 0, width);
        Imagearraylist.add (Oneimagearray); ///merge pictures into the same BufferedImage object bufferedimage bufferedimageninebynine = new BufferedImage (width * 3, HEIGht * 3, BUFFEREDIMAGE.TYPE_INT_RGB);
        int x = 0;
        int y = 0; According to the coordinates of the x,y, output three sheets on the x-axis, then output the picture for (int[] buffered:imagearraylist) {Bufferedimageninebynine.setrgb
            (Width * y, Height * x, width, height, buffered, 0, width);
            y++;
                if (y% 3 = 0) {y = 0;
            x + +;
    } return bufferedimageninebynine;
 }
}


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.