2017-01-26 29 views
0

我想旋轉圖像沒有標準方法,使顏色數組和操作它,但是當我調用,旋轉我得到黑點(看圖片)我試圖旋轉沒有lib,但它使黑色點在圖片

enter image description here

這裏是我的代碼,colScaled是我試圖轉換成數組圖片:

public void arrays() { 
    colScaled = zoom2(); 
    int j = 0; 
    int i = 0; 
    angel = Integer.parseInt(this.mn.jTextField1.getText()); 
    float degree = (float) Math.toRadians(angel); 
    float cos = (float) Math.cos(degree); 
    float sin = (float) Math.sin(degree); 
    int W = Math.round(colScaled[0].length * Math.abs(sin) + colScaled.length * Math.abs(cos)); 
    int H = Math.round(colScaled[0].length * Math.abs(cos) + colScaled.length * Math.abs(sin)); 
    int x; 
    int y; 
    int xn = (int) W/2; 
    int yn = (int) H/2; 
    int hw = (int) colScaled.length/2; 
    int hh = (int) colScaled[0].length/2; 
    BufferedImage image = new BufferedImage(W + 1, H + 1, im.getType()); 
    for (i = 0; i < colScaled.length; i++) { 
     for (j = 0; j < colScaled[0].length; j++) { 
      x = Math.round((i - hw) * cos - (j - hh) * sin + xn); 
      y = Math.round((i - hw) * sin + (j - hh) * cos + yn); 
      image.setRGB(x, y, colScaled[i][j]); 
     } 
    } 
    ImageIcon ico = new ImageIcon(image); 
    this.mn.jLabel1.setIcon(ico); 
} 
+6

[Java像素數組旋轉]的可能重複(http://stackoverflow.com/questions/29739809/java-rotation-of-pixel-array) – Spektre

+0

「黑色點」在哪裏?在圖像中還是在圖像周圍? – gpasch

+0

在圖像中,周圍不是問題 –

回答

0

聲明本塊代碼: -

for (i = 0; i < colScaled.length; i++) { 
    for (j = 0; j < colScaled[0].length; j++) { 
     x = Math.round((i - hw) * cos - (j - hh) * sin + xn); 
     y = Math.round((i - hw) * sin + (j - hh) * cos + yn); 
     image.setRGB(x, y, colScaled[i][j]); 
    } 
} 

xy是在圖像(colScaled)像素座標。
此代碼的目的是填充目的地圖像(image)中的所有像素。

在循環中,不能保證目標圖像中的所有像素都將被填充,即使它位於矩形區域中。

enter image description here

上述圖像描繪的問題。
請參閱?有可能不會寫入目的地圖像中的紅色像素。

正確的解決方案是在迭代目的地圖像像素,則在圖像以後找到一個對應的像素。

編輯:張貼後,我只看到Spektre的評論。
我同意,這似乎是一個重複的問題。 「像素陣列」這個詞讓我覺得它不是。