从图像中获取像素数组我正在寻找获取像素数据的最快方法(在表单中)int[][])从BufferedImage..我的目标是能够定位像素(x, y)从图像中使用int[x][y]..我发现的所有方法都不会这样做(大多数方法都会返回)。int[]s)。
3 回答
HUX布斯
TA贡献1876条经验 获得超6个赞
int[][] pixels = new int[w][h];for( int i = 0; i < w; i++ ) for( int j = 0; j < h; j++ ) pixels[i][j] = img.getRGB( i, j );
蝴蝶刀刀
TA贡献1801条经验 获得超8个赞
我已经将代码包装在一个方便的类中,该类在构造函数中接受BufferedImage,并公开了一个等效的getRBG(x,y)方法,该方法减少了使用BufferedImage.getRGB(x,y)替换代码的次数。
import java.awt.image.BufferedImage;import java.awt.image.DataBufferByte;public class FastRGB{
private int width;
private int height;
private boolean hasAlphaChannel;
private int pixelLength;
private byte[] pixels;
FastRGB(BufferedImage image)
{
pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
width = image.getWidth();
height = image.getHeight();
hasAlphaChannel = image.getAlphaRaster() != null;
pixelLength = 3;
if (hasAlphaChannel)
{
pixelLength = 4;
}
}
int getRGB(int x, int y)
{
int pos = (y * pixelLength * width) + (x * pixelLength);
int argb = -16777216; // 255 alpha
if (hasAlphaChannel)
{
argb = (((int) pixels[pos++] & 0xff) << 24); // alpha
}
argb += ((int) pixels[pos++] & 0xff); // blue
argb += (((int) pixels[pos++] & 0xff) << 8); // green
argb += (((int) pixels[pos++] & 0xff) << 16); // red
return argb;
}}添加回答
举报
0/150
提交
取消
