Search code examples
flutterflutter-image

invert image black to white in flutter


I have a bitmap image with black and white color I just need to invert the image and display how can I convert and display image. I have inverted bitmap image in java using this code can anyone suggest me how to do this in a flutter.

    private Bitmap createInvertedBitmap(Bitmap src) {
        ColorMatrix colorMatrix_Inverted =
                new ColorMatrix(new float[] {
                        -1,  0,  0,  0, 255,
                        0, -1,  0,  0, 255,
                        0,  0, -1,  0, 255,
                        0,  0,  0,  1,   0});

        ColorFilter ColorFilter_Sepia = new ColorMatrixColorFilter(
                colorMatrix_Inverted);

        Bitmap bitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint();

        paint.setColorFilter(ColorFilter_Sepia);
        canvas.drawBitmap(src, 0, 0, paint);

        return bitmap;
    }

Solution

  • Now it is possible to change/invert the color via ColorFiltered class

    You can refer to this blog for more details: https://www.burkharts.net/apps/blog/over-the-rainbow-colour-filters/

    Another solution

    You can try using ShaderMask class, and try passing it to the corresponding Shader, such as InvertColorShader. Currently, no shader can do this as of now.

    Also check out BlendMode class to achieve your desired result.