Example usage for java.awt.geom AffineTransform scale

List of usage examples for java.awt.geom AffineTransform scale

Introduction

In this page you can find the example usage for java.awt.geom AffineTransform scale.

Prototype

@SuppressWarnings("fallthrough")
public void scale(double sx, double sy) 

Source Link

Document

Concatenates this transform with a scaling transformation.

Usage

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

/**
 * Flip the image horizontal//from   w w  w .  j  av a  2  s  .c  o  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   w  w w. j  a  va 2  s . c o 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:ch.rasc.downloadchart.DownloadChartServlet.java

private static void handlePdf(HttpServletResponse response, byte[] imageData, Integer width, Integer height,
        String filename, PdfOptions options) throws IOException {

    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + ".pdf\";");

    try (PDDocument document = new PDDocument()) {
        PDRectangle format = PDPage.PAGE_SIZE_A4;
        if (options != null) {
            if ("A3".equals(options.format)) {
                format = PDPage.PAGE_SIZE_A3;
            } else if ("A5".equals(options.format)) {
                format = PDPage.PAGE_SIZE_A5;
            } else if ("Letter".equals(options.format)) {
                format = PDPage.PAGE_SIZE_LETTER;
            } else if ("Legal".equals(options.format)) {
                format = new PDRectangle(215.9f * MM_TO_UNITS, 355.6f * MM_TO_UNITS);
            } else if ("Tabloid".equals(options.format)) {
                format = new PDRectangle(279 * MM_TO_UNITS, 432 * MM_TO_UNITS);
            } else if (options.width != null && options.height != null) {
                Float pdfWidth = convertToPixel(options.width);
                Float pdfHeight = convertToPixel(options.height);
                if (pdfWidth != null && pdfHeight != null) {
                    format = new PDRectangle(pdfWidth, pdfHeight);
                }//w  w w.  j  a  v a2s . c  om
            }
        }

        PDPage page = new PDPage(format);

        if (options != null && "landscape".equals(options.orientation)) {
            page.setRotation(90);
        }

        document.addPage(page);

        BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageData));

        try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {

            PDPixelMap ximage = new PDPixelMap(document, originalImage);

            Dimension newDimension = calculateDimension(originalImage, width, height);
            int imgWidth = ximage.getWidth();
            int imgHeight = ximage.getHeight();
            if (newDimension != null) {
                imgWidth = newDimension.width;
                imgHeight = newDimension.height;
            }

            Float border = options != null ? convertToPixel(options.border) : null;
            if (border == null) {
                border = 0.0f;
            }

            AffineTransform transform;

            if (page.getRotation() == null) {
                float scale = (page.getMediaBox().getWidth() - border * 2) / imgWidth;
                if (scale < 1.0) {
                    transform = new AffineTransform(imgWidth, 0, 0, imgHeight, border,
                            page.getMediaBox().getHeight() - border - imgHeight * scale);

                    transform.scale(scale, scale);
                } else {
                    transform = new AffineTransform(imgWidth, 0, 0, imgHeight, border,
                            page.getMediaBox().getHeight() - border - imgHeight);
                }

            } else {
                float scale = (page.getMediaBox().getHeight() - border * 2) / imgWidth;
                if (scale < 1.0) {
                    transform = new AffineTransform(imgHeight, 0, 0, imgWidth, imgHeight * scale + border,
                            border);

                    transform.scale(scale, scale);
                } else {
                    transform = new AffineTransform(imgHeight, 0, 0, imgWidth, imgHeight + border, border);
                }

                transform.rotate(1.0 * Math.PI / 2.0);
            }

            contentStream.drawXObject(ximage, transform);

        }

        try {
            document.save(response.getOutputStream());
        } catch (COSVisitorException e) {
            throw new IOException(e);
        }
    }
}

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

public static AffineTransform getExifTransformation(ImageInformation info) {

    AffineTransform t = new AffineTransform();
    if (info == null) {
        return t;
    }/*from   ww  w  .  j  a va  2s  .  c  om*/

    switch (info.orientation) {
    case 1:
        break;
    case 2: // Flip X
        t.scale(-1.0, 1.0);
        t.translate(-info.width, 0);
        break;
    case 3: // PI rotation
        t.translate(info.width, info.height);
        t.rotate(Math.PI);
        break;
    case 4: // Flip Y
        t.scale(1.0, -1.0);
        t.translate(0, -info.height);
        break;
    case 5: // - PI/2 and Flip X
        t.rotate(-Math.PI / 2);
        t.scale(-1.0, 1.0);
        break;
    case 6: // -PI/2 and -width
        t.translate(info.height, 0);
        t.rotate(Math.PI / 2);
        break;
    case 7: // PI/2 and Flip
        t.scale(-1.0, 1.0);
        t.translate(-info.height, 0);
        t.translate(0, info.width);
        t.rotate(3 * Math.PI / 2);
        break;
    case 8: // PI / 2
        t.translate(0, info.width);
        t.rotate(3 * Math.PI / 2);
        break;
    default:
        break;
    }

    return t;
}

From source file:Main.java

public void paint(Graphics g) {
    Shape shape = new Rectangle2D.Float(100, 50, 80, 80);

    Graphics2D g2 = (Graphics2D) g;

    AffineTransform at = new AffineTransform();
    at.scale(0.3, 0.2);

    g2.setTransform(at);//from   ww w .j  a  v a  2s .com
    g2.draw(shape);

}

From source file:Scale.java

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(new Color(150, 150, 150));
    g2d.fillRect(0, 0, 80, 50);//from   w ww . j a v  a 2 s.  com

    AffineTransform tx1 = new AffineTransform();
    tx1.translate(110, 20);
    tx1.scale(0.5, 0.5);

    g2d.setTransform(tx1);
    g2d.fillRect(0, 0, 80, 50);

    AffineTransform tx2 = new AffineTransform();
    tx2.translate(200, 20);
    tx2.scale(1.5, 1.5);

    g2d.setTransform(tx2);
    g2d.fillRect(0, 0, 80, 50);

}

From source file:BasicDraw.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform tx = new AffineTransform();

    double scalex = .5;
    double scaley = 1;
    tx.scale(scalex, scaley);

    g2d.setTransform(tx);/*from   w w w .  j a  v a  2  s  .c o  m*/

    g2d.drawImage(new ImageIcon("a.png").getImage(), tx, this);
}

From source file:RotateImage45Degrees.java

public RotateImage45Degrees(String imageFile) {
    addNotify();/*from w  w  w .j av a  2 s. c  om*/
    frameInsets = getInsets();
    inputImage = Toolkit.getDefaultToolkit().getImage(imageFile);

    MediaTracker mt = new MediaTracker(this);
    mt.addImage(inputImage, 0);
    try {
        mt.waitForID(0);
    } catch (InterruptedException ie) {
    }

    sourceBI = new BufferedImage(inputImage.getWidth(null), inputImage.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = (Graphics2D) sourceBI.getGraphics();
    g.drawImage(inputImage, 0, 0, null);

    AffineTransform at = new AffineTransform();

    // scale image
    at.scale(2.0, 2.0);

    // rotate 45 degrees around image center
    at.rotate(45.0 * Math.PI / 180.0, sourceBI.getWidth() / 2.0, sourceBI.getHeight() / 2.0);

    /*
     * translate to make sure the rotation doesn't cut off any image data
     */
    AffineTransform translationTransform;
    translationTransform = findTranslation(at, sourceBI);
    at.preConcatenate(translationTransform);

    // instantiate and apply affine transformation filter
    BufferedImageOp bio;
    bio = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);

    destinationBI = bio.filter(sourceBI, null);

    int frameInsetsHorizontal = frameInsets.right + frameInsets.left;
    int frameInsetsVertical = frameInsets.top + frameInsets.bottom;
    setSize(destinationBI.getWidth() + frameInsetsHorizontal, destinationBI.getHeight() + frameInsetsVertical);
    show();
}

From source file:ScalingMethods.java

