我想用java中的其他图像制作图像,我有一些瓷砖,我已经裁剪了我需要的部分,我想将它们分组到另一个图像中;这里有一个例子:
假设我已经裁剪了图像中的每个方块,我需要一个函数将它们再次分组为1个图像,如第一张图片
public Image groupe(Image[][] images){
Image image=new Image();
for(int i=0;i<images.length;i++){
for(int j=0;j<images[0].length;j++){
//here i need a function to groupe the images into image
}
}
return image;
}
答案 0 :(得分:0)
尝试这样的事情
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Crop {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//split
BufferedImage image = ImageIO.read(new File("E:\\workspaceIndigo2\\crop\\src\\plasma.gif"));
System.out.println("Original Image Dimension: "+image.getWidth()+"x"+image.getHeight());
//Get the cropped image
BufferedImage firstHalf = image.getSubimage(0, 0, (image.getWidth()/2),image.getHeight());
BufferedImage secondHalf = image.getSubimage(image.getWidth()/2, 0, image.getWidth()/2, image.getHeight());
//Create a file to stream the out buffered image to
File croppedFile1 = new File("E:\\workspaceIndigo2\\crop\\src\\half1.png");
File croppedFile2 = new File("E:\\workspaceIndigo2\\crop\\src\\half2.png");
//Write the cropped file
ImageIO.write(firstHalf, "png", croppedFile1);
ImageIO.write(secondHalf, "png", croppedFile2);
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//join
BufferedImage joined = new BufferedImage(image.getWidth(),image.getHeight(), image.getType());
BufferedImage image1 = ImageIO.read(new File("E:\\workspaceIndigo2\\crop\\src\\half1.png"));
BufferedImage image2 = ImageIO.read(new File("E:\\workspaceIndigo2\\crop\\src\\half2.png"));
Graphics2D graph = joined.createGraphics();
graph.drawImage(image1, 0, 0,null);
graph.drawImage(image2, image1.getWidth(), 0,null);
File joinedFile = new File("E:\\workspaceIndigo2\\crop\\src\\joined.png");
ImageIO.write(joined, "png", joinedFile);
}
}