I have a folder with many images with different backgrounds. I have got requirement to sort these images on the basis of background color.
Can I make a java program to read the folder and each image file in there, and decide the image of each file? please开发者_开发百科 share options.
Yes, it is possible. You can load images with ImageIO.
BufferedImage img = ImageIO.read(imageFile);
int rgb = img.getRGB(x,y);
Color color = new Color(rgb);
But you have to create an algorithm that finds out which color is the backround color. It depends on the kind of images.
So, not knowing what your images really look like, you may want to average as much of the background as you can to come up with a good representation of the background color.
I would consider a couple things:
* Read in the pixels of each of the four edges. If there's little variance in the pixel color, then you may be done, just take the average.
* Do the same, but also read in lines from the edge to the middle until you hit a pixel that has a rather different color than your running average. Do this for all edges.
Those would be the cheapest things that I can think of to cover variances in background color. Depending on the images you're working with, you may have to get fancier.
A BufferedImage should get you your image data.
Mark
精彩评论