Example usage for java.awt.image BufferedImage getGraphics

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

Introduction

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

Prototype

public java.awt.Graphics getGraphics() 

Source Link

Document

This method returns a Graphics2D , but is here for backwards compatibility.

Usage

From source file:SaveImage.java

public SaveImage() {
    try {/*from  w  w w.ja  v a  2s . com*/
        bi = ImageIO.read(new File("bld.jpg"));
        w = bi.getWidth(null);
        h = bi.getHeight(null);
        if (bi.getType() != BufferedImage.TYPE_INT_RGB) {
            BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Graphics big = bi2.getGraphics();
            big.drawImage(bi, 0, 0, null);
            biFiltered = bi = bi2;
        }
    } catch (IOException e) {
        System.out.println("Image could not be read");
        System.exit(1);
    }
}

From source file:com.aistor.common.servlet.ValidateCodeServlet.java

private void createImage(HttpServletRequest request, HttpServletResponse response) throws IOException {

    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/jpeg");

    /*//from   ww  w  . j a va 2  s . c  om
     * ??
     */
    String width = request.getParameter("width");
    String height = request.getParameter("height");
    if (StringUtils.isNumeric(width) && StringUtils.isNumeric(height)) {
        w = NumberUtils.toInt(width);
        h = NumberUtils.toInt(height);
    }

    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();

    /*
     * ?
     */
    createBackground(g);

    /*
     * ?
     */
    String s = createCharacter(g);
    request.getSession().setAttribute("validateCode", s);

    g.dispose();
    OutputStream out = response.getOutputStream();
    ImageIO.write(image, "JPEG", out);
    out.close();

}

From source file:com.eryansky.common.web.servlet.ValidateCodeServlet.java

private void createImage(HttpServletRequest request, HttpServletResponse response) throws IOException {

    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/jpeg");

    /*//  www  . jav a 2  s .  c  o m
     * ??
     */
    String width = request.getParameter("width");
    String height = request.getParameter("height");
    if (StringUtils.isNumeric(width) && StringUtils.isNumeric(height)) {
        w = NumberUtils.toInt(width);
        h = NumberUtils.toInt(height);
    }

    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();

    /*
     * ?
     */
    createBackground(g);

    /*
     * ?
     */
    String s = createCharacter(g);
    request.getSession().setAttribute(SysConstants.SESSION_VALIDATE_CODE, s);

    g.dispose();
    OutputStream out = response.getOutputStream();
    ImageIO.write(image, "JPEG", out);
    out.close();

}

From source file:apm.common.servlet.ValidateCodeServlet.java

private void createImage(HttpServletRequest request, HttpServletResponse response) throws IOException {

    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/jpeg");

    /*/*from  w w w  .j a  va2s .  c  o m*/
     * ??
     */
    String width = request.getParameter("width");
    String height = request.getParameter("height");
    if (StringUtils.isNumeric(width) && StringUtils.isNumeric(height)) {
        w = NumberUtils.toInt(width);
        h = NumberUtils.toInt(height);
    }

    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();

    /*
     * ?
     */
    createBackground(g);

    /*
     * ?
     */
    String s = createCharacter(g);
    request.getSession().setAttribute(VALIDATE_CODE, s);

    g.dispose();
    OutputStream out = response.getOutputStream();
    ImageIO.write(image, "JPEG", out);
    out.close();

}

From source file:ImageDrawingComponent.java

public ImageDrawingComponent(URL imageSrc) {
    try {/*from   www .  j  av  a2 s  . co m*/
        bi = ImageIO.read(imageSrc);
        w = bi.getWidth(null);
        h = bi.getHeight(null);
        if (bi.getType() != BufferedImage.TYPE_INT_RGB) {
            BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Graphics big = bi2.getGraphics();
            big.drawImage(bi, 0, 0, null);
            bi = bi2;
        }
    } catch (IOException e) {
        System.out.println("Image could not be read");
        System.exit(1);
    }
}

From source file:de.anycook.upload.UploadHandler.java

