How to get the color of the image icon.png using java
Actually I have a servlet for that i wll be sending image using multipart file transfer, now the server should respond back the color of the icon file, Here the image file has a single color;
How to get the color of the image icon.png using java
Actually I have a servlet for that i wll be sending image using multipart file transfer, now the server should respond back the color of the icon file, Here the image file has a single color;
Assuming you have the path to the image file:
Color getImageColor(File imagePath) { BufferedImage image = ImageIO.read(imagePath); int color = image.getRGB(0, 0); for (int r = 0; r < image.getHeight(); r += 1) { for (int c = 0; c < image.getWidth(); c += 1) { if (image.getRGB(c, r) != color) { throw new IllegalArgumentException("Image: " + imagePath + " is not a solid color."); } } } return new Color(color); } This code assumes that the image really does only have a single color and pulls the first pixel only.
The loop is there to make sure the entire image is one color. There are many ways to handle that situation, of course.
return new Color(image.getRGB(0, 0))?You can loop the BufferedImage (two loops - one from 0 to width, and one from 0 to height), and get the call getRgb(x, y). Then count each different value. You can use a Map for that (key = color, value = number of occurences). Note that this will give you counts of how often each color occurs in the image.