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