- public class ImageUtils {
- private static BufferedImage scale(BufferedImage img, int type, int dimencao, boolean higherQuality) {
- BufferedImage ret = img;
- int w, h;
- if (higherQuality) {
- // Use multi-step technique: start with original size, then
- // scale down in multiple passes with drawImage()
- // until the target size is reached
- w = img.getWidth();
- h = img.getHeight();
- } else {
- // Use one-step technique: scale directly from original
- // size to target size with a single drawImage() call
- w = dimencao;
- h = dimencao;
- }
- Graphics2D graphics = null;
- BufferedImage tmp = null;
- if (higherQuality) {
- if (w >= h) {
- h = h * dimencao / w;
- w = dimencao;
- } else {
- w = w * dimencao / h;
- h = dimencao;
- }
- }
- tmp = new BufferedImage(w, h, type);
- graphics = tmp.createGraphics();
- graphics.setComposite(AlphaComposite.Src);
- graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
- graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
- graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- graphics.drawImage(ret, 0, 0, w, h, null);
- graphics.dispose();
- ret = tmp;
- return ret;
- }
- public static byte[] getScaledImage(File file, int dimensao) {
- byte[] result = null;
- try {
- BufferedImage image = ImageIO.read(file);
- BufferedImage thumbs = ImageFileUtils.scale(image, BufferedImage.TYPE_INT_RGB, dimensao, true);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- // ImageIO.write(image, FilenameUtils.getExtension(file.getName()), baos);
- result = baos.toByteArray();
- } catch (FileNotFoundException fnfe) {
- logger.debug(fnfe == null ? "Imagem não localizada" : fnfe.getMessage());
- } catch (Exception ex) {
- logger.debug(ex == null ? ">>> erro ao atualizar o Outputstream do response." : ex.getMessage());
- }
- return result;
- }
- }
terça-feira, 15 de janeiro de 2013
Recuperar Imagem redimensionada no java
Assinar:
Postar comentários (Atom)
Nenhum comentário:
Postar um comentário