I am trying to get every single color of every single pixel of an image. My idea was following:
int[] pixels; BufferedImage image; image = ImageIO.read(this.getClass.getResources("image.png"); int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
Is that right? I can't even check what the "pixels" array contains, because i get following error:
java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt
I just would like to receive the color of every pixel in an array, how do i achieve that?
A digital color image pixel is just numbers representing a RGB data value (Red, Green, Blue). Each pixel's color sample has three numerical RGB components (Red, Green, Blue) to represent the color of that tiny pixel area. These three RGB components are three 8-bit numbers for each pixel.
Create a Color object bypassing the new RGB values as parameters. Get the pixel value from the color object using the getRGB() method of the Color class. Set the new pixel value to the image by passing the x and y positions along with the new pixel value to the setRGB() method.
Once your phone finishes rebooting, you will find the new colors in Settings –> Display –> Styles & Wallpaper. Select "Custom 1" and choose a font and icon style. When you get to colors, you will see the new set of colors you flashed.
To Draw on a BufferedImage To draw on anything, you need a Graphics or Graphics2D drawing context. You can get a graphics context from an image like this: Graphics2D gp = myImage. createGraphics(); gp.
import java.io.*; import java.awt.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; public class GetPixelColor { public static void main(String args[]) throws IOException { File file = new File("your_file.jpg"); BufferedImage image = ImageIO.read(file); // Getting pixel color by position x and y int clr = image.getRGB(x, y); int red = (clr & 0x00ff0000) >> 16; int green = (clr & 0x0000ff00) >> 8; int blue = clr & 0x000000ff; System.out.println("Red Color value = " + red); System.out.println("Green Color value = " + green); System.out.println("Blue Color value = " + blue); } }
of course you have to add a for loop for all pixels
The problem (also with the answer that was linked from the first answer) is that you hardly ever know what exact type your buffered image will be after reading it with ImageIO. It could contain a DataBufferByte
or a DataBufferInt
. You may deduce it in some cases via BufferedImage#getType()
, but in the worst case, it has type TYPE_CUSTOM
, and then you can only fall back to some instanceof
tests.
However, you can convert your image into a BufferedImage that is guaranteed to have a DataBufferInt
with ARGB values - namely with something like
public static BufferedImage convertToARGB(BufferedImage image) { BufferedImage newImage = new BufferedImage( image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = newImage.createGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return newImage; }
Otherwise, you can call image.getRGB(x,y)
, which may perform the required conversions on the fly.
BTW: Note that obtaining the data buffer of a BufferedImage may degrade painting performance, because the image can no longer be "managed" and kept in VRAM internally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With