Example usage for java.awt Graphics2D drawImage

List of usage examples for java.awt Graphics2D drawImage

Introduction

In this page you can find the example usage for java.awt Graphics2D drawImage.

Prototype

public abstract boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer);

Source Link

Document

Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.

Usage

From source file:kz.supershiny.core.services.ImageService.java

/**
 * Creates thumb for image./*from   w w w . ja  v  a  2s  .  co m*/
 *
 * @param originalData original image in byte array
 * @param type original - 0, large - 1, small - 2
 * @return resized image in byte array
 */
public static byte[] resizeImage(byte[] originalData, ImageSize type) {
    //if original flag, then return original
    if (type.equals(ImageSize.ORIGINAL)) {
        return originalData;
    }

    BufferedImage originalImage = null;
    BufferedImage resizedImage = null;
    byte[] result = null;
    //convert bytes to BufferedImage
    try (InputStream in = new ByteArrayInputStream(originalData)) {
        originalImage = ImageIO.read(in);
    } catch (IOException ex) {
        LOG.error("Cannot convert byte array to BufferedImage!", ex);
        return null;
    }
    //get original size
    int scaledHeight = originalImage.getHeight();
    int scaledWidth = originalImage.getWidth();

    switch (type) {
    case LARGE:
        scaledWidth = LARGE_WIDTH;
        scaledHeight = LARGE_HEIGHT;
        break;
    case SMALL:
        scaledWidth = SMALL_WIDTH;
        scaledHeight = SMALL_HEIGHT;
        break;
    default:
        break;
    }
    //calculate aspect ratio
    float ratio = 1.0F * originalImage.getWidth() / originalImage.getHeight();
    if (ratio > 1.0F) {
        scaledHeight = (int) (scaledHeight / ratio);
    } else {
        scaledWidth = (int) (scaledWidth * ratio);
    }
    //resize with hints
    resizedImage = new BufferedImage(scaledWidth, scaledHeight,
            originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType());

    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
    g.dispose();
    g.setComposite(AlphaComposite.Src);

    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    //convert BufferedImage to bytes
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        ImageIO.write(resizedImage, "png", baos);
        baos.flush();
        result = baos.toByteArray();
    } catch (IOException ex) {
        LOG.error("Cannot convert BufferedImage to byte array!", ex);
    }
    return result;
}

From source file:com.l1j5.web.common.utils.ImageUtils.java

public static void getImageThumbnail(BufferedInputStream stream_file, String save, String type, int w, int h) {

    try {//from w w  w.jav a2  s .  com
        File file = new File(save);
        BufferedImage bi = ImageIO.read(stream_file);

        int width = bi.getWidth();
        int height = bi.getHeight();
        if (w < width) {
            width = w;
        }
        if (h < height) {
            height = h;
        }

        BufferedImage bufferIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Image atemp = bi.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);

        Graphics2D g2 = bufferIm.createGraphics();
        g2.drawImage(atemp, 0, 0, width, height, null);
        ImageIO.write(bufferIm, type, file);
    } catch (Exception e) {
        log.error(e);
    }

}

From source file:com.l1j5.web.common.utils.ImageUtils.java

public static void getImageThumbnail(BufferedInputStream stream_file, String save, String type, int w) {

    try {//w  w  w . j av a 2s  .com
        File file = new File(save);
        BufferedImage bi = ImageIO.read(stream_file);

        int width = bi.getWidth();
        int height = bi.getHeight();

        double ratio = (double) height / width;
        height = (int) (w * ratio);
        if (w < width) {
            width = w;
        }

        BufferedImage bufferIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Image atemp = bi.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);

        Graphics2D g2 = bufferIm.createGraphics();
        g2.drawImage(atemp, 0, 0, width, height, null);
        ImageIO.write(bufferIm, type, file);
    } catch (Exception e) {
        log.error(e);
    }

}

From source file:com.l1j5.web.common.utils.ImageUtils.java

public static void getGifImageThumbnail(BufferedInputStream stream_file, String save, String type, int w) {

    GifDecoder dec = new GifDecoder();
    AnimatedGifEncoder enc = new AnimatedGifEncoder();
    dec.read(stream_file);//  ww w .j a  v a  2  s .  com

    int cnt = dec.getFrameCount();

    int delay = 0;
    int width = 0;
    int height = 0;

    try {
        enc.start(save);
        enc.setRepeat(0);

        for (int i = 0; i < cnt; i++) {
            BufferedImage frame = dec.getFrame(i);
            delay = dec.getDelay(i);

            width = frame.getWidth();
            height = frame.getHeight();

            double ratio = (double) height / width;
            height = (int) (w * ratio);
            if (w < width) {
                width = w;
            }

            BufferedImage destimg = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D g = destimg.createGraphics();

            g.drawImage(frame, 0, 0, width, height, null);
            enc.setDelay(delay);

            enc.addFrame(destimg);
        }

        enc.finish();
    } catch (Exception ex) {
        log.error(ex);
    }
}

