简体   繁体   中英

Xamarin.Forms C# find dominant color of image or image byte[] array

I'm using Xamarin.Forms to develop a cross-platform app. What is the best approach using c# with Xamarin to find the dominant color of an image? I found an ios approach: https://github.com/mxcl/UIImageAverageColor/blob/master/UIImage%2BAverageColor.m but can't seem to convert to the c# equivalent. What is a good method? For my ios implementation I can use either a UIImage or a byte[] array.

Thanks for the help!

U can Try this (but it get a Bitmap object, hope that can help):

public static Color getDominantColor(Bitmap bmp)
        {
            //Used for tally
            int red = 0;
            int green = 0;
            int blue = 0;

            int acc = 0;

            for (int x = 0; x < bmp.Width; x++)
            {
                for (int y = 0; y < bmp.Height; y++)
                {
                    Color tmpColor = bmp.GetPixel(x, y);

                    red += tmpColor.R;
                    green += tmpColor.G;
                    blue += tmpColor.B;

                    acc++;
                }
            }

            //Calculate average
            red /= acc;
            green /= acc;
            blue /= acc;

            return Color.FromArgb(red, green, blue);
        }

Don't forget to Using :

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

Edit:

Couldn't find the exact solution for your Byte[] image representation, But I found this:

public static byte[] ImageToByteArray(Image image)
{
    ImageConverter myConverter = new ImageConverter();
    return (byte[])myConverter.ConvertTo(image typeof(byte[]));
}

The above code, as u can see, convert from Image to Byte[] .

Cheers!

This seems to be working. I need to run some tests to verify how effective and how fast is is, but in case anyone else wants something similar:

public static UIColor averageColor(UIImage image)
        {
            CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB ();
            byte[] rgba = new byte[4];
            CGBitmapContext context = new CGBitmapContext (rgba, 1, 1, 8, 4, colorSpace, CGImageAlphaInfo.PremultipliedLast);
            context.DrawImage(new RectangleF(0, 0, 1, 1), image.CGImage);

            if(rgba[3] > 0) {
                var alpha = ((float)rgba[3])/255.0;
                var multiplier = alpha/255.0;
                var color = new UIColor (
                    (float)(rgba [0] * multiplier),
                    (float)(rgba [1] * multiplier),
                    (float)(rgba [2] * multiplier),
                    (float)(alpha)
                );
                return color;
            }
            else {
                var color = new UIColor (
                    (float)(rgba [0] / 255.0),
                    (float)(rgba [1] / 255.0),
                    (float)(rgba [2] / 255.0),
                    (float)(rgba [3] / 255.0)
                );
                return color;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM