有没有办法在Xamarin.Forms或Xamarin.Android中对图像进行阈值处理?
我想在Android中使用相机拍摄每张照片(一次一张),目前我正在使用xlabs进行相机拍摄。
我搜索过,但我没有找到任何帮助。也许你们可以帮助我。 :)
答案 0 :(得分:2)
为此,您可以使用OpenCV Library for Android。
在您的启动活动(或MainActivity)中添加以下内容:
//beware,that we need to INIT Only once,so better to put in SplashActivity
public MainActivity()
{
if (!OpenCVLoader.InitDebug())
{
System.Console.WriteLine("Failed to INIT \n OpenCV Failure");
}
else
{
System.Console.WriteLine("OpenCV INIT Succes");
}
}
现在我们可以将图像转换为黑白:
void ConvertImageToBW()
{
Mat MatToBW = new Mat();
using (Bitmap SourceImg = Bitmap.CreateBitmap(BitmapFactory.DecodeFile(App._file.Path))) //App._file.Path - path of image that we have made by camera
{
if (SourceImg != null)
{
Utils.BitmapToMat(SourceImg,MatToBW);
//first we convert to grayscale
Imgproc.CvtColor(MatToBW, MatToBW, Imgproc.ColorRgb2gray);
//now converting to b/w
Imgproc.Threshold(MatToBW, MatToBW, 0.5 * 255, 255, Imgproc.ThreshBinary);
using(Bitmap newBitmap = Bitmap.CreateBitmap(MatToBW.Cols(),MatToBW.Rows(),Bitmap.Config.Argb8888))
{
Utils.MatToBitmap(MatToBW, newBitmap);
//put result in your imageView, B/W Image
imageView.SetImageBitmap(newBitmap);
}
}
}
}
现在只需将此方法用于OnActivityResult即可 享受!