需要从图像的顶部,底部,右侧和左侧照亮20个像素,然后我需要比较像素值

时间:2016-03-21 12:57:56

标签: java raster javax.imageio r-raster

我需要确定给定的图像是空白还是具有相同的像素值,请找到以下代码。我想设置一个容差。我不想将顶部,底部,左侧和右侧20个像素传递给此逻辑。请帮忙!

for (String pic : Finallist) {
    BufferedImage image = ImageIO.read(new File(pic));
    final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
    final int width = image.getWidth();
    final int height = image.getHeight();
    final boolean hasAlphaChannel = image.getAlphaRaster() != null;
    boolean blankImage=true;
    int[][] result = new int[height][width];
    if (hasAlphaChannel) {
        final int pixelLength = 4;
        for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
            int argb = 0;
            argb += (((int) pixels[pixel] & 0xff) << 24); // alpha
            argb += ((int) pixels[pixel + 1] & 0xff); // blue
            argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green
            argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red
            result[row][col] = argb;
            if(result[row][col]!=result[0][0]) {
                blankImage=false;
            }
            col++;

            if (col == width) {
                col = 0;
                row++;
            }
        }
    } else {
        final int pixelLength = 3;
        for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
            int argb = 0;
            argb += -16777216; // 255 alpha
            argb += ((int) pixels[pixel] & 0xff); // blue
            argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green
            argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red
            result[row][col] = argb;
            if(result[row][col]!=result[0][0]) {
                blankImage=false;
            }
            col++;

            if (col == width) {
                col = 0;
                row++;
            }            
        }
    }

    if(blankImage==true) {
        try {
            System.out.println("Blank image found and its deleted");
            File f = new File(pic);
            f.delete();
        } catch(Exception e) {
            System.out.println("Exception"+e);
        }
    }else {
        FinalListWithOutBlank.add(pic);
    }
}

我想要一切都在播出!所以我的代码性能不会很痛苦..我只是想跳过那些像素来触及这个逻辑..

1 个答案:

答案 0 :(得分:0)

使用

将所需的感兴趣区域复制到另一张图像中
BufferedImage imageForEvaluation = image.getSubimage(x, y, width, height);

并将此图片用于您的逻辑。