How do I call the method in this java program?

868 views Asked by At

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. 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. To call the methods the user would type in something like -writeGrayscaleImage which would call the method 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);
        }
    }
}
3

There are 3 answers

0
AudioBubble On

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.

0
mdnghtblue On

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:

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

      if (cmd.hasOption("writeGrayscaleImage") && cmd.hasOption("image"))
      {
           ImageProcessor.writeGrayscaleImage(cmd.getOptionValue("image"), ...);
      }
 }
5
Spekular On
  1. You'll need to have a main method somewhere in order to run this from command line. Either create a separate class, or add a main method to ImageProcessor.
  2. To access command line arguments, read them from the "args" array that you get in main
  3. To call the method, you'll have to set up some sort of if/then. I'd suggest using a switch statement.

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 (args1) is the name of the file. 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)