Java的:从一个缓冲图像的RGBA作为整数数组 [英] Java: Get the RGBA from a Buffered Image as an Array of Integer

查看:236
本文介绍了Java的:从一个缓冲图像的RGBA作为整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定的图像文件,说PNG格式,如何我得到INT [R,G,B,A]再presenting位于行的像素I,j列?

Given an image file, say of PNG format, how to I get an array of int [r,g,b,a] representing the pixel located at row i, column j?

到目前为止,我开始在这里:

So far I am starting here:

private static int[][][] getPixels(BufferedImage image) {

    final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
    final int width = image.getWidth();
    final int height = image.getHeight();

    int[][][] result = new int[height][width][4];

    // SOLUTION GOES HERE....
}

在此先感谢!

推荐答案

您需要获得打包像素值作为 INT ,然后你可以使用的 颜色(INT ,布尔) 建立一个颜色对象,从中可以提取RGBA值,例如...

You need to get the packed pixel value as an int, you can then use Color(int, boolean) to build a color object from which you can extract the RGBA values, for example...

private static int[][][] getPixels(BufferedImage image) {
    int[][][] result = new int[height][width][4];
    for (int x = 0; x < image.getWidth(); x++) {
        for (int y = 0; y < image.getHeight(); y++) {
            Color c = new Color(image.getRGB(i, j), true);
            result[y][x][0] = c.getRed();
            result[y][x][1] = c.getGreen();
            result[y][x][2] = c.getBlue();
            result[y][x][3] = c.getAlpha();
        }
    }
}

这不是最有效的方法,但它是最简单的一种

It's not the most efficient method, but it is one of the simplest

这篇关于Java的:从一个缓冲图像的RGBA作为整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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