/**
 * Draws the picture five times, using the five different scaling
 * approaches described in the book. All five look the same, since
 * all are using default (nearest neighbor) filtering during the
 * scale operation./*from   w  w w .j av a  2s.  c  o  m*/
 */
public void paintComponent(Graphics g) {
    int x = PADDING;
    int y = PADDING;

    // Simplest approach
    g.drawImage(picture, x, y, scaleW, scaleH, null);

    // Subregion approach
    x += scaleW + PADDING;
    g.drawImage(picture, x, y, x + scaleW, y + scaleH, 0, 0, picture.getWidth(), picture.getHeight(), null);

    // Graphics2D.scale approach
    x += scaleW + PADDING;
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.translate(x, y);
    g2d.scale(SCALE_FACTOR, SCALE_FACTOR);
    g2d.drawImage(picture, 0, 0, null);
    g2d.dispose();

    // AffineTransform.scale approach
    x += scaleW + PADDING;
    g2d = (Graphics2D) g.create();
    AffineTransform at = new AffineTransform();
    at.translate(x, y);
    at.scale(SCALE_FACTOR, SCALE_FACTOR);
    g2d.drawImage(picture, at, null);
    g2d.dispose();

    // getScaledInstance() approach
    x += scaleW + PADDING;
    Image scaledImg = picture.getScaledInstance(scaleW, scaleH, Image.SCALE_DEFAULT);
    g.drawImage(scaledImg, x, y, null);
}

From source file:org.apache.pdfbox.util.operator.pagedrawer.BeginInlineImage.java

/**
 * process : BI : begin inline image.//from   ww  w  .  j  a  v a  2s.  c o m
 * @param operator The operator that is being executed.
 * @param arguments List
 * @throws IOException If there is an error displaying the inline image.
 */
public void process(PDFOperator operator, List<COSBase> arguments) throws IOException {
    PageDrawer drawer = (PageDrawer) context;
    PDPage page = drawer.getPage();
    //begin inline image object
    ImageParameters params = operator.getImageParameters();
    PDInlinedImage image = new PDInlinedImage();
    image.setImageParameters(params);
    image.setImageData(operator.getImageData());
    BufferedImage awtImage = image.createImage(context.getColorSpaces());

    if (awtImage == null) {
        log.warn("BeginInlineImage.process(): createImage returned NULL");
        return;
    }
    int imageWidth = awtImage.getWidth();
    int imageHeight = awtImage.getHeight();
    double pageHeight = drawer.getPageSize().getHeight();

    Matrix ctm = drawer.getGraphicsState().getCurrentTransformationMatrix();
    int pageRotation = page.findRotation();

    AffineTransform ctmAT = ctm.createAffineTransform();
    ctmAT.scale(1f / imageWidth, 1f / imageHeight);
    Matrix rotationMatrix = new Matrix();
    rotationMatrix.setFromAffineTransform(ctmAT);
    // calculate the inverse rotation angle
    // scaleX = m00 = cos
    // shearX = m01 = -sin
    // tan = sin/cos
    double angle = Math.atan(ctmAT.getShearX() / ctmAT.getScaleX());
    Matrix translationMatrix = null;
    if (pageRotation == 0 || pageRotation == 180) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getXScale()),
                (float) (pageHeight - 2 * ctm.getYPosition() - Math.cos(angle) * ctm.getYScale()));
    } else if (pageRotation == 90 || pageRotation == 270) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getYScale()),
                (float) (pageHeight - 2 * ctm.getYPosition()));
    }
    rotationMatrix = rotationMatrix.multiply(translationMatrix);
    rotationMatrix.setValue(0, 1, (-1) * rotationMatrix.getValue(0, 1));
    rotationMatrix.setValue(1, 0, (-1) * rotationMatrix.getValue(1, 0));
    AffineTransform at = new AffineTransform(rotationMatrix.getValue(0, 0), rotationMatrix.getValue(0, 1),
            rotationMatrix.getValue(1, 0), rotationMatrix.getValue(1, 1), rotationMatrix.getValue(2, 0),
            rotationMatrix.getValue(2, 1));
    drawer.drawImage(awtImage, at);
}