Example usage for java.awt Graphics2D drawRenderedImage

List of usage examples for java.awt Graphics2D drawRenderedImage

Introduction

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

Prototype

public abstract void drawRenderedImage(RenderedImage img, AffineTransform xform);

Source Link

Document

Renders a RenderedImage , applying a transform from image space into user space before drawing.

Usage

From source file:D20140128.ApacheXMLGraphicsTest.TilingPatternExample.java

private void paintTileAlone(Graphics2D g2d) {
    AffineTransform at = new AffineTransform();
    at.translate(5, 5);//  w  w w  .  j av a2  s. co m
    g2d.drawRenderedImage(this.tile, at);
}

From source file:de.romankreisel.faktotum.beans.BundesbruderBean.java

public void rotateProfilePictureClockwise(BundesbruderEntity bundesbruder, double angle) throws IOException {
    BufferedImage image = this.getImageFromByteArray(bundesbruder.getPictureOriginal());
    double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
    int w = image.getWidth(), h = image.getHeight();
    int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
    GraphicsConfiguration gc = image.createGraphics().getDeviceConfiguration();
    BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
    Graphics2D g = result.createGraphics();
    g.translate((neww - w) / 2, (newh - h) / 2);
    g.rotate(angle, w / 2, h / 2);//from ww  w .j  a  v a2  s  .  co m
    g.drawRenderedImage(image, null);
    g.dispose();
    this.setProfilePicture(bundesbruder, this.storeImageToByteArray(image));
}

From source file:com.webpagebytes.cms.DefaultImageProcessor.java

