黑莓中的棕褐色图像效果

时间:2012-03-30 06:29:26

标签: blackberry

我正在尝试将棕褐色效果应用于黑莓中的图像。 我尝试过,但没有得到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;
}

1 个答案:

答案 0 :(得分:1)

这个电话:

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

返回一个int []数组,其中每个int表示格式为0xAARRGGBB的颜色。这与您之前使用JavaSE的Raster类的代码不同。

编辑:为BlackBerry修复的方法:

    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;
    }