2011-04-06 18 views
0

我目前有以下代碼來獲取Bitmap對象,去掉顏色然後將其變爲紅色,但這需要圖像中較暗的元素像現在一樣變暗,此刻這就像有人把紅色薄膜上的形象,這是我想幾乎什麼,但需要的黑人更暗:在Android中使用colorMatrix製作黑色更暗

Bitmap sourceBitmap = BitmapFactory.decodeFile(imgPath); 
     float[] colorTransform = { 
       0, 1f, 0, 0, 0, 
       0, 0, 0f, 0, 0, 
       0, 0, 0, 0f, 0, 
       0, 0, 0, 1f, 0}; 
     ColorMatrix colorMatrix = new ColorMatrix(); 
     colorMatrix.setSaturation(0f); //Remove Colour 
     colorMatrix.set(colorTransform); //Apply the Red 

     ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix); 
     Paint paint = new Paint(); 
     paint.setColorFilter(colorFilter); 

     Display display = getWindowManager().getDefaultDisplay(); 

     Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, (int)(display.getHeight() * 0.15), display.getWidth(), (int)(display.getHeight() * 0.75));    

     image.setImageBitmap(resultBitmap); 

     Canvas canvas = new Canvas(resultBitmap); 
     canvas.drawBitmap(resultBitmap, 0, 0, paint); 
+1

黑色是黑色的。你能鏈接到一個之前/之後的樣本嗎? – 2011-04-06 16:47:55

回答

0
c = 2;//this will boost your contrast by 2x thus deepening the black (and lightning the white). I'm not sure why at 0 you have anything but black... maybe I don't understand the matrix as well as I think I do... I thought 1 (in place of my c) gets you the original colors. 

Anyway... give that a whirl. 


float[] colorTransform = { 
       c, 1f, 0, 0, 0, 
       0, c, 0f, 0, 0, 
       0, 0, c, 0f, 0, 
       0, 0, 0, 1f, 0}; 
0

在圖像的色彩矩陣改變亮度前三原糖的最後一列。它在[-255 ... 255]之間變化。 -255會給你黑色的圖像,255會使它變成白色。 這種方法可以改變你的對比度。比明亮的物體會變得更加黑暗和更深。比你可以設置你亮度要求採購。對比度在[-1 ... 1]之間變化。

private static void setContrast(ColorMatrix cm, float contrast) { 
       float scale = contrast + 1.f; 
        float translate = (-.5f * scale + .5f) * 255.f; 
       cm.set(new float[] { 
         scale, 0, 0, 0, translate, 
         0, scale, 0, 0, translate, 
         0, 0, scale, 0, translate, 
         0, 0, 0, 1, 0 }); 
     } 
相關問題