Olá Senhores !
Seguinte, tenho uma aplicação na web que edita algumas fotos, estou usando dois métodos que estão consumindo demais a memória da minha
hospedagem. Tipo, quando cliente envia as fotos, as vezes toda aplicação trava e não volta até eu reiniciar o Apache. No log do apache foi indicado que é no servlet, e executando a função de imagens.
Os métodos servem para reduzir e colocar marca d'agua nas imagens.
Elas estão ai embaixo, agradeço quem poder analisar para ver onde esta o(s) erro(s).
public static void gerarThumbnail(InputStream inputStream, OutputStream out, int thumbAltura, 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 = ((thumbAltura*100.0)/altura)/100.0;
int thumbLargura = (int)(largura * 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);
// 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);
inputStream.close();
out.flush();
out.close();
} catch (Exception e) {
throw new Exception("ERRO SALVANDO O THUMBNAIL DA IMAGEM " + e);
}
}
Aqui a outra para gerar a marca d'agua.
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);
imagemOriginal.close();
imagemWatermark.close();
imagemGerada.flush();
imagemGerada.close();
} catch (Exception e) {
System.out.println(e);
}
}
A minha hospedagem esta com 256 de memória. E mesmo assim as vezes trava quando os métodos são chamados.
Obs.: O único agravante, fora os métodos, é que a forma que esta sendo enviado os forms é individual e não todos de uma so vez, (para poderem ser quebrados com common Fileupload por exemplo). Elas são enviadas simulando o sistema do Gmail, para um iframe na página e deixando uma msg de "Carregando...". Dai o usuário pode enviar uma, depois vai para outra, para outra...e todas vão sendo carregadas. Num total de até 10.
Bom, vejam se há problemas na função, se sim, mudarei do contrario mudarei a forma de envio de imagens.
Valeu !
Valeu !