2014-11-18 6 views
0

내가 가진 것은 이미지 프로세서 역할을하는 코드입니다. 이 코드에는 많은 메소드가 있지만 내가 알고 싶은 것은 어떻게 메소드를 호출하여 사용자가 java imageprocessor를 입력하는 대신 CMD에서 프로그램을 실행할 때 대신 java -imageprocessor -ascii image를 입력하는 것이다. jpg image.txt. 이것이 의미하는 바는 프로그램이 해당 이미지를 읽고 image.txt라는 파일에 저장하는 ASCII 버전을 생성한다는 것입니다. 메소드를 호출하려면 사용자가 writeGrayscaleImage 메소드를 호출하는 -writeGrayscaleImage와 같이 입력하십시오. 그렇다면 사용자가 메서드를 호출 할 수 있도록 구현하는 방법은 무엇입니까?이 Java 프로그램에서 메소드를 호출하려면 어떻게해야합니까?

import java.awt.Color; 
import java.awt.image.BufferedImage; 
import java.io.*; 
import javax.imageio.ImageIO; 

public class ImageProcessor { 



    public static int[][] readGrayscaleImage(String filename) { 
     int [][] result = null; //create the array 
     try { 
      File imageFile = new File(filename); //create the file 
      BufferedImage image = ImageIO.read(imageFile); 
      int height = image.getHeight(); 
      int width = image.getWidth(); 
      result = new int[height][width];  //read each pixel value 
      for (int x = 0; x < width; x++) { 
       for (int y = 0; y < height; y++) { 
        int rgb = image.getRGB(x, y); 
        result[y][x] = rgb & 0xff; 
       } 
      } 
     } 
     catch (IOException ioe) { 
      System.err.println("Problems reading file named " + filename); 
      System.exit(-1); 
     } 
     return result; 
    } 


    public static void writeGrayscaleImage(String filename, int[][] array) { 
     int width = array[0].length; 
     int height = array.length; 

     try { 
      BufferedImage image = new BufferedImage(width, height, 
       BufferedImage.TYPE_INT_RGB); //create the image 

      for (int x = 0; x < width; x++) { 
       for (int y = 0; y < height; y++) { 
        int rgb = array[y][x]; 
        rgb |= rgb << 8; 
        rgb |= rgb << 16; 
        image.setRGB(x, y, rgb); 
       } 
      } 

      File imageFile = new File(filename); 
      ImageIO.write(image, "jpg", imageFile); 
     } 
     catch (IOException ioe) { 
      System.err.println("Problems writing file named " + filename); 
      System.exit(-1); 
     } 
    } 
} 
+1

와 메소드를 호출한다 : 당신은 쉽게 단지 형식에 따라, 새로운 방법을 추가 할 수 있습니다 당신의 목표를 달성하기까지 그리고 왜 그것이 효과가 없습니까? –

+0

가능한 복제 [정적 메서드/필드 호출] (0120) (영문) –

답변

0

잘 브랜든 MacLeod에, 당신은 메인 메소드에서 인수 호출 적절한 방법에 따라 명령 행 인수로 주요 방법 및 공급 방법 이름을 파일 이름을 생성해야 : 여기에 지금까지 내 코드입니다. 또는 정적 블록을 사용하여 main 메소드 전에 정적 블록이 실행되고 정적 블록에도 인수를 제공 할 수 있습니다.

0

주 방법에서는 명령 줄 인수를 확인한 다음 해당 값을 기반으로 올바른 방법으로 프로그램을 라우팅하십시오. 예 :

public static void main(String[] args) 
{ 
     // ... insert CLI setup code 

     if (cmd.hasOption("writeGrayscaleImage") && cmd.hasOption("image")) 
     { 
      ImageProcessor.writeGrayscaleImage(cmd.getOptionValue("image"), ...); 
     } 
} 
0
  1. 당신은 명령에서 이것을 실행하기 위해 어딘가에 주요 방법이 있어야합니다 : 당신이 Apache Commons CLI 같은 라이브러리를 사용하는 경우

    public static void main(String[] args) 
    { 
         if (args[0].equals("-writeGrayscaleImage")) // in reality you should check the whole array for this value 
         { 
          ImageProcessor.writeGrayscaleImage(..., ...); 
         } 
    } 
    

    , 당신은 같은 것을 할 수 있습니다 선. 별도의 클래스를 만들거나 ImageProcessor에 main 메서드를 추가하십시오.

  2. 명령 줄 인수에 액세스하려면 주에서 가져 오는 "args"배열에서 읽습니다.
  3. 메서드를 호출하려면 if/then 중 일부 정렬을 설정해야합니다. 인

    class Program{ 
        public static void main (String[] args){ 
    
         switch (args[0]){ 
          case "readGrayscaleImage": 
           ImageProcessor.readGrayScaleImage(args[1]); 
           break; 
          case "writeGrayscaleImage": 
           ImageProcessor.writeGrayscaleImage(args[1], array); //You'll have to get your array somehow 
           break; 
         } 
        } 
    } 
    

    이 첫번째 인수 가정은 (인수 [0])에있어서, 명 (정확하게), 두 번째 인수 (인수 1)를이다 I 예컨대 a switch statement.

사용 좋을 것 파일의 이름. 커맨드 라인에서 배열을 어떻게 입력 할 지 모르겠다. 그래서 직접 알아 내야 할 것이다. 당신이 당신의 ImageProcessor 클래스의 주요 메소드를 추가하기로 결정하는 경우

case "methodName": 
    ImageProcessor.methodName(arguments); 

, 당신은 당신이 그렇게 노력이 무엇 methodName(args) 대신 ImageProcessor.methodName(args)

+0

메서드가 정적이므로 개체를 만드는 것이이 경우 완전히 유효하지 않습니다. . – mdnghtblue

+0

@mdnghtblue 어떤 문제가 발생합니까? 전 정적 인 것에 대해서는 완전히 확신 할 수 없습니다. – Spekular

+0

@mdnghtblue 기다리십시오, 나는 processor.methodName()를 ImageProcessor.methodName()으로 대체해야합니다, 맞습니까? – Spekular

관련 문제