Created
November 19, 2023 23:54
-
-
Save rodrigocananea/85a6b1b47040f4db0bd10dcde66ff1b1 to your computer and use it in GitHub Desktop.
Reconhecer conteúdo de imagem e forçar uma margem de 30px ao redor, recortando o conteúdo excedente da margem
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
/** | |
* | |
* @author Rodrigo Cananea <rodrigoaguiar35@gmail.com> | |
*/ | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import javax.imageio.ImageIO; | |
public class ImageBorderDetector { | |
public static void main(String[] args) throws IOException { | |
// Carregar a imagem | |
BufferedImage originalImage = ImageIO.read(new File("D:\\teste2\\nfse.jpg")); | |
// Encontrar as bordas da área colorida | |
Rectangle coloredArea = findColoredArea(originalImage); | |
// Adicionar margem | |
BufferedImage imageWithMargin = addMargin(originalImage, coloredArea, 30); // 30 pixel | |
// Salvar a nova imagem | |
ImageIO.write(imageWithMargin, "jpg", new File("D:\\teste2\\nfse-margin.jpg")); | |
} | |
private static Rectangle findColoredArea(BufferedImage image) { | |
int width = image.getWidth(); | |
int height = image.getHeight(); | |
int top = -1, left = -1, right = -1, bottom = -1; | |
// Procurar do topo para baixo | |
outerloop: | |
for (int y = 0; y < height; y++) { | |
for (int x = 0; x < width; x++) { | |
if (!isWhite(image.getRGB(x, y))) { | |
top = y; | |
break outerloop; | |
} | |
} | |
} | |
// Procurar da esquerda para a direita | |
outerloop: | |
for (int x = 0; x < width; x++) { | |
for (int y = 0; y < height; y++) { | |
if (!isWhite(image.getRGB(x, y))) { | |
left = x; | |
break outerloop; | |
} | |
} | |
} | |
// Procurar da direita para a esquerda | |
outerloop: | |
for (int x = width - 1; x >= 0; x--) { | |
for (int y = 0; y < height; y++) { | |
if (!isWhite(image.getRGB(x, y))) { | |
right = x; | |
break outerloop; | |
} | |
} | |
} | |
// Procurar de baixo para cima | |
outerloop: | |
for (int y = height - 1; y >= 0; y--) { | |
for (int x = 0; x < width; x++) { | |
if (!isWhite(image.getRGB(x, y))) { | |
bottom = y; | |
break outerloop; | |
} | |
} | |
} | |
return new Rectangle(left, top, right - left + 1, bottom - top + 1); | |
} | |
private static boolean isWhite(int rgb) { | |
Color color = new Color(rgb); | |
return color.getRed() > 200 && color.getGreen() > 200 && color.getBlue() > 200; | |
} | |
private static BufferedImage addMargin(BufferedImage originalImage, Rectangle area, int marginSize) { | |
// Calculando as novas dimensões com margem | |
int newWidth = area.width + 2 * marginSize; | |
int newHeight = area.height + 2 * marginSize; | |
BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); | |
Graphics g = newImage.getGraphics(); | |
// Preencher com branco | |
g.setColor(Color.WHITE); | |
g.fillRect(0, 0, newWidth, newHeight); | |
// Desenhar a área colorida original com a margem | |
g.drawImage(originalImage, marginSize - area.x, marginSize - area.y, originalImage.getWidth(), originalImage.getHeight(), null); | |
g.dispose(); | |
return newImage; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment