首页 > 解决方案 > Xamarin Forms 项目中的 Imread 方法找不到我的图像

问题描述

我正在尝试在我的应用程序上使用否定图像。该图像位于 android 项目的 drawable 文件夹中。

我试过只使用图像的名称,就像图像xaml一样:

<Image x:Name="img"
                Source="thanos.jpg" />

C#:

Mat image = CvInvoke.Imread("thanos.jpg", ImreadModes.AnyColor);

但我得到了例外:System.ArgumentException : File thanos.jpg do not exist

我试过在路径中添加drawable、资源、/、\,但仍然是同样的错误。

有任何想法吗 ?

标签: c#androidxamarin.formsemgucv

解决方案


此解决方案仅适用于 android atm。

首先,添加 android 路径private const string pathAndroid = "/storage/emulated/0/Android/data/com.companyname.**YourAppName**/files",因为 System.Environment.GetFolderPath(System.Environment.SpecialFolder.**WhateverYouChooseThere**)链接到其他文件夹。

然后 Imread 确实需要文件,所以我直接在手机中添加了文件(我的应用程序已经包含图像),并将路径与文件名结合起来:

var pathfile = Path.Combine(pathAndroid + "/hello.JPG"); //whatever your file name

最终,我可以使用 Imread :

Mat img = CvInvoke.Imread(pathfile);
CvInvoke.CvtColor(img, img, ColorConversion.Rgb2Gray);
CvInvoke.Imwrite(pathAndroid + "/result.JPG", img); //save the image

推荐阅读