Java - Como obtener el color de cada pixel de una imagen

 
Vista:

Como obtener el color de cada pixel de una imagen

Publicado por Ernesto Rivero (7 intervenciones) el 11/08/2006 05:14:56
Nesecito una forma de saber cual es el color de cada pixel de una imagen cualquiera si alguien tiene la forma de hacerlo me ser[ia de gran ayuda............ :-)
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Como obtener el color de cada pixel de una imag

Publicado por Ernesto Rivero (7 intervenciones) el 14/08/2006 20:17:41
Me sirve igual en otro lengueje de programación
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Como obtener el color de cada pixel de una imag

Publicado por rbp_toledo (12 intervenciones) el 15/08/2006 23:46:58
Hola! Mira, en Java sería una cosa así:

int R=0;
int G=0;
int B=0;
BufferedImage bufferPlano = toBufferedImage(nombreDeTuImagen);

/*Recorro la imagen y voy mirando cada pixel*/


for(int i=0;i<pixelesAlto-1;i++){
for(int j=0;j<pixelesLargo-1;j++){

try{

R = bufferPlano.getColorModel().getRed(bufferPlano.getRGB(i,j));
G = bufferPlano.getColorModel().getGreen(bufferPlano.getRGB(i,j));
B = bufferPlano.getColorModel().getBlue(bufferPlano.getRGB(i,j));
}

}catch(java.lang.ArrayIndexOutOfBoundsException e){}
catch(java.lang.NullPointerException e){}

}//Fin del for para recorrer pixeles
}//Fin del for para recorrer pixeles

Dentro del try, cuando cojas el valor de los colores haces con ellos lo que quieras. Espero que te sirva, a mí me funciona perfectamente.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Como obtener el color de cada pixel de una imag

Publicado por rbp_toledo (12 intervenciones) el 16/08/2006 00:06:53
Copia estos métodos también, los dejas tal cual

/*COPIADO Y PEGADO DE: http://javaalmanac.com/egs/java.awt.image/pkg.html*/
public static BufferedImage toBufferedImage(Image image) {
BufferedImage bimage = null;
try{
if (image instanceof BufferedImage) {
return (BufferedImage)image;
}

// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();

// Determine if the image has transparent pixels; for this method's
// implementation, see e661 Determining If an Image Has Transparent Pixels
boolean hasAlpha = hasAlpha(image);

// Create a buffered image with a format that's compatible with the screen

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
// Determine the type of transparency of the new buffered image
int transparency = Transparency.OPAQUE;
if (hasAlpha) {
transparency = Transparency.BITMASK;
}

// Create the buffered image
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(
image.getWidth(null), image.getHeight(null), transparency);
} catch (HeadlessException e) {
// The system does not have a screen
}

if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
if (hasAlpha) {
type = BufferedImage.TYPE_INT_ARGB;
}
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}

// Copy image to buffered image
Graphics g = bimage.createGraphics();

// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}catch(java.lang.NullPointerException e){}
return bimage;
}
// This method returns true if the specified image has transparent pixels
/*COPIADO Y PEGADO DE: http://javaalmanac.com/egs/java.awt.image/pkg.html*/
public static boolean hasAlpha(Image image) {
// If buffered image, the color model is readily available
if (image instanceof BufferedImage) {
BufferedImage bimage = (BufferedImage)image;
return bimage.getColorModel().hasAlpha();
}

// Use a pixel grabber to retrieve the image's color model;
// grabbing a single pixel is usually sufficient
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
try {
pg.grabPixels();
} catch (InterruptedException e) {
}

// Get the image's color model
ColorModel cm = pg.getColorModel();
return cm.hasAlpha();
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar