See More

package photorainbow; import java.lang.Integer; import java.util.Map; import java.util.HashMap; import java.util.List; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; public class ImageColorData { private static final Logger log = Logger.getLogger(ImageColorData.class.getName()); private int imageWidth = 0; private int imageHeight = 0; private float rangeValue = 0.0f; private HashMap _brightnessColorDict = new HashMap(); private HashMap> _colorByPixel = new HashMap>(); private HashMap imgVIBGYORHueDiffDict = new HashMap(); private HashMap> imageDataDictSorted = new HashMap>(); public HashMap getBrightnessColorDict() { return this._brightnessColorDict; } public HashMap> getColorByPixel() { return this._colorByPixel; } //Ayesha Logic... public HashMap getSortedImagesByColor(String color) { HashMap dicImgHue = new HashMap(); HashMap orderedItems = ImageColorSorter.sortByColorValue(imageDataDictSorted); Iterator> entries = orderedItems.entrySet().iterator(); while(entries.hasNext()) { Map.Entry entry = entries.next(); dicImgHue.add(entry.getKey(), entry.getValue()); } return dicImgHue; } public HashMap> getSortedImagesByRainbow() { HashMap> dicImgRainbowHue = new HashMap>(); List colors = new ArrayList(); colors.add(Color.VIOLET); colors.add(Color.INDIGO); colors.add(Color.GREEN); colors.add(Color.YELLOW); HashMap orderedItems = ImageColorSorter.sortByColorValue(imageDataDictSorted, colors); Iterator> entries = orderedItems.entrySet().iterator(); while(entries.hasNext()) { Map.Entry entry = entries.next(); dicImgRainbowHue.add(entry.getKey(), entry.getValue()); } return dicImgRainbowHue; } private boolean InRange(int min, int max, operand) { return (operand >= min && operand <= max) ? true : false; } public HashMap> getColorsInImage(Bitmap imageAsBitmap) { int xCoord = 0, yCoord = 0; float temp = 0; this.imageWidth = imageAsBitmap.getWidth(); this.imageHeight = imageAsBitmap.getHeight(); try { for (xCoord = 0; xCoord < this.imageWidth; xCoord++) { for (yCoord = 0; yCoord < this.imageHeight; yCoord++) { List imgVIBGYORHueDiff = new List(); Color imgPixelColor = imageAsBitmap.getPixel(xCoord, yCoord); float pixelColorHue = imgPixelColor.getHue(); temp = pixelColorHue; rangeValue = temp; if (!imgVIBGYORHueDiffDict.containsKey(rangeValue)) { if (InRange(300, 360, Integer.valueOf(rangeValue))) { imgVIBGYORHueDiffDict.add(rangeValue, "Violet"); break; } else if (InRange(275, 299, Integer.valueOf(rangeValue))) { imgVIBGYORHueDiffDict.add(rangeValue, "Indigo"); continue; } else if (InRange(240, 274,Integer.valueOf(rangeValue))) { imgVIBGYORHueDiffDict.add(rangeValue, "Blue"); continue; } else if (InRange(120, 239,Integer.valueOf(rangeValue))) { imgVIBGYORHueDiffDict.add(rangeValue, "Green"); continue; } else if (InRange(60, 119, Integer.valueOf(rangeValue))) { imgVIBGYORHueDiffDict.add(rangeValue, "Yellow"); continue; } else if (InRange(40, 59, Integer.valueOf(rangeValue))) { imgVIBGYORHueDiffDict.add(rangeValue, "Orange"); continue; } else if (InRange(0, 39, Integer.valueOf(rangeValue))) { imgVIBGYORHueDiffDict.add(rangeValue, "Red"); continue; } } for (float hueDiff : imgVIBGYORHueDiffDict.getKeys()) { imgVIBGYORHueDiff.add(hueDiff); } float closestPixelColorByHue = imgVIBGYORHueDiff.getMinimum(); if (_colorByPixel.containsKey(imgVIBGYORHueDiffDict[closestPixelColorByHue])) { _colorByPixel[imgVIBGYORHueDiffDict[closestPixelColorByHue]].add(imgPixelColor); _brightnessColorDict[imgVIBGYORHueDiffDict[closestPixelColorByHue]] += imgPixelColor.GetBrightness(); } else { List pixelColorStructure = new List(); pixelColorStructure.add(imgPixelColor); _colorByPixel.add(imgVIBGYORHueDiffDict[closestPixelColorByHue], pixelColorStructure); _brightnessColorDict.add(imgVIBGYORHueDiffDict[closestPixelColorByHue], imgPixelColor.GetBrightness()); } } } } catch (Exception e) { log.log(Level.ERROR, e.printStackTrace()); } return _colorByPixel; } public HashMap>> GetColorData2(Image imgObj) { _colorByPixel = getColorsInImage(imgObj.Img); HashMap>> imageDataDict = new HashMap>>(); HashMap colPercentageDict = new HashMap(); List> imgInfoDictList = new List>(); colPercentageDict.add("Violet", percentageOfColorInImage("Violet")); colPercentageDict.add("Indigo", percentageOfColorInImage("Indigo")); colPercentageDict.add("Blue", percentageOfColorInImage("Blue")); colPercentageDict.add("Green", percentageOfColorInImage("Green")); colPercentageDict.add("Yellow", percentageOfColorInImage("Yellow")); colPercentageDict.add("Orange", percentageOfColorInImage("Orange")); colPercentageDict.add("Red", percentageOfColorInImage("Red")); imgInfoDictList.add(colPercentageDict); HashMap colAvgBrightnessDict = new HashMap(); colAvgBrightnessDict.add("Violet", calcAverageBrightnessByColor("Violet")); colAvgBrightnessDict.add("Indigo", calcAverageBrightnessByColor("Indigo")); colAvgBrightnessDict.add("Blue", calcAverageBrightnessByColor("Blue")); colAvgBrightnessDict.add("Green", calcAverageBrightnessByColor("Green")); colAvgBrightnessDict.add("Yellow", calcAverageBrightnessByColor("Yellow")); colAvgBrightnessDict.add("Orange", calcAverageBrightnessByColor("Orange")); colAvgBrightnessDict.add("Red", calcAverageBrightnessByColor("Red")); imgInfoDictList.add(colAvgBrightnessDict); imageDataDict.add(imgObj.Img, imgInfoDictList); imageDataDictSorted.add(imgObj, colPercentageDict); return imageDataDict; } public float percentageOfColorInImage(String colorName) { float percentageOfColor = 0; if(_colorByPixel.containsKey(colorName)) { float numberofPixelsByColor = _colorByPixel[colorName].Length; float totalPixelsInImage = imageWidth * imageHeight; percentageOfColor = (numberofPixelsByColor / totalPixelsInImage) * 100; return percentageOfColor; } else { return percentageOfColor; } } public float calcAverageBrightnessByColor(String colorName) { float averageBrightnessByColor = 0; if(_colorByPixel.containsKey(colorName)) { int numberOfPixelsByColor = _colorByPixel[colorName].Length; averageBrightnessByColor = _brightnessColorDict[colorName] / numberOfPixelsByColor; return averageBrightnessByColor; } else { return averageBrightnessByColor; } } private static float adjustHue(float temp) { if (temp < 0) { temp = temp * (-1); return temp; } else { return temp; } } }