문제
나는 픽셀 RGB 값을 얻으려고 노력하고 있습니다. 64 x 48
비트 이미지. 나는 약간의 가치를 얻지 만 근처에있는 곳은 없습니다 3072 (= 64 x 48)
내가 기대하는 가치. 나는 또한 얻는다 :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:301)
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:871)
at imagetesting.Main.getPixelData(Main.java:45)
at imagetesting.Main.main(Main.java:27)
바운드 오류를 찾을 수 없습니다 ...
코드는 다음과 같습니다.
package imagetesting;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.image.BufferedImage;
public class Main {
public static final String IMG = "matty.jpg";
public static void main(String[] args) {
BufferedImage img;
try {
img = ImageIO.read(new File(IMG));
int[][] pixelData = new int[img.getHeight() * img.getWidth()][3];
int[] rgb;
int counter = 0;
for(int i = 0; i < img.getHeight(); i++){
for(int j = 0; j < img.getWidth(); j++){
rgb = getPixelData(img, i, j);
for(int k = 0; k < rgb.length; k++){
pixelData[counter][k] = rgb[k];
}
counter++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static int[] getPixelData(BufferedImage img, int x, int y) {
int argb = img.getRGB(x, y);
int rgb[] = new int[] {
(argb >> 16) & 0xff, //red
(argb >> 8) & 0xff, //green
(argb ) & 0xff //blue
};
System.out.println("rgb: " + rgb[0] + " " + rgb[1] + " " + rgb[2]);
return rgb;
}
}
해결책
이것:
for(int i = 0; i < img.getHeight(); i++){
for(int j = 0; j < img.getWidth(); j++){
rgb = getPixelData(img, i, j);
이것과 일치하지 않습니다.
private static int[] getPixelData(BufferedImage img, int x, int y) {
당신은 가지고 있습니다 i
줄을 세고 j
열, 즉 i
포함 와이 가치와 j
포함 엑스 가치. 그것은 거꾸로됩니다.
다른 팁
이것도 작동합니다 :
BufferedImage img = ImageIO.read(file);
int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
나는이 같은 능력을 찾고 있었다. 전체 이미지를 열거하고 싶지 않았으므로 검색을 수행하고 Pixelgrabber를 사용했습니다.
Image img = Toolkit.getDefaultToolkit().createImage(filename);
PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, false);
pg.grabPixels(); // Throws InterruptedException
width = pg.getWidth();
height = pg.getHeight();
int[] pixels = (int[])pg.getPixels();
당신은 사용할 수 있습니다 int[]
바로 여기에서 픽셀은 Colormodel에 의해 지시 된 형식입니다. pg.getColorModel()
, 또는 그 거짓을 true로 변경하여 RGB8-in-ints로 강제 할 수 있습니다.
나는 그 이후로 그것을 발견했다 Raster
그리고 이미지 클래스 도이 작업을 수행 할 수 있으며 유용한 클래스가 추가되었습니다. javax.imageio.*
.
BufferedImage img = ImageIO.read(new File(filename)); // Throws IOException
int[] pixels = img.getRGB(0,0, img.getWidth(), img.getHeight, null, 0, img.getWidth());
// also available through the BufferedImage's Raster, in multiple formats.
Raster r = img.getData();
int[] pixels = r.getPixels(0,0,r.getWidth(), r.getHeight(), (int[])null);
몇 가지가 있습니다 getPixels(...)
방법 Raster
또한.
int argb = img.getRGB(x, y);
코드
int argb = img.getRGB(y, x);
내 변화가 이제 작동합니다
변경해야합니다.
for(int i = 0; i < img.getHeight(); i++){
for(int j = 0; j < img.getWidth(); j++){
rgb = getPixelData(img, i, j);
안으로
for(int i = 0; i < img.getWidth(); i++){
for(int j = 0; j < img.getHeight(); j++){
rgb = getPixelData(img, i, j);
두 번째 매개 변수는 getPixelData
입니다 x
-값과 3은입니다 y
-값. 매개 변수를 전환했습니다.
사용하지 않은 이유는 다음과 같습니다.
public int[] getRGB(int startX,
int startY,
int w,
int h,
int[] rgbArray,
int offset,
int scansize)
내장되어 있습니다.