2012-03-30 70 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