Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Loop through pixels in an image?

Tags:

I'm trying to find a way to make maps for my 2D java game, and I thought of one Idea in which I would loop through each pixel of an image and depends on what color the pixel it was that would be the tile to draw.

e.g. enter image description here

Is it possible to loop through an Images pixels? If it is, how?

Could you please supply me with some helpful links or code snippets?

like image 876
Duncan Palmer Avatar asked Oct 13 '11 05:10

Duncan Palmer


People also ask

How do I loop an image in pixels?

putImageData(imgData, 0, 0); The image will then change according to the changes you made to its pixel array. Each pixel contains 4 components red, green, blue, alpha - each of them is number 0-255. The loop starts from top-left to bottom-right.

How do I change the pixels of an image in Java?

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.

What is pixel in Java?

java.lang.Object Pixel public class Pixel extends java.lang.Object. Class that references a pixel in a picture. A pixel has an x and y location in a picture. A pixel knows how to get and set the red, green, blue, and alpha values in the picture. A pixel also knows how to get and set the color using a Color object.


2 Answers

Note that if you want to loop over all pixels in an image, make sure to make the outer loop over the y-coordinate, like so:

for (int y = 0; y < image.getHeight(); y++) {     for (int x = 0; x < image.getWidth(); x++) {           int  clr   = image.getRGB(x, y);            int  red   = (clr & 0x00ff0000) >> 16;           int  green = (clr & 0x0000ff00) >> 8;           int  blue  =  clr & 0x000000ff;           image.setRGB(x, y, clr);     } } 

This will likely make your code much faster, as you'll be accessing the image data in the order it's stored in memory. (As rows of pixels.)

like image 50
Zarkonnen Avatar answered Sep 24 '22 12:09

Zarkonnen


I think Pixelgrabber is what you looking for. If you have problems with the code please write a comment. Here is the link to the javadoc: [Pixelgrabber][1] and another short examples: [Get the color of a specific pixel][2], Java program to get the color of pixel

The following example is from the last link. Thanks to roseindia.net

import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException;  import javax.imageio.ImageIO;  public class ImageTest {     public static void main(final String args[])         throws IOException     {         final File file = new File("c:\\example.bmp");         final BufferedImage image = ImageIO.read(file);          for (int x = 0; x < image.getWidth(); x++) {             for (int y = 0; y < image.getHeight(); y++) {                 final int clr = image.getRGB(x, y);                 final int red = (clr & 0x00ff0000) >> 16;                 final int green = (clr & 0x0000ff00) >> 8;                 final int blue = clr & 0x000000ff;                  // Color Red get cordinates                 if (red == 255) {                     System.out.println(String.format("Coordinate %d %d", x, y));                 } else {                     System.out.println("Red Color value = " + red);                     System.out.println("Green Color value = " + green);                     System.out.println("Blue Color value = " + blue);                 }             }         }     } } 

[1]: https://docs.oracle.com/javase/7/docs/api/java/awt/image/PixelGrabber.html [2]: http://www.rgagnon.com/javadetails/java-0257.html

like image 39
Tobias Sarnow Avatar answered Sep 25 '22 12:09

Tobias Sarnow