Java Image Resize resize(Image img, int newWidth, float quality)

Here you can find the source of resize(Image img, int newWidth, float quality)

Description

resize

License

LGPL

Declaration

public static Image resize(Image img, int newWidth, float quality) throws IOException 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.awt.Color;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.MemoryCacheImageOutputStream;
import javax.swing.ImageIcon;

public class Main {
    public static Image resize(Image img, int newWidth, float quality) throws IOException {

        if (quality < 0 || quality > 1) {
            throw new IllegalArgumentException("Quality has to be between 0 and 1");
        }//from w w w . j a  v  a  2  s.c  om

        ImageIcon ii = new ImageIcon(img);
        Image i = ii.getImage();
        Image resizedImage = null;

        int iWidth = i.getWidth(null);
        int iHeight = i.getHeight(null);

        if (iWidth > iHeight) {
            resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight) / iWidth, Image.SCALE_SMOOTH);
        } else {
            resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight, newWidth, Image.SCALE_SMOOTH);
        }

        // This code ensures that all the pixels in the image are loaded.
        Image temp = new ImageIcon(resizedImage).getImage();

        // Create the buffered image.
        BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null),
                BufferedImage.TYPE_INT_RGB);

        // Copy image to buffered image.
        Graphics g = bufferedImage.createGraphics();

        // Clear background and paint the image.
        g.setColor(Color.white);
        g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
        g.drawImage(temp, 0, 0, null);
        g.dispose();

        // Soften.
        float softenFactor = 0.05f;
        float[] softenArray = { 0, softenFactor, 0, softenFactor, 1 - (softenFactor * 4), softenFactor, 0,
                softenFactor, 0 };
        Kernel kernel = new Kernel(3, 3, softenArray);
        ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        bufferedImage = cOp.filter(bufferedImage, null);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        MemoryCacheImageOutputStream mos = new MemoryCacheImageOutputStream(baos);

        Object[] obj = getWriter(quality);

        if (obj == null)
            return null;

        ImageWriter writer = (ImageWriter) obj[0];
        ImageWriteParam iwp = (ImageWriteParam) obj[1];

        writer.setOutput(mos);

        IIOImage image = new IIOImage(bufferedImage, null, null);
        writer.write(null, image, iwp);

        return new ImageIcon(baos.toByteArray()).getImage();
    }

    private static Object[] getWriter(float quality) {

        Object[] obj = new Object[2];

        Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");

        if (iter.hasNext()) {

            ImageWriter writer = iter.next();

            ImageWriteParam iwp = writer.getDefaultWriteParam();

            iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);

            iwp.setCompressionQuality(quality);

            obj[0] = writer;
            obj[1] = iwp;

            return obj;
        }

        return null;

    }

    public static byte[] toByteArray(Image img) throws IOException {

        ImageIcon ii = new ImageIcon(img);
        img = ii.getImage();

        // img eh o seu objeto image
        BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bi.createGraphics();
        g2.drawImage(img, 0, 0, null);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        MemoryCacheImageOutputStream mos = new MemoryCacheImageOutputStream(baos);

        Object[] obj = getWriter(1);

        if (obj == null)
            return null;

        ImageWriter writer = (ImageWriter) obj[0];
        ImageWriteParam iwp = (ImageWriteParam) obj[1];

        writer.setOutput(mos);

        IIOImage image = new IIOImage(toBufferedImage(img), null, null);
        writer.write(null, image, iwp);

        return baos.toByteArray();

    }

    public static BufferedImage toBufferedImage(Image img) {

        ImageIcon ii = new ImageIcon(img);
        img = ii.getImage();

        BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.drawImage(img, 0, 0, null);
        g.dispose();

        return bi;
    }
}

Related

  1. getResizedImage(Image img, int iNewWidth, int iNewHeight)
  2. getResizedImage(Image original, final Integer w, final Integer h, boolean keepAspectRatio)
  3. getResizedImage(Image originalImage, Dimension newSize)
  4. getResizedImage(Image originalImage, int newWidth, int newHeight, int imageType)
  5. getResizedImage(String path, int height, int width)
  6. resize(String srcImageFile, String result, int newWidth, float quality)
  7. resizeImage(ImageIcon tmpIcon)
  8. resizeImage(String imagePath, int width, int height)
  9. resizeImage(String loadFile, String saveFile, int maxDim)