Let's take a look:
The main code is as follows:
[Java]
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 picture will be added a watermark.
*/
Public class ComposePicture {
Public static void main (String args [])
Throws IOException {
// Read nine images from the project, perform further processing on the images (scale down the image, rotate for 30 degrees, add a watermark), and save the images to the memory for further processing.
List <BufferedImage> bufferedImageList = readPicturesToMemory ();
// Merge nine images into one image in the memory
BufferedImage bufferedImageNineByNine = composeNinePicturesToOne (bufferedImageList );
// Write images in the memory to the specified file
File fileOutPut = new File (System. getProperty ("user. dir") + "/handledpictures/compose.jpg ");
ImageIO. write (bufferedImageNineByNine, "jpg", fileOutPut );
}
Public static List <BufferedImage> readPicturesToMemory ()
Throws IOException {
String [] pathArray = new String [9];
List <BufferedImage> bufferedImageList = new ArrayList <BufferedImage> ();
// Thumbnail reads the watermark file to the memory
BufferedImage waterMarkBufferedImage = Thumbnails. of (ComposePicture. class. getClassLoader (). getResource ("5.png"). getFile ())
// The Thumbnail method is scaled down according to the proportion. 0.4f means to scale down to 40% of the source image.
. Scale (0.4f)
// Read as a BufferedImage object
. AsBufferedImage ();
// Read 9 images to be processed to the memory
For (int I = 0; I <9; I ++ ){
// Obtain the image path
PathArray [I] = ComposePicture. class. getClassLoader (). getResource ("IMG_000" + I + ". JPG"). getFile ();
BufferedImage image = Thumbnails. of (pathArray [I])
// Scale down
. Scale (0.05f)
// Add a watermark. 0.5f indicates transparency.
. Watermark (Positions. CENTER, waterMarkBufferedImage, 0.5f)
// Rotate for 30 degrees
. Rotate (30)
// Read as a BufferedImage object
. AsBufferedImage ();
BufferedImageList. add (image );
}
Return bufferedImageList;
}
Public static BufferedImage composeNinePicturesToOne (List <BufferedImage> bufferedImageList ){
// Obtain the width and height of the processed image. The nine images 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 image into a byte array
OneImageArray = bufferedImage. getRGB (0, 0, width, height, oneImageArray, 0, width );
ImageArrayList. add (oneImageArray );
}
// Merge the image to 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 x and y, the three elements on the x axis are output first, and then the image on the y axis is output.
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;
}
}