Java Image Flip flipImageVertically(Image img)

Here you can find the source of flipImageVertically(Image img)

Description

Flips an image vertically.

License

Open Source License

Parameter

Parameter Description
img The source image

Return

The image after flip

Declaration

public static Image flipImageVertically(Image img) 

Method Source Code

//package com.java2s;
/*//from w  ww . ja va 2  s  . c o m
 * #%L
 * Java Applet for biometric trait acquisition [http://www.biosignin.org]
 * ImageUtils.java is part of BioSignIn project
 * %%
 * Copyright (C) 2014 Innovery SpA
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import java.awt.Graphics2D;
import java.awt.Image;

import java.awt.image.BufferedImage;

public class Main {
    /**
     * Flips an image vertically. (Mirrors it)
     * 
     * @param img The source image
     * @return The image after flip
     */
    public static Image flipImageVertically(Image img) {
        int w = img.getWidth(null);
        int h = img.getHeight(null);
        BufferedImage bimg = toBufferedImage(getEmptyImage(w, h));
        Graphics2D g = bimg.createGraphics();
        g.drawImage(img, 0, 0, w, h, 0, h, w, 0, null);
        g.dispose();
        return toImage(bimg);
    }

    public static BufferedImage toBufferedImage(Image img) {
        long start = System.nanoTime();

        if (img instanceof BufferedImage) {
            return (BufferedImage) img;
        }
        // Create a buffered image with transparency
        BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null),
                BufferedImage.TYPE_INT_ARGB);
        // Draw the image on to the buffered image
        Graphics2D bGr = bimage.createGraphics();
        bGr.drawImage(img, 0, 0, null);
        bGr.dispose();
        long last = System.nanoTime();
        System.out.println("tablet toBufferedImage in " + ((last - start) / 1000000) + "ms");
        // Return the buffered image
        return bimage;
    }

    /**
     * Creates an empty image with transparency
     * 
     * @param width The width of required image
     * @param height The height of required image
     * @return The created image
     */
    public static BufferedImage getEmptyImage(int width, int height) {
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        return (img);
    }

    /**
     * Converts a given BufferedImage into an Image
     * 
     * @param bimage The BufferedImage to be converted
     * @return The converted Image
     */
    public static Image toImage(BufferedImage bimage) {
        // Casting is enough to convert from BufferedImage to Image
        Image img = (Image) bimage;
        return img;
    }
}

Related

  1. flipImage(int stride, int height, int b[])
  2. flipImage(Object nativeImage, boolean flipHorizontal, boolean flipVertical)
  3. flipPixels(int[] imgPixels, int imgw, int imgh)