如何读取.bmp文件来识别Java中哪些像素是黑色的 [英] How to read a .bmp file identify which pixels are black in Java

查看:121
本文介绍了如何读取.bmp文件来识别Java中哪些像素是黑色的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像下面这样的东西,除了使它工作:

  public void seeBMPImage(String BMPFileName)throws IOException { b $ b BufferedImage image = ImageIO.read(getClass()。getResource(BMPFileName)); 

int [] [] array2D = new int [66] [66]; (int xPixel = 0; xPixel< array2D.length; xPixel ++)

for(int yPixel = 0; yPixel< array2D [xPixel] .length; yPixel ++)
{
int color = image.getRGB(xPixel,yPixel); ((颜色>> 23)== 1){
array2D [xPixel] [yPixel] = 1;
} else {
array2D [xPixel] [yPixel] = 1;



$ b $ / code $ / pre

解决方案我会用这个:
$ b $ pre $ public void seeBMPImage(String BMPFileName)throws IOException {
BufferedImage image = ImageIO.read(getClass()。getResource(BMPFileName));

int [] [] array2D = new int [image.getWidth()] [image.getHeight()]; (int yPixel = 0; yPixel< image.getHeight())的

(int xPixel = 0; xPixel< image.getWidth(); xPixel ++)
{ ; yPixel ++)
{
int color = image.getRGB(xPixel,yPixel);
if(color == Color.BLACK.getRGB()){
array2D [xPixel] [yPixel] = 1;
} else {
array2D [xPixel] [yPixel] = 0; //?



$ b code


隐藏RGB的所有细节给你,是更容易理解。


Something like the following... except making it work:

public void seeBMPImage(String BMPFileName) throws IOException {
BufferedImage image = ImageIO.read(getClass().getResource(BMPFileName));

    int[][] array2D = new int[66][66];

for (int xPixel = 0; xPixel < array2D.length; xPixel++)
    {
        for (int yPixel = 0; yPixel < array2D[xPixel].length; yPixel++)
        {
            int color = image.getRGB(xPixel, yPixel);
            if ((color >> 23) == 1) {
                array2D[xPixel][yPixel] = 1;
            } else {
                array2D[xPixel][yPixel] = 1;
            }
        }
    }
}

解决方案

I would use this:

public void seeBMPImage(String BMPFileName) throws IOException {
    BufferedImage image = ImageIO.read(getClass().getResource(BMPFileName));

    int[][] array2D = new int[image.getWidth()][image.getHeight()];

    for (int xPixel = 0; xPixel < image.getWidth(); xPixel++)
        {
            for (int yPixel = 0; yPixel < image.getHeight(); yPixel++)
            {
                int color = image.getRGB(xPixel, yPixel);
                if (color==Color.BLACK.getRGB()) {
                    array2D[xPixel][yPixel] = 1;
                } else {
                    array2D[xPixel][yPixel] = 0; // ?
                }
            }
        }
    }

It hides all the details of RGB to you and is more comprehensible.

这篇关于如何读取.bmp文件来识别Java中哪些像素是黑色的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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