Java 如何将图像翻转过来?

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

我想知道我是否能在这个问题上找到一些帮助。我被要求使用一个图像(“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(); 
  }
}
import images.APImage;
导入图像。像素;
公共类Test2
{ 
公共静态void main(字符串[]args)
{ 
APImage image=newapimage(“corn.jpg”);
int width=image.getImageWidth();
int height=image.getImageHeight();
int middle=高度/2;
//需要将下半部分的像素与上半部分的像素进行切换
//图像的上半部分
对于(int y=0;y中间;h++)
{ 
对于(int w=0;w

谢谢你的帮助

无论何时交换变量,如果您的语言不允许同时赋值(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在comments:p

中所说的内容如果您能够使用Graphics类,则以下内容可能有用:

以及图形类文档: 而不是使用

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()); 
您应该将下半部的RGB存储到一个临时像素中,并在替换下半部的值后使用该像素设置上半部(如果您遵循)。你也可以用这样的东西。。。。假设您的像素在整数rgb值上运行(这将改进您的主方法)


您希望将图像翻转过来,而不是交换上半部分和下半部分。 循环可以是这样的

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--;
}
int-topRow=0;
int bottomRow=高度-1;
while(顶行<底行){
对于(int x=0;x
请参阅


h++
应该是
h--
因为你要在负轴上向上缩放图像假设这是文档,那么op应该使用
getPixel()
setPixel()
并且不要尝试交换对pixelspublic静态void main的引用(字符串[]args){int a=100;int b=200;a^=b;b^=a;a^=b;System.out.println(“a=“+a+”,b=“+b”);}@ElliottFrisch:这是一种破解,只适用于整数。如果你能像C一样将其转换为位,那么它适用于任何适合
长这很有道理。谢谢你的帮助!
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--;
}
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);
    }
}