我正在尝试创建一个程序,该程序生成图像并将每个像素的颜色设置为相同的值,然后将文件保存到a.PNG。我目前收到“坐标超出范围”错误,并尝试了几种方法对其进行调试。我认为这个问题与我的讲师编写的某些第三方代码有关,因为我打印了p值,该值是负数,尽管我认为该值必须介于0到255之间。

当前,它不会生成任何PNG文件。

有人可以建议我如何解决此代码吗?

import java.util.Scanner;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;

public class Lab_Week8_ImageFromText {

    public static void main(String[] args) throws Exception  {

    int image_width = 3;
    int image_height = 3;
    String output_file_path = ("image.png");
    String input_file_path = "";
    int r = 10;
    int b = 10;
    int g = 10;

    BufferedImage imagetowrite = imageFromText(image_width, image_height, input_file_path, output_file_path, r, g, b);
    File f = new File (output_file_path);
    ImageIO.write(imagetowrite, "png", f);

    }

    public static BufferedImage imageFromText (int image_width, int image_height, String input_file_path, String output_file_path, int r, int g, int b) throws IOException{

        if(image_width <= 0 || image_height <= 0) {
            System.err.println("Width and Height have to be strictly positive!");
            return null;
        }

        System.out.println("Hello");

        BufferedImage image = new BufferedImage (image_width, image_height, BufferedImage.TYPE_INT_ARGB);

        int x = 1;
        int y = 1;
        for (x = 1; x <= image_width; x++)
        {//System.out.println (x);
        for (y = 1; y <= image_height; y++){//System.out.println(y);
            setPixel(image, x, y, r, g, b);
    }}

        System.out.println("Hello 2" + image);
        return image;
    }

    public static void setPixel(BufferedImage image, int x, int y, int r, int g, int b) {
        /*
         * Test cases for error management
         */
        if(x < 0 || y < 0) {
            System.err.println("Coordinates (x, y) cannot be negative numbers!");
            return;
        }
        if(r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
            System.err.println("Colour values (r, g, b) have to be between 0 and 255!");
            return;
        }
        /*
         * Set the alpha value for the pixel, i.e. the transparency of the pixel, to its max value
         */
        int a = 255;
        /*
         * Write the different value of the pixel, i.e. the colours red, green, blue and alpha.
         * The different colour values are all stored into a single integer using byte operators.
         * a = x << y means write the value of x as bytes at the address y in the object a.
         * a = o1 | o2 means a will be composed by o1 and o2, where o1 and o2 are binary operators.
         * It is necessary to use this operator because the setRGB method of BufferedImage
         * take only one integer that have to hold all the colour values for one given pixel.
         */
        int p = (a << 24) | (r << 16) | (g << 8) | b;
        /*
         * Use the setRGB method from BufferedImage to write the new pixel value (i.e. colour) to the image
         */
        image.setRGB(x, y, p);
        System.out.println(p);
    }

}


这是p的输出和错误消息:

Hello
-16119286
-16119286
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
        at sun.awt.image.IntegerInterleavedRaster.setDataElements(IntegerInterleavedRaster.java:301)
        at java.awt.image.BufferedImage.setRGB(BufferedImage.java:1016)
        at Lab_Week8_ImageFromText.setPixel(Lab_Week8_ImageFromText.java:79)
        at Lab_Week8_ImageFromText.imageFromText(Lab_Week8_ImageFromText.java:44)
        at Lab_Week8_ImageFromText.main(Lab_Week8_ImageFromText.java:22)

最佳答案

对于新手程序员而言,这里的问题是对数组索引的常见误解。大多数现代编程语言(关键包括Java)对长度为size的数组进行索引,第一个元素在索引0上,最后一个元素在size-1

在下面引用的程序的两个循环中,都允许索引等于数组的长度/ size。这意味着程序将在某个时候尝试访问size处的元素。

for (x = 1; x <= image_width; x++){
    //System.out.println (x);
    for (y = 1; y <= image_height; y++){
        //System.out.println(y);
        setPixel(image, x, y, r, g, b);
    }
}


因此,要纠正这些循环,您将需要像这样设置边界:

for (x = 0; x < image_width; x++){
    //System.out.println (x);
    for (y = 0; y < image_height; y++){
        //System.out.println(y);
        setPixel(image, x, y, r, g, b);
    }
}


请注意,我们现在从0开始并在image_whatever - 1结束,这意味着两个循环中的最后一个调用实际上将是这样的:

setPixel(image, image_width-1, image_height-1, r, g, b);


我希望这可以帮助您理解您的错误。

10-05 21:14
查看更多