Ottenere dati pixel da un'immagine usando Java
Domanda
Sto cercando di ottenere i valori di pixel rgb da un'immagine bit 64 x 48
. Ottengo alcuni valori ma in nessun posto vicino ai valori 3072 (= 64 x 48)
che mi aspetto. Ottengo anche:
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)
Non riesco a trovare l'errore fuori limite ...
Ecco il codice:
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;
}
}
Soluzione
Questa:
for(int i = 0; i < img.getHeight(); i++){
for(int j = 0; j < img.getWidth(); j++){
rgb = getPixelData(img, i, j);
Non corrisponde a questo:
private static int[] getPixelData(BufferedImage img, int x, int y) {
Hai i
contando le righe e j
, ovvero i
contiene i valori y e j
contiene i valori x . È al contrario.
Altri suggerimenti
Anche questo funziona:
BufferedImage img = ImageIO.read(file);
int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
Stavo cercando questa stessa abilità. Non volevo enumerare l'intera immagine, quindi ho fatto qualche ricerca e ho usato 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();
Puoi usare int []
direttamente qui, i pixel sono in un formato dettato da ColorModel da pg.getColorModel ()
, oppure puoi cambiare quel falso in true e forzalo a essere RGB8-in-ints.
Da allora ho scoperto che anche le classi Raster
e Image possono farlo, e ci sono state alcune utili classi aggiunte in 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);
Esistono diversi metodi getPixels (...)
anche in Raster
.
int argb = img.getRGB (x, y);
il tuo codice
int argb = img.getRGB (y, x);
le mie modifiche ora funzionano
Devi cambiare:
for(int i = 0; i < img.getHeight(); i++){
for(int j = 0; j < img.getWidth(); j++){
rgb = getPixelData(img, i, j);
In
for(int i = 0; i < img.getWidth(); i++){
for(int j = 0; j < img.getHeight(); j++){
rgb = getPixelData(img, i, j);
Perché il secondo parametro da getPixelData
è il valore x
e il sesto è il valore y
. Hai cambiato i parametri.
Perché non usare solo usare:
public int[] getRGB(int startX,
int startY,
int w,
int h,
int[] rgbArray,
int offset,
int scansize)
È integrato, amico.