图像处理代码运行时错误

时间:2014-01-18 21:08:07

标签: java image-processing runtime-error

我正在为我的comp sci课程开展一个处理图像处理的项目。我的小组和我将垂直翻转并将图片旋转90度。我的代码的第一部分是我老师的样本,将照片变为灰色。我能够编译我的代码,但是我得到了错误:

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

Press any key to continue...

我甚至不确定到目前为止我编码的是否有效,而且我无法运行。任何人都可以帮我解决运行时错误吗?

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 个答案:

答案 0 :(得分:1)

如果您在启动时未传递任何参数,则会出现此问题,例如已提及的reimeus

如果您使用 IDE ,例如 Eclipse Netbeans ,则应该有某种启动cfg,您可以在其中输入参数,应该传递给应用程序。如果从命令行启动应用程序,请使用:

java Project6b Jack.jpg Jack2.jpg

java PROGRAM INPUT-IMAGE OUTPUT-IMAGE

然后您可以分别更改main

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

(如果使用底部的函数调用,则不需要传递任何参数。这对于快速调试很有用。) 希望它有所帮助!

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