Created
April 15, 2020 10:43
-
-
Save olegchir/a8776816f2056b1ac450831571266d1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.olegchir.bpmscan; | |
import javax.imageio.ImageIO; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.*; | |
import java.util.List; | |
public class Main { | |
public static void main(String[] args) { | |
String infile = "C:\\temp\\sunset.bmp"; | |
String outfile = "C:\\temp\\results2.txt"; | |
BufferedImage bmp = null; | |
try { | |
File file = new File(infile); | |
System.out.println(file.exists() ? "File exists" : "File not found"); | |
bmp = ImageIO.read(file); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return; | |
} | |
HashMap<Integer, Integer> counters = new HashMap<>(); | |
List<Integer> colorList = new ArrayList<>(); | |
try (PrintWriter pw = new PrintWriter(new FileWriter(outfile))) { | |
for (int y = 0; y < bmp.getHeight(); y++) { | |
Color color = new Color(bmp.getRGB(640, y)); | |
pw.printf("%d -> [%d, %d, %d]\n", y, color.getRed(), color.getGreen(), color.getBlue()); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main2(String[] args) { | |
String infile = "C:\\temp\\sunset.bmp"; | |
String outfile = "C:\\temp\\results.txt"; | |
BufferedImage bmp = null; | |
try { | |
File file = new File(infile); | |
System.out.println(file.exists() ? "File exists" : "File not found"); | |
bmp = ImageIO.read(file); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return; | |
} | |
HashMap<Integer, Integer> counters = new HashMap<>(); | |
for (int x = 0; x < bmp.getWidth(); x++) { | |
for (int y = 0; y < bmp.getHeight(); y++) { | |
Integer rgb = bmp.getRGB(x, y); | |
Integer oldValue = counters.get(rgb); | |
if (null == oldValue) { | |
counters.put(rgb, 1); | |
} else { | |
counters.put(rgb, ++oldValue); | |
} | |
} | |
} | |
Map<Integer, Integer> sortedCounters = sortByComparator(counters, false); | |
int allPixels = 0; | |
File outf = new File(outfile); | |
if (outf.exists()) { | |
boolean delete = outf.delete(); | |
} | |
try (PrintWriter pw = new PrintWriter(new FileWriter(outfile))) { | |
for (Map.Entry<Integer, Integer> entry : sortedCounters.entrySet()) { | |
Color color = new Color(entry.getKey()); | |
int num = entry.getValue(); | |
pw.printf("[%d, %d, %d] -> %d\n", color.getRed(), color.getGreen(), color.getBlue(), num); | |
allPixels += num; | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
System.out.println(allPixels); | |
} | |
private static Map<Integer, Integer> sortByComparator(Map<Integer, Integer> unsortMap, final boolean order) { | |
List<Map.Entry<Integer, Integer>> list = new LinkedList<>(unsortMap.entrySet()); | |
// Sorting the list based on values | |
Collections.sort(list, (o1, o2) -> { | |
if (order) { | |
return o1.getValue().compareTo(o2.getValue()); | |
} else { | |
return o2.getValue().compareTo(o1.getValue()); | |
} | |
}); | |
Map<Integer, Integer> sortedMap = new LinkedHashMap<>(); | |
for (Map.Entry<Integer, Integer> entry : list) { | |
sortedMap.put(entry.getKey(), entry.getValue()); | |
} | |
return sortedMap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment