如何让我的图像翻转?

时间:2012-12-24 00:21:33

标签: java image

我在翻转图像时遇到问题。我的程序应该显示默认图像和翻转图像。我想如果我可以用原始图片的(width-1,height-1)替换翻转图片的(0,0)像素,它会起作用,但不是得到original image,而是得到this

这是我的代码:

import java.awt.Color;

public class Horizontal {

public static void main(String[] args)
{
    Picture source = new Picture(args[0]);//name of picture.
    Picture flip = new Picture(source.width(), source.height());//sets the width and height of source

    for (int i =0; i < source.width(); i++)
    {
        int w = 1;
        int sw = source.width()-w;
        for (int j = 0; j < source.width(); j++)
        {
            int h=1;
            int sh =  source.height()-h;
            Color SourceColor =  source.get(sw,sh);// return the the color pixel of (sw,sh)
            flip.set(i, j, SourceColor);//suppose to replace the (i,j) pixel of flip with source's (sw,sh) pixel
            h++;
        }
        w++;
    }
    source.show();// shows the original image
    flip.show(); // shows flipped version of image
}

}

1 个答案:

答案 0 :(得分:1)

Chceck这个网站。它有很多关于java中基本图像算法的信息。 您可以复制用于翻转图像的代码。

http://www.javalobby.org/articles/ultimate-image/#9

水平翻转:

public static BufferedImage horizontalflip(BufferedImage img) {  

    int w = img.getWidth();  
    int h = img.getHeight();  
    BufferedImage dimg = new BufferedImage(w, h, img.getType());  
    Graphics2D g = dimg.createGraphics();  
    g.drawImage(img, 0, 0, w, h, w, 0, 0, h, null);  
    g.dispose();  
    return dimg;  
}  

垂直翻转:

public static BufferedImage verticalflip(BufferedImage img) {  
        int w = img.getWidth();  
        int h = img.getHeight();  
        BufferedImage dimg = dimg = new BufferedImage(w, h, img.getColorModel().getTransparency());  
        Graphics2D g = dimg.createGraphics();  
        g.drawImage(img, 0, 0, w, h, 0, h, w, 0, null);  
        g.dispose();  
        return dimg;  
    }