Pessoal, fiz o seguinte código para tirar bordas brancas de imagens JPG.
/**
* Método responsável por retirar as bordas brancas da imagem.
* @param bi
* @return cropped buffered image.
*/
public static BufferedImage autoRecortarImagem(BufferedImage bi){
int w = bi.getWidth();
int h = bi.getHeight();
int RGB = 0;
int upperLeftX = 0;
int upperLeftY = 0;
int bottonX = 0;
int bottonY = 0;
for(int posY = 0;posY<h;posY++){
for(int posX = 0;posX<w;posX++){
RGB = bi.getRGB(posX,posY);
Color color = new Color(RGB);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
//if(red != 0 && green != 0 && blue != 0){
if(red<240 && green < 240 && blue < 240){
if(upperLeftX == 0 || upperLeftX > posX){
upperLeftX = posX;
}
if(upperLeftY == 0 ||upperLeftY > posY){
upperLeftY = posY;
}
if(bottonX <= w && bottonX < posX){
bottonX = posX;
}
if(bottonY <=h && bottonY < posY){
bottonY = posY;
}
}
//}
}
}
int newX = upperLeftX;
int newY = upperLeftY;
int newWidth = bottonX - upperLeftX;
int newHeight = bottonY - upperLeftY;
BufferedImage cropedBi = bi.getSubimage(newX, newY, newWidth, newHeight);
return cropedBi;
}
Gostaria de saber se alguém aqui já precisou fazer isto ou se alguém pode me ajudar a aumentar o desempenho. Atualmente ele só corta quando o background é branco, mas gostaria de colocar ele para qualquer tipo de background.
Abraços.