java implements an example of merging images

  • 2020-06-12 09:00:21
  • OfStack

An example of java shows how to merge images. To share for your reference, specific as follows:


package com.test;
import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class ImageCombineTest {
  public static void main(String args[]) {
    try {
      //  Read the first 1 image 
      File fileOne = new File("/Users/coolcloud/Pictures/Art/lena-2.jpg");
      BufferedImage ImageOne = ImageIO.read(fileOne);
      int width = ImageOne.getWidth();
      //  Image width 
      int height = ImageOne.getHeight();
      //  Picture height 
      //  Read from the picture RGB
      int[] ImageArrayOne = new int[width * height];
      ImageArrayOne = ImageOne.getRGB(0, 0, width, height, ImageArrayOne,
      0, width);
      //  For the first 2 Do the same for this picture 
      File fileTwo = new File("/Users/coolcloud/Pictures/Art/lena-2.jpg");
      BufferedImage ImageTwo = ImageIO.read(fileTwo);
      int[] ImageArrayTwo = new int[width * height];
      ImageArrayTwo = ImageTwo.getRGB(0, 0, width, height, ImageArrayTwo,
      0, width);
      //  Generate a new image 
      // BufferedImage ImageNew = new BufferedImage(width * 2, height,
      // BufferedImage.TYPE_INT_RGB);
      BufferedImage ImageNew = new BufferedImage(width*2, height*2,
      BufferedImage.TYPE_INT_RGB);
      ImageNew.setRGB(0, 0, width, height, ImageArrayOne, 0, width);
      //  Set the left half RGB
      // ImageNew.setRGB(width, 0, width, height, ImageArrayTwo, 0, width);//  Set the right half RGB
      // ImageNew.setRGB(0, height, width, ImageOne.getHeight()+ImageTwo.getHeight(), ImageArrayTwo, 0, width);//  Set the right half RGB
      ImageNew.setRGB(0, height, width, height, ImageArrayTwo, 0, width);
      //  Set the right half RGB
      File outFile = new File("/Users/coolcloud/Pictures/generatepic.jpg");
      ImageIO.write(ImageNew, "png", outFile);
      //  Write image 
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

More java related content interested readers can see the special topics: Java picture operation skills Summary, java Date and time operation skills Summary, Java DOM node operation skills Summary, Java File and directory operation Skills Summary and Java Data Structure and Algorithm Tutorial.

I hope this article has been helpful in java programming.


Related articles: