2012-12-24 50 views
0

我遇到了一个让图像翻转的问题。我的程序应该显示默认图像和翻转的图像。我认为,如果我可以用原始图片的(宽度为1,高度为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 
} 

}

+0

您的图片被打破......无法看到它的样子 – Chanckjh

+0

好了,现在就来试试吧。 – iii

+0

这通常使用一些'AffineTransfroem'实例来完成,如下所示(例如)[this answer](http://stackoverflow.com/questions/13742365/how-do-i-flip-an-image-horizo​​ntally -flip与 - glreadpixels-的BufferedImage-和O/13756357#13756357)。顺便说一句 - 什么是“图片”类,它来自哪里?为了更快地获得更好的帮助,请发布[SSCCE](http://sscce.org/)。 –

回答

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