读取和存储在java中的.bmp文件 [英] reading and storing a .bmp file in java

查看:425
本文介绍了读取和存储在java中的.bmp文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读名为.bmp文件 circle1.bmp 。这是我所导入以下文件的包。

到目前为止,我有以下的code,但是当我运行下面的code我得到:

javax.imageio.llOException:无法读取输入文件

 公共无效设置()抛出IOException异常
{
    BufferedImage的图像= ImageIO.read(新文件(circle1.bmp));
    字节[] [] = greenInputData新的字节[30] [40];    为(中间体X = 0; X&下; inputData.length; X ++)
    {
        对于(INT Y = 0; Y< inputData [X]。长度; Y ++)
        {
            INT颜色= image.getRGB(X,Y);
            //阿尔法[X] [Y] =(字节)(彩色>> 24);
            //红色[X] [Y] =(字节)(彩色>> 16);
            greenInputData [X] [Y] =(字节)(彩色>→8);
            //蓝[X] [Y] =(字节)(彩色);
        }
    }
    this.inputData = greenInputData;    的System.out.println(this.inputData);
}


解决方案

您应该尝试像

 图像= ImageIO.read(的getClass()的getResourceAsStream(路径/要/你/ file.bmp));

I'm am trying to read a .bmp file called circle1.bmp. It is in a package that I have imported into the following file.

So far I have the following code, but when I run the following code I get:

javax.imageio.llOException: Can't read input file!

public void setUp() throws IOException
{
    BufferedImage image = ImageIO.read(new File("circle1.bmp"));
    byte[][] greenInputData = new byte[30][40];

    for (int x = 0; x < inputData.length; x++)
    {
        for (int y = 0; y < inputData[x].length; y++)
        {
            int color = image.getRGB(x, y);
            //alpha[x][y] = (byte)(color>>24);
            //red[x][y] = (byte)(color>>16);
            greenInputData[x][y] = (byte)(color>>8);
            //blue[x][y] = (byte)(color);
        }
    }
    this.inputData = greenInputData;

    System.out.println(this.inputData);
}

解决方案

You should try something like

image = ImageIO.read(getClass().getResourceAsStream("path/to/your/file.bmp"));

这篇关于读取和存储在java中的.bmp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