从图像中获取像素数组

从图像中获取像素数组

我正在寻找获取像素数据的最快方法(在表单中)int[][])从BufferedImage..我的目标是能够定位像素(x, y)从图像中使用int[x][y]..我发现的所有方法都不会这样做(大多数方法都会返回)。int[]s)。



子衿沉夜
浏览 889回答 3
3回答

HUX布斯

像这样吗?int[][]&nbsp;pixels&nbsp;=&nbsp;new&nbsp;int[w][h];for(&nbsp;int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;w;&nbsp;i++&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;for(&nbsp;int&nbsp;j&nbsp;=&nbsp;0;&nbsp;j&nbsp;<&nbsp;h;&nbsp;j++&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pixels[i][j]&nbsp;=&nbsp;img.getRGB(&nbsp;i,&nbsp;j&nbsp;);

蝴蝶刀刀

我已经将代码包装在一个方便的类中,该类在构造函数中接受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;     }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java