How to flip an image horizontally

Judy Tran picture Judy Tran · Sep 24, 2012 · Viewed 17.1k times · Source

HiI was wondering how to flip and image horizontally, for a practce task I was given a code that reads an image, inverting it to an image indicating it's brightness from 0-5, I had to flip an image.

This is my code of my reading an image and drawing it

    public int[][] readImage(String url) throws IOException
{
    // fetch the image
    BufferedImage img = ImageIO.read(new URL(url));

    // create the array to match the dimensions of the image
    int width = img.getWidth();
    int height = img.getHeight();
    int[][] imageArray = new int[width][height];

    // convert the pixels of the image into brightness values
    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            // get the pixel at (x,y) 

            int rgb = img.getRGB(x,y);
            Color c = new Color(rgb);
            int red = c.getRed();
            int green = c.getGreen();
            int blue = c.getBlue();

            // convert to greyscale
            float[] hsb = Color.RGBtoHSB(red, green, blue, null);                
            int brightness = (int)Math.round(hsb[2] * (PIXEL_CHARS.length - 1));

            imageArray[x][y] = brightness;
        }
    }
    return imageArray;
}

public void draw() throws IOException
{
    int[][] array = readImage("http://sfpl.org/images/graphics/chicklets/google-small.png");
    for(int i=0; i<array.length; i++)
    {
        for(int pic=0; pic<array[i].length; pic++)
        {
            if(array[pic][i] == 0)
            {
                System.out.print("X");
            }
            else if(array[pic][i] == 1)
            {
                System.out.print("8");
            }

            else if(array[pic][i] == 2)
            {
                System.out.print("0");
            }       

            else if(array[pic][i] == 3)
            {
                System.out.print(":");
            }

            else if(array[pic][i] == 4)
            {
                System.out.print(".");
            }

            else if (array[pic][i] == 5)
            {
                System.out.print(" ");
            }

            else 
            {
                System.out.print("error");
                break;
            }   

        }
        System.out.println();
    }
}    

and this is the code I tried to create to horizontally flip it,

void mirrorUpDown()
{
    int[][] array = readImage("http://sfpl.org/images/graphics/chicklets/google-small.png");
    int i = 0;

    for (int x = 0; x < array.length; x++)
    {
        for (int y = 0; y < array[i].length; y++)
        {{
                int temp = array[x][y]; 
                array[x][y]= array[-x][y]; 
                array[array[i].length-x][y]=temp;
            }
        }
    }

}    

I get an error

 unreported exception java.io.IException;
 must be caught or declared to be thrown

Answer

eProw picture eProw · Sep 4, 2017

I'd actually do it by this way...

    BufferedImage flip(BufferedImage sprite){
        BufferedImage img = new BufferedImage(sprite.getWidth(),sprite.getHeight(),BufferedImage.TYPE_INT_ARGB);
        for(int xx = sprite.getWidth()-1;xx>0;xx--){
            for(int yy = 0;yy < sprite.getHeight();yy++){
                img.setRGB(sprite.getWidth()-xx, yy, sprite.getRGB(xx, yy));
            }
        }
    return img;
}

Just a loop whose x starts at the end of the first image and places its rgba value on the flipped position of the second image. Clean, easy code :)