2012-09-24 117 views
3

HiI想知道如何水平翻轉和圖像,對於練習任務,我獲得了一個讀取圖像的代碼,將其反轉爲圖像,指示其亮度爲0-5,我不得不翻轉一個圖像。如何水平翻轉圖像

這是我的我的讀取圖像,並繪製它

public int[][] readImage(String url) throws IOException 
{ 
    // fetch the image 
    BufferedImage img = ImageIO.read(new URL(url)); 

    // create the array to match the dimensions of the image 
    int width = img.getWidth(); 
    int height = img.getHeight(); 
    int[][] imageArray = new int[width][height]; 

    // convert the pixels of the image into brightness values 
    for (int x = 0; x < width; x++) 
    { 
     for (int y = 0; y < height; y++) 
     { 
      // get the pixel at (x,y) 

      int rgb = img.getRGB(x,y); 
      Color c = new Color(rgb); 
      int red = c.getRed(); 
      int green = c.getGreen(); 
      int blue = c.getBlue(); 

      // convert to greyscale 
      float[] hsb = Color.RGBtoHSB(red, green, blue, null);     
      int brightness = (int)Math.round(hsb[2] * (PIXEL_CHARS.length - 1)); 

      imageArray[x][y] = brightness; 
     } 
    } 
    return imageArray; 
} 

public void draw() throws IOException 
{ 
    int[][] array = readImage("http://sfpl.org/images/graphics/chicklets/google-small.png"); 
    for(int i=0; i<array.length; i++) 
    { 
     for(int pic=0; pic<array[i].length; pic++) 
     { 
      if(array[pic][i] == 0) 
      { 
       System.out.print("X"); 
      } 
      else if(array[pic][i] == 1) 
      { 
       System.out.print("8"); 
      } 

      else if(array[pic][i] == 2) 
      { 
       System.out.print("0"); 
      }  

      else if(array[pic][i] == 3) 
      { 
       System.out.print(":"); 
      } 

      else if(array[pic][i] == 4) 
      { 
       System.out.print("."); 
      } 

      else if (array[pic][i] == 5) 
      { 
       System.out.print(" "); 
      } 

      else 
      { 
       System.out.print("error"); 
       break; 
      } 

     } 
     System.out.println(); 
    } 
}  

代碼,這是我試圖創建水平翻轉它的代碼,

void mirrorUpDown() 
{ 
    int[][] array = readImage("http://sfpl.org/images/graphics/chicklets/google-small.png"); 
    int i = 0; 

    for (int x = 0; x < array.length; x++) 
    { 
     for (int y = 0; y < array[i].length; y++) 
     {{ 
       int temp = array[x][y]; 
       array[x][y]= array[-x][y]; 
       array[array[i].length-x][y]=temp; 
      } 
     } 
    } 

}  

我得到一個錯誤

unreported exception java.io.IException; 
must be caught or declared to be thrown 
+0

什麼是你面臨的錯誤是請更新它在這裏? – gks

+0

http://webmuch.com/image-flip-using-jquery/ – Dgo

+0

我面對的錯誤是「未報告的異常java.io.IException;必須被捕獲或聲明爲拋出」@Stranger –

回答

1

函數mirrorUpDown(),在那裏添加一個拋出IOException異常。

而且要從中調用這些方法的功能,是否處理異常,不包含在try catch塊代碼或功能也被設置爲拋出IOException(的一方應該有)

+0

是的,抱歉,我添加了一個扔它所以它的工作,但我一直走出界限,因爲我做了一個-x,但我不知道如何解決它 應該我將-x更改爲array.length- x,但我仍然得到一個界限 –

+0

問題:循環外i = 0的目的是什麼。我將在整個函數中攜帶0值?內部循環將始終指向圖像數組的第一行。 也脫離了鍵:'array [x] [y] = array [-x] [y];舉一個例子x = 1,y = 1然後你試圖做數組[1] [1] = array [-1] [1]' 我不認爲數組帶有負行索引..做它? –

