Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get color of each pixel of an image using BufferedImages

Tags:

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?

like image 809
user2410644 Avatar asked Mar 13 '14 21:03

user2410644


People also ask

How does each pixel get a color number?

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.

How do I change the pixel color of an image in Java?

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.

How do you set the color in pixels?

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.

How do you draw on BufferedImage?

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.


2 Answers

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

like image 195
Black Shadow Avatar answered Jan 25 '23 23:01

Black Shadow


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.

like image 42
Marco13 Avatar answered Jan 26 '23 00:01

Marco13