/**
 * speichert eine grosse Version des Bildes
 *
 * @param image    BufferedImage/*ww w. j  a va2s  .  c  o m*/
 * @param filename Name der zu erzeugenden Datei
 */
private void saveOriginalImage(BufferedImage image, String filename) throws IOException {
    int height = image.getHeight();
    int width = image.getWidth();

    BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    newImage.getGraphics().drawImage(image, 0, 0, null);

    imageSaver.save("original/", filename, newImage);

}

From source file:WaitingImageObserver.java

/**
 * The workerthread. Simply draws the image to a BufferedImage's
 * Graphics-Object and waits for the AWT to load the image.
 *//*from   w w  w .j a  va 2  s .  c o m*/
public synchronized void waitImageLoaded() {

    if (this.lock == false) {
        return;
    }

    final BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    final Graphics g = img.getGraphics();

    while (this.lock) {
        if (g.drawImage(this.image, 0, 0, img.getWidth(this), img.getHeight(this), this)) {
            return;
        }

        try {
            wait(500);
        } catch (InterruptedException e) {
            System.out.println("WaitingImageObserver.waitImageLoaded(): InterruptedException thrown" + e);
        }
    }
}

From source file:de.anycook.upload.UploadHandler.java

/**
 * speichert eine grosse Version des Bildes
 *
 * @param image    BufferedImage/*from   w w w .ja v  a2s. c o m*/
 * @param filename Name der zu erzeugenden Datei
 */
private void saveBigImage(BufferedImage image, String filename) throws IOException {
    int height = image.getHeight();
    int width = image.getWidth();

    int newWidth = bigWidth;
    int newHeight = new Double(((double) height / (double) width) * newWidth).intValue();

    if (bigHeight > -1 && newHeight > bigHeight) {
        newHeight = bigHeight;
        newWidth = new Double(((double) width / (double) height) * newHeight).intValue();
    }

    BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
    newImage.getGraphics().drawImage(image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0,
            null);

    imageSaver.save("big/", filename, newImage);

}

From source file:de.anycook.upload.UploadHandler.java

/**
 * speichert eine kleine Version des Bildes
 *
 * @param image    BufferedImage/*from ww w.  ja  v  a 2 s.c  om*/
 * @param filename Name der zu erzeugenden Datei
 */
private void saveSmallImage(BufferedImage image, String filename) throws IOException {
    int height = image.getHeight();
    int width = image.getWidth();
    double imageRatio = (double) width / (double) height;

    int xtranslate = 0;
    int ytranslate = 0;

    if (imageRatio > 1) {
        xtranslate = (width - height) / 2;
    } else {
        ytranslate = (height - width) / 2;
    }

    BufferedImage tempImage = image.getSubimage(xtranslate, ytranslate, width - xtranslate * 2,
            height - ytranslate * 2);
    BufferedImage newImage = new BufferedImage(smallSize, smallSize, BufferedImage.TYPE_INT_RGB);
    newImage.getGraphics().drawImage(tempImage.getScaledInstance(smallSize, smallSize, Image.SCALE_SMOOTH), 0,
            0, null);

    imageSaver.save("small/", filename, newImage);
}

From source file:org.jcurl.core.swing.WCComponent.java

private BufferedImage renderPng(final String watermark) {
    final BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g2 = (Graphics2D) img.getGraphics();
    {/* w  w w  . j  a va  2 s .c  om*/
        final Map<Key, Object> hints = new HashMap<Key, Object>();
        hints.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        hints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        hints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
        hints.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        hints.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
        hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.addRenderingHints(hints);
    }
    final Font f0 = g2.getFont();
    paint(g2);
    g2.setTransform(new AffineTransform());
    if (watermark != null) {
        if (log.isDebugEnabled())
            log.debug(f0);
        g2.setFont(f0);
        g2.setColor(new Color(0, 0, 0, 128));
        g2.drawString(watermark, 10, 20);
    }
    g2.dispose();
    return img;
}