|
package me.panjohnny; |
|
|
|
import javax.imageio.ImageIO; |
|
import java.awt.*; |
|
import java.awt.image.BufferedImage; |
|
import java.io.File; |
|
import java.io.IOException; |
|
import java.nio.file.DirectoryStream; |
|
import java.nio.file.Files; |
|
import java.nio.file.Path; |
|
import java.nio.file.Paths; |
|
|
|
public class UNOCardsNormalizer { |
|
public static final int MAX_PIXELS = 500; |
|
|
|
public static final int X = 300; |
|
public static final int Y = 300; |
|
|
|
public static final int DPI = 600; |
|
|
|
public static final int CANVAS_WIDTH_MM = 63; |
|
public static final int CANVAS_HEIGHT_MM = 88; |
|
public static final int GAP_MM = 3; |
|
|
|
public static void main(String[] args) { |
|
try(DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get("input/"))) { |
|
//BufferedImage backSide = ImageIO.read(Paths.get("input/BACK.png").toFile()); |
|
int index = 0; |
|
for (Path path : stream) { |
|
String newName = "lic-" + index + ".png"; |
|
index++; |
|
if (path.endsWith("BACK.png")) { |
|
newName="rub.png"; |
|
} |
|
System.out.println("Processing " + path.getFileName()); |
|
BufferedImage image = ImageIO.read(Files.newInputStream(path)); |
|
|
|
// Find out if the pixel at 110, 155 is red |
|
int pixel = image.getRGB(100, 155); |
|
|
|
// Red cards |
|
int top = 0; |
|
int left = 0; |
|
int right = 0; |
|
int bottom = 0; |
|
// Remove this part in case of not having stupid red card format where the card has black border around it |
|
if (isRed(pixel)) { |
|
// Calculate distance from top, left, right and bottom to the edge of first black pixel |
|
|
|
// Top |
|
for (; top < MAX_PIXELS; top++) { |
|
pixel = image.getRGB(X, top); |
|
if (isRed(pixel)) { |
|
break; |
|
} |
|
} |
|
|
|
// Left |
|
for (; left < MAX_PIXELS; left++) { |
|
pixel = image.getRGB(left, Y); |
|
if (isRed(pixel)) { |
|
break; |
|
} |
|
} |
|
|
|
// Right |
|
for (; right < MAX_PIXELS; right++) { |
|
pixel = image.getRGB(image.getWidth() - 1 - right, Y); |
|
if (isRed(pixel)) { |
|
break; |
|
} |
|
} |
|
|
|
// Bottom |
|
for (; bottom < MAX_PIXELS; bottom++) { |
|
pixel = image.getRGB(X, image.getHeight() - 1 - bottom); |
|
if (isRed(pixel)) { |
|
break; |
|
} |
|
} |
|
|
|
// Crop the card to the edges and save it |
|
image = image.getSubimage(left, top, image.getWidth() - left - right, image.getHeight() - top - bottom); |
|
|
|
// In the corners draw white triangles of set size |
|
Graphics2D g = image.createGraphics(); |
|
g.setColor(Color.WHITE); |
|
final int triangleSize = 60; |
|
g.fillPolygon(new int[]{0, 0, triangleSize}, new int[]{0, triangleSize, 0}, 3); |
|
g.fillPolygon(new int[]{0, 0, triangleSize}, new int[]{image.getHeight(), image.getHeight() - triangleSize, image.getHeight()}, 3); |
|
g.fillPolygon(new int[]{image.getWidth(), image.getWidth(), image.getWidth() - triangleSize}, new int[]{0, triangleSize, 0}, 3); |
|
g.fillPolygon(new int[]{image.getWidth(), image.getWidth(), image.getWidth() - triangleSize}, new int[]{image.getHeight(), image.getHeight() - triangleSize, image.getHeight()}, 3); |
|
g.dispose(); |
|
|
|
} else { |
|
// Other colors |
|
// Crop the card to the edges and save it (not white) |
|
|
|
// Top |
|
for (; top < MAX_PIXELS; top++) { |
|
pixel = image.getRGB(X, top); |
|
if (isNotWhite(pixel)) { |
|
break; |
|
} |
|
} |
|
|
|
// Left |
|
for (; left < MAX_PIXELS; left++) { |
|
pixel = image.getRGB(left, Y); |
|
if (isNotWhite(pixel)) { |
|
break; |
|
} |
|
} |
|
|
|
// Right |
|
for (; right < MAX_PIXELS; right++) { |
|
pixel = image.getRGB(image.getWidth() - 1 - right, Y); |
|
if (isNotWhite(pixel)) { |
|
break; |
|
} |
|
} |
|
|
|
// Bottom |
|
for (; bottom < MAX_PIXELS; bottom++) { |
|
pixel = image.getRGB(X, image.getHeight() - 1 - bottom); |
|
if (isNotWhite(pixel)) { |
|
break; |
|
} |
|
} |
|
|
|
// Crop the card to the edges and save it |
|
image = image.getSubimage(left, top, image.getWidth() - left - right, image.getHeight() - top - bottom); |
|
|
|
} |
|
image = resizeImage(image, CANVAS_WIDTH_MM, CANVAS_HEIGHT_MM, GAP_MM); |
|
ImageIO.write(image, "png", new File("output/" + newName)); |
|
} |
|
} catch (IOException e) { |
|
throw new RuntimeException(e); |
|
} |
|
} |
|
|
|
public static boolean isRed(int rgb) { |
|
int red = (rgb >> 16) & 0xff; |
|
int green = (rgb >> 8) & 0xff; |
|
int blue = rgb & 0xff; |
|
return red > 240 && green < 30 && blue < 30; |
|
} |
|
|
|
public static boolean isNotWhite(int rgb) { |
|
int red = (rgb >> 16) & 0xff; |
|
int green = (rgb >> 8) & 0xff; |
|
int blue = rgb & 0xff; |
|
return red <= 240 || green <= 240 || blue <= 240; |
|
} |
|
|
|
public static BufferedImage resizeImage(BufferedImage i, int w, int h, int gap) { |
|
BufferedImage resized = createImageInMm(w, h); |
|
Graphics2D g = resized.createGraphics(); |
|
gap = mmToPixels(gap); |
|
g.setColor(Color.WHITE); |
|
g.fillRect(0, 0, resized.getWidth(), resized.getHeight()); |
|
g.drawImage(i, gap, gap, resized.getWidth() - gap * 2, resized.getHeight()- gap*2, null); |
|
g.dispose(); |
|
return resized; |
|
} |
|
|
|
public static BufferedImage createImageInMm(int widthMm, int heightMm) { |
|
// Create the BufferedImage |
|
return new BufferedImage(mmToPixels(widthMm), mmToPixels(heightMm), BufferedImage.TYPE_INT_ARGB); |
|
} |
|
|
|
public static int mmToPixels(int mm) { |
|
return (int) Math.round(mm / 25.4 * DPI); |
|
} |
|
} |