2012-02-28 4 views
-1

300 개의 이미지 파일 이름 배열을 가지고 있으며 각 파일 이름을 새로운 BufferedImage로 변환하고자합니다.Java - 파일 [] 항목을 BufferedImage로 변환

300 이미지 이름의

배열하여 만들어집니다

//Default image directory (to convert to greyscale). 
static File dir = new File("images"); 
//Array of original image filenames. 
static File imgList[] = dir.listFiles(); 

public static void processGreyscale(){ 
    if(dir.isDirectory()){ 
     for(File img : imgList){ 
      if(img.isFile()){ 
       //functions are carried out here. 
      } 
      else{ 
       //functions are carried out here. 
      } 
     } 
    } 
} 

의 라인을 따라 뭔가를 사용하여 BufferedImage 항목을 모두 imgList[x] 항목을 변환하는 방법이 : 나는 솔루션 아래에 희망

File file = new File(new BufferedImage(imgList[0-300])); 

try { 
    image = ImageIO.read(file); 
} catch (IOException e) { 
    ... 
} 
+1

코드의 2 비트가 이해가되지 않으며, 컴파일되지 않습니다. File 배열을 반복하고 ImageIO로 각각로드합니다. 각로드는 이미지를 반환합니다. [Java 자습서] (http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html)를 참조하십시오.) 이에. – DNA

+0

두 번째 부분은 이론적으로 볼 수있는 코드 조각이기 때문에 컴파일되지 않습니다. – MusTheDataGuy

답변

1

가 너를 도울 것이다.

import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.image.BufferedImage; 
import java.awt.image.ImageObserver; 
public class BufferedImageBuilder { 

private static final int DEFAULT_IMAGE_TYPE = BufferedImage.TYPE_INT_RGB; 

public BufferedImage bufferImage(Image image) { 
    return bufferImage(image, DEFAULT_IMAGE_TYPE); 
} 

public BufferedImage bufferImage(Image image, int type) { 
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); 
    Graphics2D g = bufferedImage.createGraphics(); 
    g.drawImage(image, null, null); 
    waitForImage(bufferedImage); 
    return bufferedImage; 
} 

private void waitForImage(BufferedImage bufferedImage) { 
    final ImageLoadStatus imageLoadStatus = new ImageLoadStatus(); 
    bufferedImage.getHeight(new ImageObserver() { 
     public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { 
      if (infoflags == ALLBITS) { 
       imageLoadStatus.heightDone = true; 
       return true; 
      } 
      return false; 
     } 
    }); 
    bufferedImage.getWidth(new ImageObserver() { 
     public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { 
      if (infoflags == ALLBITS) { 
       imageLoadStatus.widthDone = true; 
       return true; 
      } 
      return false; 
     } 
    }); 
    while (!imageLoadStatus.widthDone && !imageLoadStatus.heightDone) { 
     try { 
      Thread.sleep(300); 
     } catch (InterruptedException e) { 

     } 
    } 
} 

class ImageLoadStatus { 

    public boolean widthDone = false; 
    public boolean heightDone = false; 
} 

}