1
이미지를 그레이 스케일로 변환하는 코드에 이미지 이름 배열을 공급할 수 있습니까? 이 코드를 사용하여 그레이 스케일로 이미지를 변환 할 수 있어요Java - 파일 이름 배열을 사용하여 이미지를 그레이 스케일로 변환
그러나
public static void makeGrey() {
try{
//Read in original image.
BufferedImage image = ImageIO.read(new File("images\\012.jpg"));
//Obtain width and height of image.
double image_width = image.getWidth();
double image_height = image.getHeight();
BufferedImage bimg = null;
BufferedImage img = image;
//Draw the new image.
bimg = new BufferedImage((int)image_width, (int)image_height, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D gg = bimg.createGraphics();
gg.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
//Save new greyscale (output) image.
String temp = "_inverted";
File fi = new File("images\\" + temp + ".jpg");
ImageIO.write(bimg, "jpg", fi);
}
catch (Exception e){
System.out.println(e);
}
}
,이 코드는 한 번에 하나의 파일에 작동하고 나는 그것을 것에 대해 이동하는 방법을 알고 싶습니다 images
디렉토리에있는 모든 파일을 처리 하시겠습니까?
images
디렉토리를 통해 모든 파일의 이름을 저장하는 배열을 만들었으며이 파일 이름을 내 makeGrey()
메서드에 전달하는 방법을 알고 싶습니다.
static File dir = new File("images");
static File imgList[] = dir.listFiles();
public static void listFiles(String imageName) {
if(dir.isDirectory()){
for(File img : imgList){
if(img.isFile()){
MakeGrey.makeGrey();
}
}
}
고맙습니다.
가 감사를 - 나는이 변경 한 이에 경고하고있다 'image' 변수가 중복이라는 사실; 그런 다음 BufferedImage image = ImageIO.read (image);를 BufferedImage inputImage = ImageIO.read (image);로 변경하고 코드를 컴파일하고 실행할 때 아무런 반응이 없습니다 ... 제안 사항이 있습니까? – MusTheDataGuy
고맙습니다 만, 알아 냈습니다. 문제는 메소드 선언과 관련이 있습니다 :'public static void listFiles (String imageName)'은 입력 할 필요가 없으므로'public static void listFiles()'를 읽어야합니다. 다시 한번 감사드립니다. – MusTheDataGuy
또한 DirectoryScanner를 사용하여 이미지 파일 만 확장자로 나열 할 수 있습니다. http://stackoverflow.com/questions/794381/how-to-find-files-that-match-a-wildcard-string-in-java – Kris