简体   繁体   English

如何在此Java程序中调用该方法?

[英]How do I call the method in this java program?

What I have is a code that acts as an image processor. 我所拥有的是充当图像处理器的代码。 The code has a bunch of methods but what I want to know is how do I call the methods so that when the user runs the program (from CMD) instead of just entering java imageprocessor, they instead would type java -imageprocessor –ascii image.jpg image.txt. 该代码有很多方法,但是我想知道如何调用这些方法,以便当用户运行程序(从CMD)而不是仅输入java imageprocessor时,他们将键入java -imageprocessor -ascii image。 jpg image.txt。 What that means is the program reads that image and produces an ascii version of it which saves it in a file called image.txt. 这意味着程序读取该图像并生成该图像的ascii版本,并将其保存在名为image.txt的文件中。 To call the methods the user would type in something like -writeGrayscaleImage which would call the method writeGrayscaleImage. 要调用这些方法,用户将键入-writeGrayscaleImage之类的内容,该名称将调用方法writeGrayscaleImage。 So how would I implement it so the user calls the method? 那么我将如何实现它以便用户调用该方法? Here's my code so far: 到目前为止,这是我的代码:

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

Well Mr. Brandon MacLeod, You have to create a main method and supply method name file names as command line argument and based on argument call appropriate method from main method. 好吧,布兰登·麦克劳德先生,您必须创建一个主方法,并提供方法名称文件名作为命令行参数,并基于从主方法中调用适当的方法。 or you can use static block to call method as static block will run before main method and you can supply argument to static block too. 或者您可以使用静态块来调用方法,因为静态块将在主方法之前运行,并且您也可以为静态块提供参数。

In your main method, check the command line arguments and then route the program to the correct method based on the values. 在您的主要方法中,检查命令行参数,然后根据值将程序路由到正确的方法。 Example: 例:

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

If you use a library like Apache Commons CLI , you can do something like this: 如果使用类似Apache Commons CLI的库,则可以执行以下操作:

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

      if (cmd.hasOption("writeGrayscaleImage") && cmd.hasOption("image"))
      {
           ImageProcessor.writeGrayscaleImage(cmd.getOptionValue("image"), ...);
      }
 }
  1. You'll need to have a main method somewhere in order to run this from command line. 您需要在某个地方有一个main方法,以便从命令行运行它。 Either create a separate class, or add a main method to ImageProcessor. 创建单独的类,或向ImageProcessor添加main方法。
  2. To access command line arguments, read them from the "args" array that you get in main 要访问命令行参数,请从主菜单中获取的“ args”数组中读取它们
  3. To call the method, you'll have to set up some sort of if/then. 要调用该方法,您必须设置某种if / then。 I'd suggest using a switch statement. 我建议使用switch语句。

For example: 例如:

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

This assumes the first argument (args[0]) is the method name (exactly), and the second argument (args 1 ) is the name of the file. 假定第一个参数(args [0])是方法名称(完全),第二个参数(args 1 )是文件名称。 I'm not sure how you expect to input an array from command line, so you'll have to figure that out yourself. 我不确定您希望如何从命令行输入数组,因此您必须自己弄清楚。 You can easily add new methods, just by following this format: 您只需遵循以下格式即可轻松添加新方法:

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

If you decide to add the main method to your ImageProcessor class, you should call methods with methodName(args) instead of ImageProcessor.methodName(args) 如果决定将主方法添加到ImageProcessor类,则应使用methodName(args)而不是ImageProcessor.methodName(args)调用方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM