public void redimencionarImagem(Image image, int width, int height, int quality, String nomeImagem) {
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.setBackground(java.awt.Color.white);
graphics2D.drawImage(image, 0, 0, width, height, null);
BufferedOutputStream out;
try {
out = new BufferedOutputStream(new FileOutputStream("C:\\" + 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());
}
}
mas estou com um problema, necessito redimensionar arquivos de extensão .bmp, e quando é redimensionado e criado o arquivo, o arquivo está todo preto, quando faço o mesmo para arquivos .jpg, funciona normalmente, apenas para .bmp que não estou conseguindo fazer funcionar.
alguém tem alguma idéia que possa me ajudar?
Obrigado!