Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中加载图像时遇到问题_Java_Rgb_Bufferedimage_Toolkit_Loadimage - Fatal编程技术网

在Java中加载图像时遇到问题

在Java中加载图像时遇到问题,java,rgb,bufferedimage,toolkit,loadimage,Java,Rgb,Bufferedimage,Toolkit,Loadimage,嗨,我正在上Java课程,对编程非常陌生 我正在尝试编写一个程序,要求用户加载不同的图像,并在多维数组中返回它们的RGB值。我有两个部分分别工作,但我有困难,使两者一起工作 我使用toolkit.getImage在一个类中加载图像- void imageload1 () { fd.show(); if(fd.getFile() == null) { Label1.setText("Try again");

嗨,我正在上Java课程,对编程非常陌生

我正在尝试编写一个程序,要求用户加载不同的图像,并在多维数组中返回它们的RGB值。我有两个部分分别工作,但我有困难,使两者一起工作

我使用toolkit.getImage在一个类中加载图像-

void imageload1 () 
    {
        fd.show();
        if(fd.getFile() == null)
        { 
            Label1.setText("Try again");
        }//end if
        else
        {
            String d = (fd.getDirectory() + fd.getFile());
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            image1 = toolkit.getImage(d);
            canvas1.setImage(image1);
            canvas1.repaint();

            GetRGB testRGB;
            testRGB = new GetRGB();
            testRGB.setup(image1, image2);

        }//end else
    }//end imageLoad1
然后将图像发送到其他类以获取RGB值-

public void setup(Image image1, Image image2) 
{
    try {
          // get the BufferedImage, using the ImageIO class
          BufferedImage image = 
            ImageIO.read(this.getClass().getResource("imag1"));
          marchThroughImage(image);
        } catch (IOException e) {
          System.err.println(e.getMessage());
        }
      }

    private void marchThroughImage(BufferedImage image) {
    int w = image.getWidth();
    int h = image.getHeight();
    int [][] pixels = new int [h][w];
    int lenRows = pixels.length;
    int lenColums = pixels[0].length;
    System.out.println("width, height: " + w + ", " + h);

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            System.out.println("x,y: " + j + ", " + i);
            int pixel = image.getRGB(j, i);
            printPixelARGB(pixel);
            pixels [i][j] = image.getRGB(j,i);
            System.out.println("");
            System.out.println("there are "+lenRows+" rows");
            System.out.println("there are "+lenColums+" colums");
        }
    }
}

public void printPixelARGB(int pixel) {
    int alpha = (pixel >> 24) & 0xff;
    int red = (pixel >> 16) & 0xff;
    int green = (pixel >> 8) & 0xff;
    int blue = (pixel) & 0xff;
    System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue);
}
public void设置(图像image1、图像image2)
{
试一试{
//使用ImageIO类获取BuffereImage
BuffereImage图像=
read(this.getClass().getResource(“imag1”);
marchThroughImage(图像);
}捕获(IOE异常){
System.err.println(e.getMessage());
}
}
私有void marchThroughImage(BuffereImage图像){
int w=image.getWidth();
int h=image.getHeight();
int[][]像素=新的int[h][w];
int lenRows=pixels.length;
int lenColums=像素[0]。长度;
系统输出打印项次(“宽度、高度:+w+”、“+h”);
对于(int i=0;i>24)&0xff;
int red=(像素>>16)和0xff;
绿色整数=(像素>>8)&0xff;
蓝色整数=(像素)&0xff;
System.out.println(“argb:+alpha+”、“+red+”、“+green+”、“+blue”);
}
}

这给了我一个“java.lang.IllegalArgumentException”错误。 我认为问题在于它无法读取图像。我不确定是否需要将图像转换为BuffereImage,或者是否命名错误,或者试图读取错误的文件。 我尝试了很多不同的方法来让它工作,但没有任何运气。任何帮助都将是巨大的,非常感谢


谢谢。

来自ImageIO.read javadoc的:“Throws:IllegalArgumentException-如果输入为null”,因此产生了问题,因为这个.getClass().getResource(“imag1”)返回null。首先,您应该为“imag1”(.png、.jpg等)指定扩展名并对其进行测试,如果问题仍然存在,请检查查找图像的路径(正如我从代码中看到的,它应该与包含方法“setup”的类位于同一目录中)。

在哪一行出现错误?你能把异常的堆栈跟踪放进去吗?我在这一部分遇到了问题,从我所知道的情况来看,我的图像需要转换成BuffereImage。BuffereImage image=ImageIO.read(this.getClass().getResource(“imag1”);谢谢很抱歉回复晚了,我正忙着让一切正常进行。我确实有我的一些周边关闭,我没有通过的东西正确。现在一切都好了。谢谢大家的帮助!