Ai pessoal, to usando o seguinte método para reduzir imagens:
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_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);
}
}
O problema é que ele esta dando erro ao tentar reduzir imagens que tenham grande resolução. Com imagens de baixa resolução ele resolve sem problemas.
Agora imagens com tamanho acima de 4mb por exemplo ele dah "ERRO ALTERANDO TAMANHO DA IMAGEM".
Alguem ai saberia com resolver essa parada ?? Poxa, ficou tudo bonitinho aqui, mas quando fui testar com outras fotos maiores...
Aqui vai o erro:Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)root cause
javax.servlet.ServletException: java.lang.Exception: ERRO ALTERANDO TAMANHO DA IMAGEM javax.imageio.IIOException: Unsupported Image Type
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
org.apache.jsp.galeriaFotos.classes.classeReduzir_jsp._jspService(classeReduzir_jsp.java:77)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)root cause
java.lang.Exception: ERRO ALTERANDO TAMANHO DA IMAGEM javax.imageio.IIOException: Unsupported Image Type
utilitarios.MyImages.gerarThumbnail(MyImages.java:57)
org.apache.jsp.galeriaFotos.classes.classeReduzir_jsp._jspService(classeReduzir_jsp.java:64)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
Obs.: A linha 57 é a que se refere ao catch/try do erro.