2015-02-06 70 views
0

我有一個指紋圖象作爲位圖然後轉動此代碼像素:的Android旋轉像素中的位圖,但是位圖被轉白/ mising

public Bitmap rotateImage(Bitmap rotateBmp) 
{ 
     double radians=Math.toRadians(90); 
     double cos, sin; 
     cos=Math.cos(radians); 
     sin=Math.sin(radians); 
     boolean rotatePix[][]=new boolean[width][height]; 

     for(int i=0;i<width;++i) 
     { 
      for(int j=0;j<height;++j) 
      { 
       int centerX=core.x, centerY=core.y; 
       int m=i - centerX; 
       int n=j - centerY; 
       int k=(int)(m * cos + n * sin); 
       int l=(int)(n * cos - m * sin); 

       k+=centerX; 
       l+=centerY; 



       if(!((k<0)||(k>width-1)||(k<0)||(k>height-1))) 
       { 
        try 
        { 
         rotatePix[k][l]=binaryMap[i][j]; 
        } 
        catch(Exception e) 
        { 
         Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); 
        } 
       } 
      } 
     } 

     for(int i=0;i<width;++i) 
     { 
      for(int j=0;j<height;++j) 
      { 
       if(rotatePix[i][j]==true) 
       { 
        rotateBmp.setPixel(i, j, Color.BLACK); 
       } 
       else 
       { 
        rotateBmp.setPixel(i, j, Color.WHITE); 
       } 
      } 
     } 

     return rotateBmp; 
    //} 
} 

但然後當我檢查的結果,黑色像素變得更小,我猜它會變成白色,因爲當我檢查X和Y座標中的計算時,它們中的許多具有相同的X和Y新座標,並且可能會將黑色像素更改爲白色。請告訴我如何以一個角度旋轉像素,但與之前的顏色相同。我附上結果給你看。非常感謝您的幫助...

Rotate Result

+0

如果看不到圖像,請在此處打開它:http://s22.postimg.org/fffg5ym5d/Screenshot_2015_02_06_09_53_10.png – user1290932 2015-02-06 03:22:07

+0

上述代碼的問題在於,當圖像被轉換時,xy像素會映射到新的位置這些不是整數,並且代碼將它們舍入爲最小整數值,正如答案中所建議的那樣,您最好使用Android的旋轉函數,它計算輸出圖像中每個像素的加權值。 – erdemoo 2015-09-02 10:49:48

回答

1

如果你想要做的只是旋轉位圖,你可以使用一個像Matrix我在下面做:

public Bitmap rotateBitmap (Bitmap rotateBmp) { 
    int rotationDegree = 90; 

    /* rotate the image based the rotation degree */ 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(rotationDegree); 

    rotateBmp = Bitmap.createBitmap(rotateBmp, 0, 0, rotateBmp.getWidth(), 
      rotateBmp.getHeight(), matrix, true); 

    // rotateBmp is now rotated by 90 degrees 
    return rotateBmp; 
} 

我希望這幫助。