Example usage for java.awt.image AffineTransformOp AffineTransformOp

List of usage examples for java.awt.image AffineTransformOp AffineTransformOp

Introduction

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

Prototype

public AffineTransformOp(AffineTransform xform, int interpolationType) 

Source Link

Document

Constructs an AffineTransformOp given an affine transform and the interpolation type.

Usage

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage resizeImage(BufferedImage img, double xScale, double yScale, int type) {
    if (img == null) {
        return null;
    }/*from   ww  w.  j  a va2s .  c  o m*/

    if (xScale <= 0 || yScale <= 0) {
        return null;
    }

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

    int neww = (int) (((double) w) * xScale);
    int newh = (int) (((double) h) * yScale);

    BufferedImage out = new BufferedImage(neww, newh, img.getType());

    AffineTransform tr = new AffineTransform();
    tr.scale(xScale, yScale);
    BufferedImageOp op = new AffineTransformOp(tr, type);
    op.filter(img, out);
    return out;
}

From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java

/**
 * Flip the image horizontal/*from   ww  w .  jav  a  2  s . co  m*/
 *
 * @param bufferedImage original image
 * @return horizontally flipped image
 */
private static BufferedImage flipHorizontal(BufferedImage bufferedImage) {
    AffineTransform at = AffineTransform.getTranslateInstance(bufferedImage.getWidth(), 0);
    at.scale(-1.0, 1.0);
    BufferedImageOp biOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    BufferedImage imgRes = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
            bufferedImage.getType());
    return biOp.filter(bufferedImage, imgRes);
}

From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java

/**
 * Flip the image horizontal//from  www.  j a  va  2 s  . co m
 *
 * @param bufferedImage original image
 * @return horizontally flipped image
 */
private static BufferedImage flipVertical(BufferedImage bufferedImage) {
    AffineTransform at = AffineTransform.getTranslateInstance(0, bufferedImage.getHeight());
    at.scale(1.0, -1.0);
    BufferedImageOp biOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    BufferedImage imgRes = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
            bufferedImage.getType());
    return biOp.filter(bufferedImage, imgRes);
}

From source file:net.sf.ginp.browser.FolderManagerImpl.java

