|
private static void parse(Path path) { |
|
|
|
BufferedImage image; |
|
|
|
try { |
|
image = ImageIO.read(path.toFile()); |
|
} catch (IOException e) { |
|
throw new RuntimeException("Failed to read an image by given path.", e); |
|
} |
|
|
|
String result = path.getFileName().toString() + " - "; |
|
|
|
for (int i = 0; i < 5; i++) { |
|
|
|
BufferedImage rankImage = cropImage(image, rankRegions[i]); |
|
BufferedImage suitImage = cropImage(image, suitRegions[i]); |
|
|
|
int background = image.getRGB(rankRegions[i][0][0], rankRegions[i][0][1]); |
|
|
|
if (background != -1 && background != -8882056) break; |
|
|
|
rankImage = clean(rankImage); |
|
suitImage = clean(suitImage); |
|
|
|
Optional<Feature> currentRank = match(rankImage, ranks); |
|
Optional<Feature> currentSuit = match(suitImage, suits); |
|
|
|
if (currentRank.isPresent()) result = result + currentRank.get().getName(); |
|
if (currentSuit.isPresent()) result = result + currentSuit.get().getName(); |
|
} |
|
|
|
System.out.println(result); |
|
} |
|
|
|
private static BufferedImage clean(BufferedImage image) { |
|
|
|
int width = image.getWidth(); |
|
int height = image.getHeight(); |
|
|
|
int white = Color.WHITE.getRGB(); |
|
int black = Color.BLACK.getRGB(); |
|
|
|
for (int row = 0; row < height; row++) { |
|
for (int col = 0; col < width; col++) { |
|
int color = image.getRGB(col, row); |
|
if (color == -8882056) { |
|
image.setRGB(col, row, white); |
|
} else if (color != -1) { |
|
image.setRGB(col, row, black); |
|
} |
|
} |
|
} |
|
|
|
return image; |
|
} |
|
|
|
private static BufferedImage cropImage(BufferedImage image, int[][] areaCoordinates) { |
|
int x1Point = areaCoordinates[0][0]; |
|
int y1Point = areaCoordinates[0][1]; |
|
|
|
int x2Point = areaCoordinates[1][0]; |
|
int y2Point = areaCoordinates[1][1]; |
|
|
|
return image.getSubimage(x1Point, y1Point, x2Point - x1Point + 1, y2Point - y1Point + 1); |
|
} |
|
|
|
private static Optional<Feature> match(BufferedImage sample, List<Feature> originals) { |
|
|
|
Optional<Feature> result = null; |
|
double limit = 100; |
|
|
|
for (int i = 0; i < originals.size(); i++) { |
|
Feature feature = originals.get(i); |
|
double difference = getDifference(sample, feature.getOriginal()); |
|
|
|
if (difference < limit) { |
|
limit = difference; |
|
result = Optional.of(feature); |
|
} |
|
} |
|
|
|
return result; |
|
} |
|
|
|
private static double getDifference(BufferedImage sample, BufferedImage original) { |
|
int originalWidth = original.getWidth(); |
|
int originalHeight = original.getHeight(); |
|
|
|
int sampleWidth = sample.getWidth(); |
|
int sampleHeight = sample.getHeight(); |
|
|
|
if (sampleWidth != originalWidth || sampleHeight != originalHeight) return 100; |
|
|
|
int totalPixels = originalHeight * originalWidth; |
|
int matchedPixels = 0; |
|
|
|
for (int row = 0; row < originalHeight; row++) { |
|
for (int col = 0; col < originalWidth; col++) { |
|
int originalColor = original.getRGB(col, row); |
|
int sampleColor = sample.getRGB(col, row); |
|
|
|
if (originalColor == sampleColor) matchedPixels++; |
|
} |
|
} |
|
|
|
return 100 - (((double) matchedPixels / (double) totalPixels) * 100); |
|
} |
|
} |