0

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

这是我的代码:

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
}

}

4

1 回答 1

1

检查这个网站。它有关于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;  
    }  
于 2012-12-24T01:27:04.270 回答