2016-08-24 22 views
0

我目前正在試圖端口的圖像編輯應用從Java到C#,我已經從以下方法java.awt.image.BandedSampleModel中包碰到的問題:Java的圖像採集功能在C#類似物

  • INT的getSample(INT的x,INT Y,INT帶,int值)
  • 空隙setSample(INT的x,INT Y,INT帶,int值)

原始Java代碼:

public int getSample(int x, int y, int band) 
    { 
     return image.getRaster().getSample(x, y, band); 
    } 

    public void setSample(int x, int y, int band, int value) 
    { 
     image.getRaster().setSample(x, y, band, value); 
    } 

的問題是以下幾點:

  1. 什麼是真正getSample和和setSample方法呢?據我瞭解,它們用於獲取並設置(x,y)位置的像素值(R + G + B的總和)。 'int band'包含在方法的簽名中是什麼?
  2. 據我所知,在默認的C#API中沒有模擬函數。如果是這樣,那麼這些函數應該如何在C#上重寫爲具有相同的簽名和行爲?

代碼示例非常感謝。

回答

1
  1. 看過這些函數在Java應用程序中的用法後,我發現'band'參數可以在[0,2]的範圍內。此參數指定RGB通道號(0 - 紅色,1 - 貪婪,2 - 藍色)。
  2. 正如我設法找出這種方法的目的,我很清楚該如何重寫它們。我已經成功地移植了應用程序,這兩種方法(在C#和Java上)都具有相同的行爲。

    public int getSample(int x, int y, int band) 
    { 
        //return image.getRaster().getSample(x, y, band); 
        var pixelColor = image.GetPixel(x, y); 
        switch (band) 
        { 
         case 0: return pixelColor.R; 
         case 1: return pixelColor.G; 
         case 2: return pixelColor.B; 
        } 
        throw new ArgumentException(nameof(band)); 
    } 
    
    public void setSample(int x, int y, int band, int value) 
    { 
        //image.getRaster().setSample(x, y, band, value); 
        var oldColor = image.GetPixel(x, y); 
        Color newColor; 
        switch (band) 
        { 
         case 0: 
          newColor = Color.FromArgb(255, value, oldColor.G, oldColor.B); 
          break; 
         case 1: 
          newColor = Color.FromArgb(255, oldColor.R, value, oldColor.B); 
          break; 
         case 2: 
          newColor = Color.FromArgb(255, oldColor.R, oldColor.G, value); 
          break; 
         default: 
          throw new ArgumentException(nameof(band)); 
        } 
        image.SetPixel(x, y, newColor); 
    }