Bom dia gente, estou tendo problemas em redimensionar uma imagem. O código funciona 100%, mas a imagem que é redimensionada fica com outra cor, e a recortada fica ok, sem qualquer coloração.
public void salvar() {
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String newFileName = servletContext.getRealPath("") + File.separator + "arqs" + File.separator + "banners" + File.separator + "img" + File.separator + nomeCategoria + File.separator + nomeBanner + ".jpg";
String newFileName2 = servletContext.getRealPath("") + File.separator + "arqs" + File.separator + "banners" + File.separator + "img" + File.separator + nomeCategoria + File.separator + "miniaturas" + File.separator + nomeBanner + ".jpg";
newFileName = newFileName.replace("\\", "/");
newFileName2 = newFileName2.replace("\\", "/");
BufferedImage img;
try {
img = ImageIO.read(new ByteArrayInputStream(croppedImage.getBytes()));
System.out.print(img);
ImageIO.write(img, "jpeg", new File(newFileName));
//a imagem salva aqui está 100%, sem perda de cores
BufferedImage miniatura = ImageIO.read(new File(newFileName));
ImageIO.write(resizeTrick(miniatura, 100, 55), "jpeg", new File(newFileName2));
//fica com uma mascara de cor em cima
} catch (IOException ex) {
Logger.getLogger(ControleUpload.class.getName()).log(Level.SEVERE, null, ex);
}
}
/* Redimensionar */
private static BufferedImage resize(BufferedImage image, int width, int height) {
int type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType();
BufferedImage resizedImage = new BufferedImage(width, height, type);
Graphics2D g = resizedImage.createGraphics();
g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return resizedImage;
}
private static BufferedImage resizeTrick(BufferedImage image, int width, int height) {
image = createCompatibleImage(image);
image = resize(image, 100, 100);
image = blurImage(image);
return resize(image, width, height);
}
public static BufferedImage blurImage(BufferedImage image) {
float ninth = 1.0f / 9.0f;
float[] blurKernel = {
ninth, ninth, ninth,
ninth, ninth, ninth,
ninth, ninth, ninth
};
Map<RenderingHints.Key, Object> map = new HashMap<RenderingHints.Key, Object>();
map.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
map.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
map.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
RenderingHints hints = new RenderingHints(map);
BufferedImageOp op = new ConvolveOp(new Kernel(3, 3, blurKernel), ConvolveOp.EDGE_NO_OP, hints);
return op.filter(image, null);
}
private static BufferedImage createCompatibleImage(BufferedImage image) {
GraphicsConfiguration gc = BufferedImageGraphicsConfig.getConfig(image);
int w = image.getWidth();
int h = image.getHeight();
BufferedImage result = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
Graphics2D g2 = result.createGraphics();
g2.drawRenderedImage(image, null);
g2.dispose();
return result;
}
/* ------------- */
como eu disse, tudo funciona e salva tranquilamente, só a miniatura que recebe um layer de cor.

