2012-12-24 82 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; 
    }