void makeThumbImage(final File origPicture, final String thumbFileName, final int maxThumbSize) {
    if (log.isDebugEnabled()) {
        log.debug("makeThumbImage: origFileName=" + origPicture.getAbsolutePath() + " thumbFileName="
                + thumbFileName + " maxThumbSize=" + maxThumbSize);
    }/*from w ww.j  a va 2  s.co m*/

    // Only jpegs supported.
    if ((origPicture.getName().toLowerCase()).endsWith(".jpg")
            || (origPicture.getName().toLowerCase()).endsWith(".jpeg")) {
        try {
            // thumb it.
            JPEGImageDecoder dc = JPEGCodec.createJPEGDecoder((new FileInputStream(origPicture)));
            BufferedImage origImage = dc.decodeAsBufferedImage();
            int origHeight = origImage.getHeight(null);
            int origWidth = origImage.getWidth(null);
            int scaledW = 0;
            int scaledH = 0;
            double scale = 1.0;

            if (origHeight < origWidth) {
                scale = (double) maxThumbSize / (double) origWidth;
            } else {
                scale = (double) maxThumbSize / (double) origHeight;
            }

            scaledW = (int) (scale * origWidth);
            scaledH = (int) (scale * origHeight);

            //AffineTransform  at  = new AffineTransform();
            AffineTransform tx;
            AffineTransformOp af;
            JPEGImageEncoder encoder;
            BufferedImage outImage;

            outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB);
            tx = new AffineTransform();
            tx.scale(scale, scale);
            af = new AffineTransformOp(tx, null);
            af.filter(origImage, outImage);

            File ginpFolder = new File(
                    thumbFileName.substring(0, thumbFileName.lastIndexOf("/.ginp")) + "/.ginp");

            if (!(ginpFolder.exists())) {
                ginpFolder.mkdir();
            }

            encoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(thumbFileName));
            encoder.encode(outImage);
        } catch (Exception e) {
            log.error("Error Makeing Thumb Image " + thumbFileName, e);
        }
    }
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage rotateImage(BufferedImage img, double angle, int type, Color fillBgColor) {
    if (img == null) {
        return null;
    }/* w  w w. j  a v a  2s. c  o m*/

    if (angle > 360.0 || angle < -360) {
        angle = angle % 360.0;
    }

    if (angle < 0) {
        angle = 360 + angle;
    }

    if (angle == 0.0 || angle == 360.0) {
        return img;
    }

    //System.out.println("angle="+angle);

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

    /*
    AffineTransform tr = new AffineTransform();
    tr.rotate(theta,w/2,h/2);
    BufferedImageOp op = new AffineTransformOp(tr, type);
    BufferedImage out = op.filter(img,null);
     */
    /*
    AffineTransform tr = new AffineTransform();
    tr.rotate(theta, w/2.0, h/2.0);
    AffineTransform translationTransform = findTranslation(tr, img);
    tr.preConcatenate(translationTransform);
    BufferedImageOp op = new AffineTransformOp(tr, type);
            
    BufferedImage out = op.filter(img,null);
     */
    BufferedImage out = null;
    if (angle == 90.0 || angle == 180.0 || angle == 270.0) {
        switch ((int) angle) {
        case 90:
            out = new BufferedImage(h, w, img.getType());
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    out.setRGB(h - y - 1, x, img.getRGB(x, y));
                }
            }
            break;
        case 180:
            out = new BufferedImage(w, h, img.getType());
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    out.setRGB(w - x - 1, h - y - 1, img.getRGB(x, y));
                }
            }
            break;
        case 270:
            out = new BufferedImage(h, w, img.getType());
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    out.setRGB(y, w - x - 1, img.getRGB(x, y));
                }
            }
            break;
        }
    } else {
        double theta = angle * Math.PI / 180.0;
        int neww = w, newh = h;
        double dx = 0.0, dy = 0.0;
        double s = Math.sin(theta);
        double c = Math.cos(theta);
        if (angle > 0.0 && angle < 90.0) {
            neww = (int) Math.round(((double) w) * c + ((double) h) * s);
            newh = (int) Math.round(((double) w) * s + ((double) h) * c);
            dx = ((double) h) * s;
            dy = 0.0;
        } else if (angle > 90.0 && angle < 180.0) {
            neww = (int) Math.round(-((double) w) * c + ((double) h) * s);
            newh = (int) Math.round(((double) w) * s - ((double) h) * c);
            dx = -((double) w) * c + ((double) h) * s;
            dy = -((double) h) * c;
        } else if (angle > 180.0 && angle < 270.0) {
            neww = (int) Math.round(-((double) w) * c - ((double) h) * s);
            newh = (int) Math.round(-((double) w) * s - ((double) h) * c);
            dx = -((double) w) * c;
            dy = -((double) w) * s - ((double) h) * c;
        } else if (angle > 270.0 && angle < 360.0) {
            neww = (int) Math.round(((double) w) * c - ((double) h) * s);
            newh = (int) Math.round(-((double) w) * s + ((double) h) * c);
            dx = 0.0;
            dy = -((double) w) * s;
        }

        AffineTransform tr = new AffineTransform();
        tr.translate(dx, dy);
        tr.rotate(theta);
        BufferedImageOp op = new AffineTransformOp(tr, type);
        out = new BufferedImage(neww, newh, img.getType());
        Graphics2D g2d = (Graphics2D) out.getGraphics();
        Rectangle clear = new Rectangle(0, 0, out.getWidth(), out.getHeight());
        g2d.setPaint(fillBgColor);
        g2d.fill(clear);
        op.filter(img, out);
    }
    return out;
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage translateImage(BufferedImage img, double dx, double dy, int type,
        Color fillBgColor) {//from  w  ww . j  ava2s.c  o m
    if (img == null) {
        return null;
    }

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

    BufferedImage out = null;
    AffineTransform tr = new AffineTransform();
    tr.translate(dx, dy);
    BufferedImageOp op = new AffineTransformOp(tr, type);
    out = new BufferedImage(w, h, img.getType());
    Graphics2D g2d = (Graphics2D) out.getGraphics();
    Rectangle clear = new Rectangle(0, 0, out.getWidth(), out.getHeight());
    g2d.setPaint(fillBgColor);
    g2d.fill(clear);
    op.filter(img, out);
    return out;
}

From source file:com.aimluck.eip.fileupload.util.FileuploadUtils.java

public static BufferedImage transformImage(BufferedImage image, AffineTransform transform, int newWidth,
        int newHeight) throws Exception {

    AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);

    BufferedImage destinationImage = new BufferedImage(newWidth, newHeight, image.getType());
    Graphics2D g = destinationImage.createGraphics();
    g.setColor(Color.WHITE);// w ww. j  av  a 2  s .c o m

    destinationImage = op.filter(image, destinationImage);

    return destinationImage;
}

From source file:lucee.runtime.img.Image.java

public void grayscale() throws ExpressionException {
    BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    Graphics2D graphics = img.createGraphics();
    graphics.drawImage(image(), new AffineTransformOp(AffineTransform.getTranslateInstance(0.0, 0.0), 1), 0, 0);
    graphics.dispose();/*  w w  w .  ja  va  2  s  .co  m*/
    image(img);
}

From source file:lucee.runtime.img.Image.java

public void rgb() throws ExpressionException {
    BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
    Graphics2D graphics = img.createGraphics();
    graphics.drawImage(image(), new AffineTransformOp(AffineTransform.getTranslateInstance(0.0, 0.0), 1), 0, 0);
    graphics.dispose();/*from  w  w  w .  j  av a 2  s  .  c  o m*/
    image(img);

}

From source file:lucee.runtime.img.Image.java

public void threeBBger() throws ExpressionException {
    BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D graphics = img.createGraphics();
    graphics.drawImage(image(), new AffineTransformOp(AffineTransform.getTranslateInstance(0.0, 0.0), 1), 0, 0);
    graphics.dispose();//from  w ww .j ava 2  s .c  om
    image(img);
}