public boolean resizeImage(WPBFileStorage cloudStorage, WPBFilePath cloudFile, int desiredSize,
        String outputFormat, OutputStream os) throws WPBException {
    InputStream is = null;/*  w ww .j a  v  a2s  .c o  m*/
    try {
        //get the file content
        WPBFileInfo fileInfo = cloudStorage.getFileInfo(cloudFile);
        String type = fileInfo.getContentType().toLowerCase();
        if (!type.startsWith("image")) {
            return false;
        }
        is = cloudStorage.getFileContent(cloudFile);
        BufferedImage bufImg = ImageIO.read(is);
        Dimension<Integer> newSize = getResizeSize(bufImg.getWidth(), bufImg.getHeight(), desiredSize);
        BufferedImage bdest = new BufferedImage(newSize.getX(), newSize.getY(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bdest.createGraphics();
        Dimension<Double> scale = getResizeScale(bufImg.getHeight(), bufImg.getWidth(), desiredSize);
        AffineTransform at = AffineTransform.getScaleInstance(scale.getX(), scale.getY());
        g.drawRenderedImage(bufImg, at);
        ImageIO.write(bdest, outputFormat, os);
        return true;
    } catch (Exception e) {
        throw new WPBException("Cannot resize image ", e);
    } finally {
        IOUtils.closeQuietly(is);
    }

}

From source file:com.salesmanager.core.util.ProductImageUtil.java

private BufferedImage createCompatibleImage(BufferedImage image) {
    GraphicsConfiguration gc = BufferedImageGraphicsConfig.getConfig(image);
    int w = image.getWidth();
    int h = image.getHeight();
    BufferedImage result = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
    Graphics2D g2 = result.createGraphics();
    g2.drawRenderedImage(image, null);
    g2.dispose();//from   w  w w.ja v  a  2 s . co  m
    return result;
}

From source file:dk.dma.msiproxy.common.repo.ThumbnailService.java

/**
 * Creates a thumbnail for the image file using plain old java
 * @param file the image file//  w w  w  .  ja  v  a 2s. c  o  m
 * @param thumbFile the resulting thumbnail file
 * @param size the size of the thumbnail
 */
private void createThumbnailUsingJava(Path file, Path thumbFile, IconSize size) throws IOException {

    try {
        BufferedImage image = ImageIO.read(file.toFile());

        int w = image.getWidth();
        int h = image.getHeight();

        // Never scale up
        if (w <= size.getSize() && h <= size.getSize()) {
            FileUtils.copyFile(file.toFile(), thumbFile.toFile());

        } else {
            // Compute the scale factor
            double dx = (double) size.getSize() / (double) w;
            double dy = (double) size.getSize() / (double) h;
            double d = Math.min(dx, dy);

            // Create the thumbnail
            BufferedImage thumbImage = new BufferedImage((int) Math.round(w * d), (int) Math.round(h * d),
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = thumbImage.createGraphics();
            AffineTransform at = AffineTransform.getScaleInstance(d, d);
            g2d.drawRenderedImage(image, at);
            g2d.dispose();

            // Save the thumbnail
            String fileName = thumbFile.getFileName().toString();
            ImageIO.write(thumbImage, FilenameUtils.getExtension(fileName), thumbFile.toFile());

            // Releas resources
            image.flush();
            thumbImage.flush();
        }

        // Update the timestamp of the thumbnail file to match the change date of the image file
        Files.setLastModifiedTime(thumbFile, Files.getLastModifiedTime(file));

    } catch (Exception e) {
        log.error("Error creating thumbnail for image " + file, e);
        throw new IOException(e);
    }
}

From source file:omr.jai.TestImage3.java

public void paintComponent(Graphics g)
{
    // For background
    super.paintComponent(g);

    // Meant for visual check
    if (image != null) {

        Graphics2D g2 = (Graphics2D) g;

        g2.drawRenderedImage (image, scaleXform);
        //g2.drawImage (image, 1, 1, this);
    }/*www.j  a  v a2  s  . co  m*/
}

From source file:eu.novait.imagerenamer.model.ImageFile.java

private void createThumbnail() {
    try {/*from   ww  w.  j av  a 2s  .  c o m*/
        BufferedImage tmp = ImageIO.read(this.filepath);
        int tmpW = tmp.getWidth();
        int tmpH = tmp.getHeight();
        double ratio = (double) tmpW / (double) tmpH;
        int w = 400;
        int h = (int) Math.round(w / ratio);
        BufferedImage bi = getCompatibleImage(w, h);
        Graphics2D g2d = bi.createGraphics();
        double xScale = (double) w / tmp.getWidth();
        double yScale = (double) h / tmp.getHeight();
        AffineTransform at = AffineTransform.getScaleInstance(xScale, yScale);
        g2d.drawRenderedImage(tmp, at);
        g2d.dispose();
        this.setThumbnail(bi);
    } catch (IOException ex) {
        Logger.getLogger(ImageFile.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.iish.visualmets.services.ImageTransformation.java

/**
 * Returns a scaled BufferedImage//from ww  w .ja v  a  2s . c  om
 *
 * @param bi        current image
 * @param maxWidth  new width
 * @param maxHeight new height
 * @return a new/scaled image
 */

/*public static BufferedImage ScaleImage(BufferedImage image, int width, int height) throws IOException {
   int imageWidth  = image.getWidth();
   int imageHeight = image.getHeight();
        
   double scaleX = (double)width/imageWidth;
   double scaleY = (double)height/imageHeight;
   AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
   AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
        
   return bilinearScaleOp.filter(
    image,
    new BufferedImage(width, height, image.getType()));
}*/

public BufferedImage ScaleImage(BufferedImage bi, int maxWidth, int maxHeight) {
    double originalWidth = bi.getWidth() * 1.0;
    double originalHeight = bi.getHeight() * 1.0;

    double widthRatio = (maxWidth * 1.0) / originalWidth;
    double heightRatio = (maxHeight * 1.0) / originalHeight;
    double newImageRatio = 0;
    if (widthRatio < heightRatio) {
        newImageRatio = widthRatio;
    } else {
        newImageRatio = heightRatio;
    }

    BufferedImage bdest = new BufferedImage((int) (originalWidth * newImageRatio),
            (int) (originalHeight * newImageRatio), BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bdest.createGraphics();
    AffineTransform at = AffineTransform.getScaleInstance(newImageRatio, newImageRatio);
    g.drawRenderedImage(bi, at);

    return bdest;
}

From source file:com.lizardtech.expresszip.model.Job.java

private BufferedImage mattCroppedImage(final BufferedImage source, GridCoverage2D cropped, int bufferType) {
    int height = source.getHeight();
    int width = source.getWidth();
    BufferedImage image = new BufferedImage(width, height, bufferType);
    Graphics2D gr = image.createGraphics(); // the way getRenderedImage works, this image should be drawn at 0, 0, not at based
    // on where the coverage's tile is
    AffineTransform at = AffineTransform.getTranslateInstance(0, 0);
    gr.drawRenderedImage(cropped.getRenderedImage(), at);
    return image;
}

From source file:com.joliciel.jochre.graphics.RowOfShapesImpl.java

public BufferedImage getImage() {
    if (this.image == null && this.container != null) {
        int buffer = 5;
        int width = this.container.getOriginalImage().getWidth();
        int height = this.getBottom() - this.getTop() + 1 + (buffer * 2);
        int bottom = (this.getTop() - buffer) + height;

        if (bottom > this.container.getOriginalImage().getHeight()) {
            int overlap = bottom - this.container.getOriginalImage().getHeight();
            height = height - overlap;/*from ww  w.  ja  v a 2  s  .  c om*/
        }
        BufferedImage rowImage = this.container.getOriginalImage().getSubimage(0, this.getTop() - buffer, width,
                height);

        double scale = (double) ROW_IMAGE_WIDTH / (double) width;
        int newHeight = (int) Math.floor(scale * height);
        if (this.container.getOriginalImage().getColorModel() instanceof IndexColorModel) {
            image = new BufferedImage(ROW_IMAGE_WIDTH, newHeight, this.container.getOriginalImage().getType(),
                    (IndexColorModel) this.container.getOriginalImage().getColorModel());
        } else {
            image = new BufferedImage(ROW_IMAGE_WIDTH, newHeight, this.container.getOriginalImage().getType());
        }
        Graphics2D graphics2d = image.createGraphics();

        AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
        graphics2d.drawRenderedImage(rowImage, at);

        // white out the space to the right & left of this row
        graphics2d.setColor(Color.WHITE);
        graphics2d.fillRect(0, 0, (int) Math.round((this.getLeft() - 5) * scale),
                (int) Math.round(height * scale));
        graphics2d.fillRect((int) Math.round((this.getRight() + 5) * scale), 0,
                (int) Math.round((width - this.getRight()) * scale), (int) Math.round(height * scale));

    }
    return image;
}