English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes the method of merging images implemented in Java. Share it with everyone for reference, 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 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(); // Image height // Read RGB from the image int[] ImageArrayOne = new int[width * height]; ImageArrayOne = ImageOne.getRGB(0, 0, width, height, ImageArrayOne, 0, width); // Perform the same processing on the second image 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 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 RGB of the left half // ImageNew.setRGB(width, 0, width, height, ImageArrayTwo, 0, width);// Set the RGB of the right half // ImageNew.setRGB(0, height, width, ImageOne.getHeight())+ImageTwo.getHeight(), ImageArrayTwo, 0, width);// Set the RGB of the right half ImageNew.setRGB(0, height, width, height, ImageArrayTwo, 0, width); // Set the RGB of the right half File outFile = new File("/Users/coolcloud/Pictures/generatepic.jpg"); ImageIO.write(ImageNew, "png", outFile); // Write image } catch (Exception e) { e.printStackTrace(); } } }
Readers who are interested in more Java-related content can check the special topics on this site: 'Summary of Java Image Operation Skills', 'Summary of Java Date and Time Operation Skills', 'Summary of Java DOM Node Operation Skills', 'Summary of Java File and Directory Operation Skills', and 'Java Data Structures and Algorithms Tutorial'.
I hope the content described in this article will be helpful to everyone in Java program design.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report any violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.