Get the rgb value of an image in java
331 views
An example application i wrote that displays the rgb value of the current selected pixel. The demo doesn't have a gui and works from the command line. The image file is static but it can be changed easily.
JAVA:
-
import javax.imageio.ImageIO;
-
-
import java.awt.Color;
-
import java.awt.image.BufferedImage;
-
import java.io.File;
-
import java.io.IOException;
-
import java.util.Scanner;
-
-
// I rewrote it from a tutorial here: http://www.rgagnon.com/javadetails/java-0257.html
-
// Ark - www.engineeringserver.com , the application shows the RGB value from an X,Y coordinate of an image
-
// this version reads the user input for the X Y coordinates and shows the width and height of the image.
-
-
public class PixelColor {
-
PixelColor PC = new PixelColor();
-
PC.getImageInfo();
-
}
-
-
public void getImageInfo(){
-
BufferedImage image;
-
try {
-
-
image = ImageIO.read(file);
-
int c = image.getRGB(x,y);
-
int red = (c & 0x00ff0000)>> 16;
-
int green = (c & 0x0000ff00)>> 8;
-
int blue = c & 0x000000ff;
-
-
PixelColor PC = new PixelColor();
-
PC.getImageInfo();
-
PixelColor PC = new PixelColor();
-
PC.getImageInfo();
-
}
-
PixelColor PC = new PixelColor();
-
PC.getImageInfo();
-
}
-
}
-
}
Output
Get X,Y coordinate a from image: 10, 2
Your input doesn't match.. example: 15,20
Get X,Y coordinate a from image: 10,3
Pixel value at X:10 Y:3
R:51 G:56 B:62
Width of Image:598 Height of Image:500












