[英]Get Dominant Color from Image in UWP C#
基本上我有一个 UWP 应用程序,它允许用户打开图像文件(.jpg、.jpeg、.png),我需要从该图像中检测主色。
我使用 System.Drawing 找到了 GDI+ 的解决方案,但在 UWP 中不可用。我在 Win2D 中找不到类似内容的任何参考。 那么有没有办法从直方图中获取主色呢?
我可以随时使用任何 Web 服务并通过 python 或 node.js 等完成此操作,但我想在 UWP 应用程序内本地完成,而不需要 inte.net。
任何帮助表示赞赏!
我们通过以下方式做到了:
对于每个像素:转换为HSL。 使用H和S确定颜色。 使用L确定强度。 在由H和S索引的2D矩阵中对a求和。
在矩阵中找到最大值。 那是你的主导色彩。
看看这个仓库 : ColorThief及其Nuget包 。 它支持UWP。 您也可以查看此答案以获取更多信息。
UWP Works 修改上面的代码
//ksemenenko.ColorThief Nuget
async Task<SolidColorBrush> GetIdealTextColor(StorageFile file)
{
var solid = new SolidColorBrush();
solid = TextBlockHelper._textBrush;
var random = RandomAccessStreamReference.CreateFromFile(file);
using (IRandomAccessStream randomAccessStream = await random.OpenReadAsync())
{
var decoder = await BitmapDecoder.CreateAsync(randomAccessStream);
var colorThief = new ColorThiefDotNet.ColorThief();
var quantizedColor = await colorThief.GetColor(decoder);
solid = IdealTextColor(quantizedColor.Color);
}
return solid;
}
public SolidColorBrush IdealTextColor(ColorThiefDotNet.Color bg)
{
int nThreshold = 105;
int bgDelta = Convert.ToInt32((bg.R * 0.299) + (bg.G * 0.587) +
(bg.B * 0.114));
var foreColor = (255 - bgDelta < nThreshold) ? new SolidColorBrush(Colors.Black) : new SolidColorBrush(Colors.White);
return foreColor;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.