1

你的圖像應該如何知道它應該從imageArray獲取數據?相反,您應該訪問圖像的柵格並修改其中的數據。

void flip(BufferedImage image) { 
     WritableRaster raster = image.getRaster(); 
     int h = raster.getHeight(); 
     int w = raster.getWidth(); 
     int x0 = raster.getMinX(); 
     int y0 = raster.getMinY(); 
     for (int x = x0; x < x0 + w; x++){ 
      for (int y = y0; y < y0 + h/2; y++){ 
       int[] pix1 = new int[3]; 
       pix1 = raster.getPixel(x, y, pix1); 
       int[] pix2 = new int[3]; 
       pix2 = raster.getPixel(x, y0 + h - 1 - (y - y0), pix2); 
       raster.setPixel(x, y, pix2); 
       raster.setPixel(x, y0 + h - 1 - (y - y0), pix1); 
      } 
     } 
     return; 
    } 
1

對不起後來在這裏張貼這一年多,但它應該在一個階段幫助別人

try{ 
    java.awt.image.BufferedImage bi = javax.imageio.ImageIO.read(getClass().getResource("Your image bro.jpg")) ; 
int[] h = bi.getRGB(0, 0, bi.getWidth(), bi.getHeight(), null, 0, bi.getWidth()); 
int [] h1 = new int[h.length]; 
System.out.println(""+h.length); 
    for(int j = 0;500>j;j++){ 
    for(int i = 500;i>0;i--){ 
     h1[j*500+(500-i)] = h[(j*500)+(i-1)]; 
    } 
    } 
bi.setRGB(0, 0, bi.getWidth(), bi.getHeight(), h1, 0, bi.getWidth()); 
     } 
     catch(Exception e){e.printStackTrace();} 

讓我們打破代碼下來

java.awt.image.BufferedImage bi =javax.imageio.ImageIO.read(getClass().getResource("Your image bro.jpg")); 

嘗試讀取圖像並存儲將讀入的圖像寫入BufferedImage變量bi

int[] h = bi.getRGB(0, 0, bi.getWidth(), bi.getHeight(), null, 0, bi.getWidth()); 
    int [] h1 = new int[h.length]; 

實例化兩個數組,h是原始RGB數組,h1將是水平翻轉的RGB數組。

for(int j = 0;500>j;j++){ 
for(int i = 500;i>0;i--){ 
    h1[j*500+(500-i)] = h[(j*500)+(i-1)]; 
} 
} 

讓我們看東西尤其更緊密

h1[j*500+(500-i)] = h[(j*500)+(i-1)]; 

圖像被從位置0掃描; 0至x.length; y.length 但它是在一個coninual陣列掃描。因此我們使用僞數組來操縱圖像的翻轉。 j * 500引用Y值和(500-i)引用x值。

bi.setRGB(0, 0, bi.getWidth(), bi.getHeight(), h1, 0, bi.getWidth()); 

最後,圖像被存回到BufferedImage變量中。

請注意,500常數是指您的x分辨率的圖像。例如,1920 x 1080大小的圖像使用1920的最大值。邏輯是您自己決定的。

2

其實我通過這種方式做到這一點...

BufferedImage flip(BufferedImage sprite){ 
     BufferedImage img = new BufferedImage(sprite.getWidth(),sprite.getHeight(),BufferedImage.TYPE_INT_ARGB); 
     for(int xx = sprite.getWidth()-1;xx>0;xx--){ 
      for(int yy = 0;yy < sprite.getHeight();yy++){ 
       img.setRGB(sprite.getWidth()-xx, yy, sprite.getRGB(xx, yy)); 
      } 
     } 
    return img; 
} 

只是一個循環,其X開始在第一個圖像的末端並且對第二圖像的fliped位置的RGBA值。清潔,簡單的代碼:)