2014-01-19 42 views
0

我想從java中的其他圖像製作圖像,我有一些拼貼,並且裁剪了我需要的部分,我想將它們分組到另一個圖像中;這裏的爲例:如何從java中的一些其他圖像製作圖像

enter image description here

可以說,我已經裁剪圖像中的每個方塊,我又需要一個功能,他們組在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

基本上你創建一個新的[BufferedImage的(http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html#getGraphics%28%29)然後畫出其他圖像緩衝區與[drawImage()](http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawImage%28java.awt.Image,%20int,%20int,%20int ,%20int,%20java.awt.image.ImageObserver%29) – PeterMmm

+0

我忘記說我正在使用Slick Image librarie,所以看起來很難從Image轉換到BufferedImage – anony50600

回答

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); 
} 

} 
相關問題