Boa tarde,
Gostaria de saber como posso fazer para pegar uma determinada imagem, e deixa-la com seus lados iguais sem distorcer a imagem?
Exemplo, pego uma imagem 1600x1200 e faço ela ficar 800x800, sem distorcer.
Obrigado
Vinícius
Boa tarde,
Gostaria de saber como posso fazer para pegar uma determinada imagem, e deixa-la com seus lados iguais sem distorcer a imagem?
Exemplo, pego uma imagem 1600x1200 e faço ela ficar 800x800, sem distorcer.
Obrigado
Vinícius
Ola,
Vc pode usar uma regra de tres, clássica, assim:
ImageIcon image = //pega a sua imagem...
ImageIcon icon = new ImageIcon(image);
double width = ((double)icon.getIconWidth() * 100) / 800;
double height = ((double)icon.getIconHeight() * 100) / 800;
ImageIcon background = new ImageIcon(image.getScaledInstance((int) width, (int) height, 1));
Oi BrunoCarlo,
Para redimencionar estou fazendo certo, a questão é deixar os lados iguais após redimencionar.
Pensei em ver qual dos lados é o menor e cortar um pedaço do lado maior para ficar igual, assim não distorceria a imagem.
Abaixo está a classe que estou usando para redimencionar a imagem.
public class ArquivoMini {
public static final int NAOFOTO = 0;
public static final int SIMFOTO = 1;
private static String imagesDir = "c:/tmp/";
public static void main(String[] args) {
new ArquivoMini().iniciar("loira2.jpg", imagesDir, ArquivoMini.NAOFOTO);
}
/**
* Inicia a manipulação da imagem
* Parametros: arquivo, diretorio onde será gravado a imagem
* e um inteiro dizendo se é uma foto ou não,
* 1 é foto e 0 não é foto...
*
*/
public void iniciar(String arquivo, String diretorio, int foto) {
int width = 115; // Lagura da miniatura
int height = 115; // Altuta da miniatura
int quality = 80; // Qualidade da imagem [0~100]
File file = new File(diretorio + arquivo);
try {
Image image = new ImageIcon(file.toURL()).getImage();
if (foto == ArquivoMini.NAOFOTO) {
if (image.getWidth(null) > 640) {
width = 640;
} else {
width = image.getWidth(null);
}
if (image.getHeight(null) > 480) {
height = 480;
} else {
height = image.getHeight(null);
}
}
redimensionar(image, width, height, quality, file.getName(), diretorio);
System.out.println("A imagem " + file.getName() + " foi redimensionada com sucesso");
} catch (MalformedURLException e) {
System.out.println("MalformedURLException " + e.getMessage());
}
}
// Método para redimensionar imagens (criar thubmnails)
private void redimensionar(Image image, int width, int height, int quality, String nomeImagem, String diretorio) {
// Calculos necessários para manter as proporções da imagem, conhecido
// como "aspect ratio"
double thumbRatio = (double) width / (double) height;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double) imageWidth / (double) imageHeight;
if (thumbRatio < imageRatio) {
height = (int) (width / imageRatio);
} else {
width = (int) (height * imageRatio);
}
// Fim do cálculo
BufferedImage thumbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, width, height, null);
BufferedOutputStream out;
try {
out = new BufferedOutputStream(new FileOutputStream(diretorio + nomeImagem));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float) quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
out.close();
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException " + e.getMessage());
} catch (ImageFormatException e) {
System.out.println("ImageFormatException " + e.getMessage());
} catch (IOException e) {
System.out.println("IOException " + e.getMessage());
}
}
}
Bom dia,
Resolvi o problema…
BufferedImage cropImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
cropImage = cropImage.getSubimage(x, y, width, height);
Basta usar o metodo getSubimage da classe BufferedImage passando as cordenadas X e Y a largura e a altura da imagem.
Valeu pela força…
Vinícius