【问题标题】:Make Black darker using colorMatrix in Android在 Android 中使用 colorMatrix 使黑色更深
【发布时间】:2011-04-06 16:08:50
【问题描述】:

我目前有以下代码来获取位图对象,去除颜色,然后将其变为红色,这可以工作,但我需要图像中较暗的元素变得更暗,此时就像有人放了红色胶片在图像上,这几乎是我想要的,但需要黑色更暗:

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

【问题讨论】:

  • 黑色就是黑色。您可以链接到之前/之后的示例吗?

标签: java android colors android-image colormatrix


【解决方案1】:
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};

【讨论】:

    【解决方案2】:

    颜色矩阵中前三个原始数据的最后一列改变了图像的亮度。它在 [-255...255] 之间变化。 -255 将为您提供黑色图像,而 255 将使其变为白色。 这种方法可以改变你的对比度。比明亮的物体会变得更亮,更暗的物体会更暗。比您可以将亮度设置为 requared 位置。对比度在 [-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 });
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      相关资源
      最近更新 更多