在 Android 中使用 colorMatrix 使黑色变暗

发布于 2024-10-30 14:19:28 字数 1067 浏览 3 评论 0原文

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

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

I currently have the following code to get a Bitmap object, strip the color and then turn it red which works but I need the darker elements of the image to come through as darker, at the moment it's like somebody put a red film over the image, which is almost what I want but need the blacks to be darker:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半暖夏伤 2024-11-06 14:19:29

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

Last column of first three raws in color matrix change brightness of image. Its changed between [-255...255]. -255 will gave you black image and 255 will make it white.
This method can change you contrast. Than bright object will become more bighter and dark more darker. Than you can set you brightness to requared poisition. Contrast changed between [-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 });
        }
第七度阳光i 2024-11-06 14:19:28
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};
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};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文