Jun*_*iuz 0 c# async-await uwp
我有一个UWP应用程序,用于捕获和处理来自摄像头的图像.该项目利用Microsoft认知服务人脸识别API,我现在正在探索应用程序的现有功能.我的目标是当相机识别人物的图像时(通过脸部识别API服务),我想显示该人的相关图像.
这样,图像被捕获并存储在我的机器的本地目录中.我想检索图像文件并在识别出人物后在屏幕上呈现它.
下面的代码显示了asyncTask方法ProcessCameraCapture
private async Task ProcessCameraCapture(ImageAnalyzer e)
{
if (e == null)
{
this.UpdateUIForNoFacesDetected();
this.isProcessingPhoto = false;
return;
}
DateTime start = DateTime.Now;
await e.DetectFacesAsync();
if (e.DetectedFaces.Any())
{
string names;
await e.IdentifyFacesAsync();
this.greetingTextBlock.Text = this.GetGreettingFromFaces(e, out names);
if (e.IdentifiedPersons.Any())
{
this.greetingTextBlock.Foreground = new SolidColorBrush(Windows.UI.Colors.GreenYellow);
this.greetingSymbol.Foreground = new SolidColorBrush(Windows.UI.Colors.GreenYellow);
this.greetingSymbol.Symbol = Symbol.Comment;
GetSavedFilePhoto(names);
}
else
{
this.greetingTextBlock.Foreground = new SolidColorBrush(Windows.UI.Colors.Yellow);
this.greetingSymbol.Foreground = new SolidColorBrush(Windows.UI.Colors.Yellow);
this.greetingSymbol.Symbol = Symbol.View;
}
}
else
{
this.UpdateUIForNoFacesDetected();
}
TimeSpan latency = DateTime.Now - start;
this.faceLantencyDebugText.Text = string.Format("Face API latency: {0}ms", (int)latency.TotalMilliseconds);
this.isProcessingPhoto = false;
}
Run Code Online (Sandbox Code Playgroud)
在GetSavedFilePhoto,识别出一个人后,我传递了字符串名称参数.
下面的代码GetSavedFilePhoto方法
private void GetSavedFilePhoto(string personName)
{
if (string.IsNullOrWhiteSpace(personName)) return;
var directoryPath = @"D:\PersonImages";
var directories = Directory.GetDirectories(directoryPath);
var filePaths = Directory.GetFiles(directoryPath, "*.jpg", SearchOption.AllDirectories);
}
Run Code Online (Sandbox Code Playgroud)
但是,在GetSavedFilePhoto方法中,变量directories在使用directoryPath字符串变量时返回一个空的数组字符串.目录"D:\ PersonImages"是我的机器中的有效和现有文件夹,它包含内部带有图像的子文件夹.我也尝试Directory.GetFiles检索jpg图像,但仍然返回一个空字符串.
我认为它应该工作,因为我已经Directory多次使用类但不在asyncTask方法中.async使用I/O操作时是否使用导致文件未返回?
抱歉这个愚蠢的问题,但我真的不明白.
任何帮助是极大的赞赏.
使用Directory.GetFiles或Directory.GetDirectories方法可以通过以下代码获取Application的本地文件夹中的文件夹/文件.但它无法打开D:\.
var directories = Directory.GetDirectories(ApplicationData.Current.LocalFolder.Path);
Run Code Online (Sandbox Code Playgroud)
在UWP应用程序中,您只能访问默认的两个位置(本地文件夹和安装文件夹),其他需要功能设置或文件打开选择器.详细信息请参考文件访问权限.
如果您需要访问所有文件D:\,用户必须使用手动选择D:\驱动器FolderPicker,然后您有权访问此驱动器中的文件.
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.ComputerFolder;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
}
else
{
//do some stuff
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
475 次 |
| 最近记录: |