Example usage for java.awt.image BufferedImage createGraphics

List of usage examples for java.awt.image BufferedImage createGraphics

Introduction

In this page you can find the example usage for java.awt.image BufferedImage createGraphics.

Prototype

public Graphics2D createGraphics() 

Source Link

Document

Creates a Graphics2D , which can be used to draw into this BufferedImage .

Usage

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);
    }//w  ww .  java 2 s .co m
    g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
    g.dispose();
    return scaledBI;
}

From source file:Main.java

public static void draw(BufferedImage imageBG, BufferedImage imageFG) {
    Ellipse2D.Double ellipse1 = new Ellipse2D.Double(20, 20, 30, 30);
    Ellipse2D.Double ellipse2 = new Ellipse2D.Double(25, 25, 30, 30);
    Area circle = new Area(ellipse1);
    circle.subtract(new Area(ellipse2));

    Graphics2D g = imageBG.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g.setClip(circle);// ww w .  ja  v a 2  s . c  om
    g.drawImage(imageFG, 0, 0, null);
    g.setClip(null);
    Stroke s = new BasicStroke(2);
    g.setStroke(s);
    g.setColor(Color.BLACK);
    g.draw(circle);
    g.dispose();

    JLabel l = new JLabel(new ImageIcon(imageBG));
    JOptionPane.showMessageDialog(null, l);
}

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

/**
 * Esta funcin se aplica pq para imagenes muy grandes no reconoce el cdigo
 * QR//from   ww w.j a  v a2 s  .  c o 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:edu.sdsc.scigraph.services.jersey.writers.ImageWriter.java

private static BufferedImage renderImage(JPanel panel) {
    JFrame frame = new JFrame();
    frame.setUndecorated(true);/* w  w  w  .  j  a  va2s.co m*/
    frame.getContentPane().add(panel);
    frame.pack();
    BufferedImage bi = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = bi.createGraphics();
    panel.print(graphics);
    graphics.dispose();
    frame.dispose();
    return bi;
}

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);//from ww w . ja va 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:at.gv.egiz.pdfas.common.utils.ImageUtils.java

public static BufferedImage removeAlphaChannel(BufferedImage src) {
    //if (src.getColorModel().hasAlpha()) {
    BufferedImage image = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();
    g.setComposite(AlphaComposite.Src);
    g.drawImage(src, 0, 0, null);/* w  ww . j a  va 2 s . c  o m*/
    g.dispose();
    return image;
    //}
    //return src;
    /*
     * BufferedImage rgbImage = new BufferedImage(src.getWidth(),
     * src.getHeight(), BufferedImage.TYPE_3BYTE_BGR); for (int x = 0; x <
     * src.getWidth(); ++x) { for (int y = 0; y < src.getHeight(); ++y) {
     * rgbImage.setRGB(x, y, src.getRGB(x, y) & 0xFFFFFF); } } return
     * rgbImage;
     */
}

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 ww .ja va 2  s  .  c o  m

    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:com.github.lucapino.sheetmaker.PreviewJFrame.java

public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) {
    int w = image.getWidth();
    int h = image.getHeight();
    BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2 = output.createGraphics();

    // This is what we want, but it only does hard-clipping, i.e. aliasing
    // g2.setClip(new RoundRectangle2D ...)
    // so instead fake soft-clipping by first drawing the desired clip shape
    // in fully opaque white with antialiasing enabled...
    g2.setComposite(AlphaComposite.Src);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(Color.WHITE);//from   w  w  w.  j  a  va 2s . co m
    g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius));

    // ... then compositing the image on top,
    // using the white shape from above as alpha source
    g2.setComposite(AlphaComposite.SrcIn);
    g2.drawImage(image, 0, 0, null);

    g2.dispose();

    return output;
}

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public final static BufferedImage reduceImage(BufferedImage image, int width, int height) {
    int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = (BufferedImage) image;
    int w = image.getWidth();
    int h = image.getHeight();

    while (w != width || h != height) {
        if (w > width) {
            w /= 2;/*from w w w.j av a  2 s . c  o m*/
            if (w < width)
                w = width;
        }

        if (h > height) {
            h /= 2;
            if (h < height)
                h = height;
        }

        BufferedImage tmp = new BufferedImage(w, h, type);

        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();
        ret = tmp;
    }
    return ret;
}

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public static BufferedImage toBufferedImage(Image image) throws InterruptedException {

    if (image instanceof BufferedImage)
        return (BufferedImage) image;

    image = new ImageIcon(image).getImage();
    int type = hasAlpha(image) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
    BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    Graphics g = bimage.createGraphics();
    g.drawImage(image, 0, 0, null);//  www .j av  a  2  s  . co  m
    g.dispose();
    return bimage;
}