From source file:com.l1j5.web.common.utils.ImageUtils.java

public static void getGifImageThumbnail(BufferedInputStream stream_file, String save, String type, int w,
        int h) {//  w  w  w.j a  v  a 2  s .c om

    GifDecoder dec = new GifDecoder();
    AnimatedGifEncoder enc = new AnimatedGifEncoder();
    dec.read(stream_file);

    int cnt = dec.getFrameCount();

    int delay = 0;
    int width = 0;
    int height = 0;

    try {
        enc.start(save);
        enc.setRepeat(0);

        for (int i = 0; i < cnt; i++) {
            BufferedImage frame = dec.getFrame(i);
            delay = dec.getDelay(i);

            width = frame.getWidth();
            height = frame.getHeight();
            if (w < width) {
                width = w;
            }
            if (h < height) {
                height = h;
            }

            BufferedImage destimg = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D g = destimg.createGraphics();

            g.drawImage(frame, 0, 0, width, height, null);
            enc.setDelay(delay);

            enc.addFrame(destimg);
        }

        enc.finish();
    } catch (Exception ex) {
        log.error(ex);
    }
}

From source file:es.logongas.util.seguridad.CodigoVerificacionSeguro.java

/**
 * Esta funcin se aplica pq para imagenes muy grandes no reconoce el cdigo
 * QR//  w w w  .  ja  v a 2  s. co  m
 *
 * @param originalImage La imagen original
 * @param f El factor de escalado
 * @return La iamgen reescalada.
 */
private static BufferedImage resizeImage(BufferedImage originalImage, double f) {
    int ancho = (int) (((double) originalImage.getWidth()) * f);
    int alto = (int) (((double) originalImage.getHeight()) * f);
    int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
    BufferedImage resizedImage = new BufferedImage(ancho, alto, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, ancho, alto, null);
    g.dispose();

    return resizedImage;
}

From source file:com.fun.util.TesseractUtil.java

/**
 * /*from   w ww .ja v a2  s .c om*/
 *
 * @param imageFile
 * @param times
 * @param targetFile
 * @throws IOException
 */
private static void scaled(File imageFile, int times, File targetFile) throws IOException {
    BufferedImage image = ImageIO.read(imageFile);
    int targetWidth = image.getWidth() * times;
    int targetHeight = image.getHeight() * times;
    int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage tmp = new BufferedImage(targetWidth, targetHeight, type);
    Graphics2D g2 = tmp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2.drawImage(image, 0, 0, targetWidth, targetHeight, null);
    g2.dispose();
    ImageIO.write(tmp, "png", targetFile);
}

From source file:Main.java

public static BufferedImage scaleImage(BufferedImage img, int width, int height, Color background) {
    int imgWidth = img.getWidth();
    int imgHeight = img.getHeight();
    if (imgWidth * height < imgHeight * width) {
        width = imgWidth * height / imgHeight;
    } else {/*  w w  w  . j a v a 2 s  . com*/
        height = imgHeight * width / imgWidth;
    }
    BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = newImage.createGraphics();
    try {
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.setBackground(background);
        g.clearRect(0, 0, width, height);
        g.drawImage(img, 0, 0, width, height, null);
    } finally {
        g.dispose();
    }
    return newImage;
}

From source file:com.vaadin.testbench.screenshot.ImageUtil.java

/**
 * Clones the given BufferedImage/*from w w w  .  ja va2  s  . com*/
 * 
 * @param sourceImage
 *            The image to copy
 * @return A copy of sourceImage
 */
public static BufferedImage cloneImage(BufferedImage sourceImage) {
    // This method could likely be optimized but the gain is probably
    // small
    final int w = sourceImage.getWidth();
    final int h = sourceImage.getHeight();

    BufferedImage newImage = new BufferedImage(w, h, TYPE_INT_RGB);

    Graphics2D g = (Graphics2D) newImage.getGraphics();
    g.drawImage(sourceImage, 0, 0, w, h, null);
    g.dispose();

    return newImage;
}

From source file:su.fmi.photoshareclient.remote.ImageHandler.java

public static BufferedImage createResizedCopy(Image originalImage, int scaledWidth, int scaledHeight,
        boolean preserveAlpha) {
    int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
    BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
    Graphics2D g = scaledBI.createGraphics();
    if (preserveAlpha) {
        g.setComposite(AlphaComposite.Src);
    }//from  w  ww .j a va2  s.  co  m
    g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
    g.dispose();
    return scaledBI;
}