如何将图像翻转?

时间:2013-11-18 04:06:14

标签: java image

我想知道我是否能在这个问题上找到一些帮助。我被要求使用图像(“corn.jpg”),并将其完全翻转。我知道我需要编写一个程序,它会从左下角切换左下角的像素,依此类推,但是在时间用完之前我无法使程序正常工作。任何人都可以提供一些提示或建议来解决这个问题吗?我希望能够自己编写代码,所以建议只是请。请注意,我对APImage和Pixel的了解非常有限。我用Java编程。 这是我设法完成的。

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(); 
  }
}

感谢您的帮助!

5 个答案:

答案 0 :(得分:2)

请参阅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);
    }
}

答案 1 :(得分:1)

每当您交换变量时,如果您的语言不允许同时分配(而Java不允许),则需要使用临时变量。

考虑一下:

a = 1;
b = 2;

a = b; // a is now 2, just like b
b = a; // b now uselessly becomes 2 again

而不是那样做,这样做:

t = a; // t is now 1
a = b; // a is now 2
b = t; // b is now 1

编辑:还有@vandale在评论中说的话:P

答案 2 :(得分:0)

如果您能够使用Graphics类,可能会使用以下内容: http://www.javaworld.com/javatips/jw-javatip32.html

以及Graphics类文档: http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html

答案 3 :(得分:0)

而不是使用

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()); 

您应该将bottomHalf的RGB存储到临时Pixel中,并在替换bottomHalf值后使用它来设置topHalf(如果您遵循)。您也可以使用类似的东西....假设您的像素在整数rgb值上运行(这会改善您的主方法)。

 private static final Pixel updateRGB(Pixel in, int red, int green, int blue) {
   in.setRed(red); in.setGreen(green); in.setBlue(blue);
 }

答案 4 :(得分:0)

您想要将图像上下颠倒,而不是交换上半部分和下半部分。 循环可能看起来像这样。

int topRow = 0;
int bottomRow = height-1;
while(topRow < bottomRow) {
    for(int x = 0; x < width; x++) {
        Pixel t = image.getPixel(x, topRow);
        image.setPixel(x, topRow, image.getPixel(x, bottomRow));
        image.setPixel(x, bottomRow, t);
    }
    topRow++;
    bottomRow--;
}