【问题标题】:Xamarin.Forms C# find dominant color of image or image byte[] arrayXamarin.Forms C# 查找图像或图像字节 [] 数组的主色
【发布时间】:2015-03-23 06:47:18
【问题描述】:

我正在使用 Xamarin.Forms 开发跨平台应用程序。使用带有 Xamarin 的 c# 来查找图像的主色的最佳方法是什么?我找到了一种 ios 方法:https://github.com/mxcl/UIImageAverageColor/blob/master/UIImage%2BAverageColor.m,但似乎无法转换为 c# 等效项。什么是好方法?对于我的 ios 实现,我可以使用 UIImage 或 byte[] 数组。

感谢您的帮助!

【问题讨论】:

  • 你知道字节的格式(rgb、argb等?),还是像JPG这样的压缩流?
  • 对于我的 iOS 实现(我使用自定义图像裁剪器控件来选择图像并裁剪;尚未开始我的 android 实现),我使用 UIImage 然后: data = image.AsPNG(); data.ToArray (); 来获取我的数组。

标签: c# ios xamarin xamarin.forms


【解决方案1】:

你可以试试这个(但它会得到一个Bitmap 对象,希望能有所帮助):

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

别忘了Using

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

编辑:

找不到您的Byte[] 图像表示的确切解决方案,但我找到了这个:

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

如您所见,上面的代码从Image 转换为Byte[]

干杯!

【讨论】:

  • 感谢@gran33,这看起来是一个非常优雅的解决方案,但不幸的是我不相信 Xamarin.Forms 支持 System.Drawing.Bitmap。我想知道是否可以使用图像的 byte[] 数组完成类似的操作?
  • 我有 byte[] 形式的数据。希望从数组数据中计算平均值。
【解决方案2】:

这似乎有效。我需要运行一些测试来验证效率和速度有多快,但以防其他人想要类似的东西:

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

【讨论】:

    猜你喜欢
    • 2016-09-20
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 2014-11-14
    • 2010-10-30
    • 2012-01-18
    相关资源
    最近更新 更多