2012-03-30 69 views
1

我想在黑莓中的圖像上應用棕褐色效果。 我已經嘗試過,但沒有得到100%的棕褐色效果。 這是我嘗試過的棕褐色效果的代碼。我使用了getARGB()setARGB()位圖類的方法。棕褐色在黑莓中的圖像效果

public Bitmap changetoSepiaEffect(Bitmap bitmap) { 

    int sepiaIntensity=30;//value lies between 0-255. 30 works well 

    // Play around with this. 20 works well and was recommended 
    // by another developer. 0 produces black/white image 
    int sepiaDepth = 20; 

    int w = bitmap.getWidth(); 
    int h = bitmap.getHeight(); 

    // WritableRaster raster = img.getRaster(); 

    // We need 3 integers (for R,G,B color values) per pixel. 
    int[] pixels = new int[w*h*3]; 
    // raster.getPixels(0, 0, w, h, pixels); 

    bitmap.getARGB(pixels, 0, w, x, y, w, h); 
    // Process 3 ints at a time for each pixel. 
    // Each pixel has 3 RGB colors in array 
    for (int i=0;i<pixels.length; i+=3) { 
     int r = pixels[i]; 
     int g = pixels[i+1]; 
     int b = pixels[i+2]; 

     int gry = (r + g + b)/3; 
     r = g = b = gry; 
     r = r + (sepiaDepth * 2); 
     g = g + sepiaDepth; 

     if (r>255) r=255; 
     if (g>255) g=255; 
     if (b>255) b=255; 

     // Darken blue color to increase sepia effect 
     b-= sepiaIntensity; 

     // normalize if out of bounds 
     if (b<0) { 
      b=0; 
     } 
     if (b>255) { 
      b=255; 
     } 

     pixels[i] = r; 
     pixels[i+1]= g; 
     pixels[i+2] = b; 
    } 
    //raster.setPixels(0, 0, w, h, pixels); 
    bitmap.setARGB(pixels, 0, w, 0, 0, w, h); 
    return bitmap; 
} 
+0

你是什麼意思,它並沒有得到100%的棕褐色效果?它做什麼,你不想要? – 2012-03-30 06:39:20

+0

棕褐色效果意味着棕色+灰色的顏色....我沒有得到任何棕色的顏色 – 2012-03-30 07:07:33

+0

我已經使用過你的代碼,但是我的圖像保持原樣。沒有發生變化..但是當我返回原圖像,而不是新的image.It顯示一些效果。你在黑莓模擬器上測試你的發佈代碼。 – 2012-03-30 11:03:56

回答

1

此調用:

bitmap.getARGB(pixels, 0, w, x, y, w, h); 

返回int []數組,其中每個INT表示在格式0xAARRGGBB的顏色。這與使用JavaSE的Raster類的以前的代碼不同。

編輯:固定的黑莓手機的方法:

public static Bitmap changetoSepiaEffect(Bitmap bitmap) { 

     int sepiaIntensity = 30;// value lies between 0-255. 30 works well 

     // Play around with this. 20 works well and was recommended 
     // by another developer. 0 produces black/white image 
     int sepiaDepth = 20; 

     int w = bitmap.getWidth(); 
     int h = bitmap.getHeight(); 

     // Unlike JavaSE's Raster, we need an int per pixel 
     int[] pixels = new int[w * h]; 

     // We get the whole image 
     bitmap.getARGB(pixels, 0, w, 0, 0, w, h); 

     // Process each pixel component. A pixel comes in the format 0xAARRGGBB. 
     for (int i = 0; i < pixels.length; i++) { 
      int r = (pixels[i] >> 16) & 0xFF; 
      int g = (pixels[i] >> 8) & 0xFF; 
      int b = pixels[i] & 0xFF; 

      int gry = (r + g + b)/3; 
      r = g = b = gry; 
      r = r + (sepiaDepth * 2); 
      g = g + sepiaDepth; 

      if (r > 255) 
       r = 255; 
      if (g > 255) 
       g = 255; 
      if (b > 255) 
       b = 255; 

      // Darken blue color to increase sepia effect 
      b -= sepiaIntensity; 

      // normalize if out of bounds 
      if (b < 0) { 
       b = 0; 
      } 
      if (b > 255) { 
       b = 255; 
      } 

      // Now we compose a new pixel with the modified channels, 
      // and an alpha value of 0xFF (full opaque) 
      pixels[i] = ((r << 16) & 0xFF0000) | ((g << 8) & 0x00FF00) | (b & 0xFF) | 0xFF000000; 
     } 

     // We return a new Bitmap. Trying to modify the one passed as parameter 
     // could throw an exception, since in BlackBerry not all Bitmaps are modifiable. 
     Bitmap ret = new Bitmap(w, h); 
     ret.setARGB(pixels, 0, w, 0, 0, w, h); 
     return ret; 
    } 
+0

你的棕褐色代碼循環內應該工作。在'getARGB'方法調用中檢查x和y是否爲0。 – 2012-03-30 08:11:11

+0

此外,循環中的增量應從「i + = 3」變爲「i ++'。 – 2012-03-30 08:12:44

+0

代碼中仍然存在一些缺陷,因爲試圖修改位圖而不是返回新位圖,或者由於最後一行中的錯誤,像素是完全透明的。我會很快上傳一個測試過的代碼片段。 – 2012-03-30 09:35:14