Galera, to com um problema que não sei mais o que fazer. Tenho um gerenciador de galeria de fotos. Tô usando um método para redimensionar as fotos e adicionar uma marca dágua, só que ele tem dois problemas: QUALIDADE RUIM DOS THUMBS E CONSOME MUITA MEMÓRIA.
O mais sério é a QUALIDADE, que meus clientes reclamam demais. Alguém tem um método show de bola ai não ?
Segue os métodos:
[code]public static void gerarThumbnail(InputStream inputStream, OutputStream out, int thumbAltura, int thumbLargura, float quality) throws Exception{
Image image = null;
int largura = 0;
int altura = 0;
try {
//Recuperar os valores de LARGURA/ALTURA e ALTERAR no registro da FOTO
image = ImageIO.read(inputStream);
largura = image.getWidth(null);
altura = image.getHeight(null);
} catch (Exception e) {
throw new Exception("ERRO ALTERANDO TAMANHO DA IMAGEM " + e);
}
try {
//Fazer a geracao do TumbNail levando em conta o tamanho da ALTURA
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
// determine thumbnail size from WIDTH and HEIGHT
double thumbPorcentagem;
if(thumbLargura == 0){
thumbPorcentagem = ((thumbAltura*100.0)/altura)/100.0;
if(thumbLargura == 0){
thumbLargura = (int)(largura * thumbPorcentagem);
}
}
if(thumbAltura == 0){
thumbPorcentagem = ((thumbLargura*100.0)/largura)/100.0;
if(thumbAltura == 0){
thumbAltura = (int)(altura * thumbPorcentagem);
}
}
// draw original image to thumbnail image object and
// scale it to the new size on-the-fly
BufferedImage thumbImage = new BufferedImage(thumbLargura,thumbAltura, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); //VALUE_INTERPOLATION_BICUBIC - For speed better use RenderingHints.VALUE_INTERPOLATION_BILINEAR
graphics2D.drawImage(image, 0, 0, thumbLargura, thumbAltura, null);
graphics2D.dispose();
// save thumbnail image to OUTFILE
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
param.setQuality(quality, false); //The quality argument from the command line is converted from the interval 0 to 100 to the interval 0.0f to 1.0f, because that's what the codec expects (I mostly use 0.75f).
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
/* ALTERAÇÕES EM 26/12/2008 */
graphics2D.dispose();
image.flush();
thumbImage.flush();
/* ------------------------------------ */
inputStream.close();
out.flush();
out.close();
} catch (Exception e) {
throw new Exception("ERRO SALVANDO O THUMBNAIL DA IMAGEM " + e);
}
}
public static void gerarWatermark(InputStream imagemOriginal, InputStream imagemWatermark, OutputStream imagemGerada,String posicao,float alpha){
try {
BufferedImage im = ImageIO.read(imagemOriginal);
BufferedImage im2 = ImageIO.read(imagemWatermark);
Graphics2D g = im.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
//g.drawImage(im2, (im.getWidth()-im2.getWidth())/2, (im.getHeight()-im2.getHeight())/2, null);
// TOP ESQUERDO
if(posicao.equals("7")){
g.drawImage(im2, 0, 0, null);
// TOP CENTRO
}else if(posicao.equals("8")){
g.drawImage(im2, (im.getWidth()-im2.getWidth())/2, 0, null);
// TOP DIREITO
}else if(posicao.equals("9")){
g.drawImage(im2, (im.getWidth()-im2.getWidth()), 0, null);
// CENTRO ESQUERDO
}else if(posicao.equals("4")){
g.drawImage(im2, 0, (im.getHeight()-im2.getHeight())/2, null);
// CENTRO
}else if(posicao.equals("5")){
g.drawImage(im2, (im.getWidth()-im2.getWidth())/2, (im.getHeight()-im2.getHeight())/2, null);
// CENTRO DIREITO
}else if(posicao.equals("6")){
g.drawImage(im2, (im.getWidth()-im2.getWidth()), (im.getHeight()-im2.getHeight())/2, null);
// BASE ESQUERDA
}else if(posicao.equals("1")){
g.drawImage(im2, 0, (im.getHeight()-im2.getHeight()), null);
// BASE CENTRO
}else if(posicao.equals("2")){
g.drawImage(im2, (im.getWidth()-im2.getWidth())/2, (im.getHeight()-im2.getHeight()), null);
// BASE DIREITA
}else if(posicao.equals("3")){
g.drawImage(im2, (im.getWidth()-im2.getWidth()), (im.getHeight()-im2.getHeight()), null);
}
g.dispose();
ImageIO.write(im, "jpeg", imagemGerada);
/* ALTERAÇÕES EM 26/12/2008 */
im.flush();
im2.flush();
/* --------------------------- */
imagemOriginal.close();
imagemWatermark.close();
imagemGerada.flush();
imagemGerada.close();
} catch (Exception e) {
System.out.println(e);
}
}
public static void gerarWatermark(InputStream imagemOriginal, String textoWatermark, OutputStream imagemGerada){
try {
BufferedImage im = ImageIO.read(imagemOriginal);
//Preparando o Graphics 2D para incluir o texto na imagem
Graphics2D g = im.createGraphics();
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//Setar a font utilizada no texto
g.setFont(new Font(“Lucida Bright”, Font.ITALIC, 60));
//Efeitos de rotacao no texto
g.rotate(-Math.PI/4, im.getWidth()/2, im.getHeight()/2);
//Setando o valor do texto
TextLayout tl = new TextLayout(textoWatermark, g.getFont(), g.getFontRenderContext());
//???
Rectangle2D bounds = tl.getBounds();
double x = (im.getWidth()-bounds.getWidth())/2 - bounds.getX();
double y = (im.getHeight()-bounds.getHeight())/2 - bounds.getY();
//
Shape outline = tl.getOutline(AffineTransform.getTranslateInstance(x+2, y+1));
// Montando a saida
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); // Estava 0.3
g.setPaint(Color.WHITE);
g.draw(outline);
g.setPaint(new GradientPaint(0, 0, Color.WHITE, 30, 20, new Color(128,128,255), true));
tl.draw(g, (float)x, (float)y);
g.dispose();
ImageIO.write(im, “jpeg”, imagemGerada);
imagemOriginal.close();
imagemGerada.close();
imagemGerada.flush();
imagemGerada.close();
} catch (Exception e) {
System.out.println(e);
}
}[/code]