0

I'm working on a project for my comp sci class which deals with image processing. My group and I are going to vertically flip and rotate a picture 90 degrees. The first part of my code is the sample from my teacher that turns the photo gray. I'm able to compile my code, but I get the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Project6b.main(Project6b.java:10)

Press any key to continue...

I'm not even sure if what I've coded so far works, and I can't get this to run. Can anyone help me solve my runtime error?

import java.awt.image.*; import java.io.*; import javax.imageio.*; import java.awt.*; public class Project6b { public static void main (String args[]) throws Exception { // I would use these, if the program is finished. //grayscale(args[0], args[1]); //verticalFlip(args[0], args[1]); // I would comment the first two lines and //uncomment these for testing purposes. //grayscale("Vicc.jpg", "Vicc2.jpg"); verticalFlip("Vic.png", "Vic2.png"); } /*public static void grayscale(String originalImage, String convertedImage) throws Exception { BufferedImage bufferImg = ImageIO.read(new File(originalImage)); int r,g,b; Color color = null; for( int x = 0; x <bufferImg.getWidth(); x++) { for(int y = 0; y<bufferImg.getHeight(); y++) { int rgb = bufferImg.getRGB(x,y); color = new Color(rgb,true); r = color.getRed(); g = color.getGreen(); b = color.getBlue(); color = new Color((r+g+b)/3, (r+g+b)/3, (r+g+b)/3); bufferImg.setRGB(x,y, color.getRGB()); } } File outputfile = new File(convertedImage); ImageIO.write(bufferImg, "png", outputfile); } */ public static void verticalFlip(String originalImage, String convertedImage) throws Exception { BufferedImage bufferImg = ImageIO.read(new File(originalImage)); BufferedImage bufferImgOut = new BufferedImage(bufferImg.getWidth(),bufferImg.getHeight(), bufferImg.getType()); for (int x = 0; x < bufferImg.getWidth(); x++) { for (int y = 0; y < bufferImg.getHeight(); y++) { int px = bufferImg.getRGB(x, y); int destY = bufferImg.getHeight() - y - 1; bufferImg.setRGB(x, destY, px); } } File outputfile = new File(convertedImage); ImageIO.write(bufferImgOut, "png", outputfile); } //public static void Rotate(String originalImage, String convertedImage) throws Exception //{ //} } 
1
  • 3
    Did you pass in any arguments? Commented Jan 18, 2014 at 21:13

1 Answer 1

1

The problem occurs when you don't pass any arguments at start up, like reimeus already mentioned.

If you are using an IDE like Eclipse or Netbeans, there should be some kind of start cfg where you can enter the arguments, which should be passed to the app. In case you are launching your app from the command line, use:

java Project6b Jack.jpg Jack2.jpg

java PROGRAM INPUT-IMAGE OUTPUT-IMAGE

Then you can change your main respectively:

public static void main (String args[]) throws Exception { // I would use these, if the program is finished. grayscale(args[0], args[1]); verticalFlip(args[0], args[1]); // I would comment the first two lines and //uncomment these for testing purposes. //grayscale("Jack.jpg", "Jack.jpg"); //verticalFlip("Jack.jpg", "Jack.jpg"); } 

(If you use the function calls at the bottom, you don't need to pass any arguments. This is good for quick debugging.) Hope it helps!

Regarding verticalFlip:

public static void verticalFlip(String originalImage, String convertedImage) throws Exception { BufferedImage bufferImg = ImageIO.read(new File(originalImage)); // create a new Image, which will be your output img, so that // you do NOT override some pixels in your source img - you'll need them. BufferedImage bufferImgOut = new BufferedImage(bufferImg.getWidth(), bufferImg.getHeight(), bufferImg.getType()); for (int x = 0; x < bufferImg.getWidth(); x++) { for (int y = 0; y < bufferImg.getHeight(); y++) { int px = bufferImg.getRGB(x, y); int destY = bufferImg.getHeight() - y - 1; // your x-coordinate should stay the same, since you only flip vertically. bufferImgOut.setRGB(x, destY, px); } } File outputfile = new File(convertedImage); ImageIO.write(bufferImgOut, "png", outputfile); } 
Sign up to request clarification or add additional context in comments.

17 Comments

Okay, um I think I get what you're saying. I just started this comp sci class so I'm not the best... but am I supposed to take the java Project6b Jack.jpg Jack2.jpg
sorry, am I supposed to that that line I just highlighted and put that on my public class Project6b line? and where do I put the java PROGRAM INPUT-IMAGE OUTPUT-IMAGE line in my code? and for the flipping method... I'm really not sure what I'm doing... kind of just figuring it out as I go. How should I fix the code so the whole thing flips?
Okay, I used the main method you had, but now I get this error,
Exception in thread "main" javax.imageio.IIOException: Can't read input file! at javax.imageio.ImageIO.read(ImageIO.java:1301) at Project6b.grayscale(Project6b.java:23) at Project6b.main(Project6b.java:18) Press any key to continue...
Ok I corrected your flip method. How exactly do you launch your main method or program respectively? In this case, when your image is not in the same folder as your .class file, you have to adjust the path - e.g. java Project6b ../images/Jack.jpg ../images/Jack2.jpg. You know what I mean?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.