Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I flip an image upside-down?

Tags:

java

image

I was wondering if I could find some help on this problem. I was asked to use an image ("corn.jpg"), and flip it entirely upside down. I know I need to write a program which will switch pixels from the top left corner with the bottom left, and so on, but I wasn't able to get my program to work properly before time ran out. Could anyone provide a few tips or suggestions to solve this problem? I'd like to be able to write my code out myself, so suggestions only please. Please note that my knowledge of APImage and Pixel is very limited. I am programming in Java. Here is what I managed to get done.

import images.APImage; 
import images.Pixel; 
public class Test2 
{ 
  public static void main(String [] args) 
  { 
    APImage image = new APImage("corn.jpg"); 
    int width = image.getImageWidth(); 
    int height = image.getImageHeight(); 
    int middle = height / 2; 
    //need to switch pixels in bottom half with the pixels in the top half 

    //top half of image 
    for(int y = 0; y < middle; y++) 
    { 
      for (int x = 0; x < width; x++) 
      { 
        //bottom half of image 
        for (int h = height; h > middle; h++) 
        { 
          for(int w = 0; w < width; w++) 
          { 
            Pixel bottomHalf = image.getPixel(h, w); 
            Pixel topHalf = image.getPixel(x, y); 
            //set bottom half pixels to corresponding top ones? 
            bottomHalf.setRed(topHalf.getRed()); 
            bottomHalf.setGreen(topHalf.getGreen()); 
            bottomHalf.setBlue(topHalf.getBlue()); 
            //set top half pixels to corresponding bottom ones? 
            topHalf.setRed(bottomHalf.getRed()); 
            topHalf.setGreen(bottomHalf.getGreen()); 
            topHalf.setBlue(bottomHalf.getBlue()); 
          }
        }
      }
    }
    image.draw(); 
  }
}

Thank you for your help!

like image 237
lexi_lu_22 Avatar asked Nov 18 '13 04:11

lexi_lu_22


People also ask

How do I turn a picture upside down?

Click on the Picture Format tab at the top. Go to the Rotate Objects icon (it looks like a triangle) and click on the drop-down menu. You'll see options to rotate your image right 90 degrees, left 90 degrees, flip horizontal, or flip vertical. Make sure to click on the Flip Vertical option.

What are the two ways to flip a picture?

There are two ways to flip images, as known as flipping horizontally and flipping vertically. When you flip an image horizontally, you will create a water reflection effect; when you flip an image vertically, you will create a mirror reflection effect.

How do you flip mirror image?

To flip your images vertically or horizontally and achieve this mirrored effect, right-click on the image and select Edit Image. This will bring up an Edit Image menu where you will find the two Flip options: Flip Horizontal and Flip Vertical.


1 Answers

See Transforming Shapes, Text, and Images.

enter image description here

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class FlipVertical {

    public static BufferedImage getFlippedImage(BufferedImage bi) {
        BufferedImage flipped = new BufferedImage(
                bi.getWidth(),
                bi.getHeight(),
                bi.getType());
        AffineTransform tran = AffineTransform.getTranslateInstance(0, bi.getHeight());
        AffineTransform flip = AffineTransform.getScaleInstance(1d, -1d);
        tran.concatenate(flip);

        Graphics2D g = flipped.createGraphics();
        g.setTransform(tran);
        g.drawImage(bi, 0, 0, null);
        g.dispose();

        return flipped;
    }

    FlipVertical(BufferedImage bi) {
        JPanel gui = new JPanel(new GridLayout(1,2,2,2));

        gui.add(new JLabel(new ImageIcon(bi)));
        gui.add(new JLabel(new ImageIcon(getFlippedImage(bi))));

        JOptionPane.showMessageDialog(null, gui);
    }

    public static void main(String[] args) throws AWTException {
        final Robot robot = new Robot();
        Runnable r = new Runnable() {

            @Override
            public void run() {
                final BufferedImage bi = robot.createScreenCapture(
                        new Rectangle(0, 660, 200, 100));
                new FlipVertical(bi);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
like image 99
Andrew Thompson Avatar answered Oct 04 '22 09:10

Andrew Thompson