2

在我的应用程序中,我想编辑亮度、对比度等图像。我有一些教程,我正在尝试这个来改变对比度

public static Bitmap createContrast(Bitmap src, double value) {
        // image size
        int width = src.getWidth();
        int height = src.getHeight();
        // create output bitmap
        Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
        // color information
        int A, R, G, B;
        int pixel;
        // get contrast value
        double contrast = Math.pow((100 + value) / 100, 2);

        // scan through all pixels
        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                // get pixel color
                pixel = src.getPixel(x, y);
                A = Color.alpha(pixel);
                // apply filter contrast for every channel R, G, B
                R = Color.red(pixel);
                R = (int)(((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
                if(R < 0) { R = 0; }
                else if(R > 255) { R = 255; }

                G = Color.red(pixel);
                G = (int)(((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
                if(G < 0) { G = 0; }
                else if(G > 255) { G = 255; }

                B = Color.red(pixel);
                B = (int)(((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
                if(B < 0) { B = 0; }
                else if(B > 255) { B = 255; }

                // set new pixel color to output bitmap
                bmOut.setPixel(x, y, Color.argb(A, R, G, B));

            }
        }


        // return final image
        return bmOut;

称它为:

ImageView image = (ImageView)(findViewById(R.id.image));
        //image.setImageBitmap(createContrast(bitmap));

但我没有看到图像发生任何问题。你能帮我在哪里出错吗?

我从 APi 14 看到了 effectFactory。是否有类似的/任何教程可用于旧版本的图像处理

4

1 回答 1

0

这种方法存在三个基本问题。前两个是编码问题。首先,您总是调用,并且在您的代码中Color.red没有Color.green和。Color.blue第二个问题是这种计算过于重复。您假设颜色在 [0, 255] 范围内,因此创建一个包含 256 个位置的数组并为 [0, 255] 中的每个位置计算对比度要快得多i

第三个问题更成问题。你为什么考虑这个算法来提高对比度?结果对 RGB 毫无意义,您可能会在不同的颜色系统中得到更好的结果。以下是您应该期望的结果,您的参数value为 0、10、20 和 30:

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

这是执行该操作的示例 Python 代码:

import sys
from PIL import Image

img = Image.open(sys.argv[1])
width, height = img.size

cvalue = float(sys.argv[2]) # Your parameter "value".
contrast = ((100 + cvalue) / 100) ** 2

def apply_contrast(c):
    c = (((c / 255.) - 0.5) * contrast + 0.5) * 255.0
    return min(255, max(0, int(c)))

# Build the lookup table.
ltu = []
for i in range(256):
    ltu.append(apply_contrast(i))

# The following "point" method applies a function to each
# value in the image. It considers the image as a flat sequence
# of values.
img = img.point(lambda x: ltu[x])

img.save(sys.argv[3])
于 2012-12-21T13:59:20.117 回答