我在Xamarin wpf上拍摄Iphone上的OCR视觉API时遇到了问题,而在Android上它完美无缺。 在Android和IOS上从图库中挑选图片也可以正常工作。
Thread started: #11 Resolved pending breakpoint at 'OcrRecognitionPage.xaml.cs:60,1' to void ComputerVisionSample.OcrRecognitionPage.d__5.MoveNext () [0x00162]. Error while resolving expression: One or more errors occurred.
线程完成:#9 线程'未知' (0x9)已退出代码0(0x0)。 线程完成:#10 线程'未知' (0xa)已退出代码0(0x0)。 线程开始:#12 线程开始:#13 线程开始:#14 线程完成:#8 线程'未知' (0x8)已退出代码0(0x0)。Unhandled Exception: Microsoft.ProjectOxford.Vision.ClientException: Exception of type 'Microsoft.ProjectOxford.Vision.ClientException' was thrown. Thread finished: #11
源代码:
public partial class OcrRecognitionPage : ContentPage
{
private readonly VisionServiceClient visionClient;
string sourceLanguage = "en";
string sourceText = "";
public OcrRecognitionPage()
{
InitializeComponent();
this.visionClient = new VisionServiceClient("Your code here");
}
private async Task AnalyzePictureAsync(Stream inputFile)
{
if (!CrossConnectivity.Current.IsConnected)
{
await DisplayAlert("Network error", "Please check your network connection and retry.", "OK");
return null;
}
OcrResults ocrResult = await visionClient.RecognizeTextAsync(inputFile);
return ocrResult;
}
private async void TakePictureButton_Clicked(object sender, EventArgs e)
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("No Camera", "No camera available.", "OK");
return;
}
var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
SaveToAlbum = false,
Name = "test.jpg"
});
if (file == null)
return;
Image1.Source = ImageSource.FromStream(() => file.GetStream());
var ocrResult = await AnalyzePictureAsync(file.GetStream());
this.BindingContext = null;
this.BindingContext = ocrResult;
sourceLanguage = ocrResult.Language;
PopulateUIWithRegions(ocrResult);
}
private void PopulateUIWithRegions(OcrResults ocrResult)
{
// Iterate the regions
foreach (var region in ocrResult.Regions)
{
// Iterate lines per region
foreach (var line in region.Lines)
{
// For each line, add a panel
// to present words horizontally
var lineStack = new StackLayout
{ Orientation = StackOrientation.Horizontal };
// Iterate words per line and add the word
// to the StackLayout
foreach (var word in line.Words)
{
Text = word.Text };
sourceText += textLabel.Text + " ";
lineStack.Children.Add(textLabel);
}
// Add the StackLayout to the UI
this.DetectedText.Children.Add(lineStack);
}
}
}
private async void UploadPictureButton_Clicked(object sender, EventArgs e)
{
if (!CrossMedia.Current.IsPickPhotoSupported)
{
await DisplayAlert("No upload", "Picking a photo is not supported.", "OK");
return;
}
var file = await CrossMedia.Current.PickPhotoAsync();
if (file == null)
return;
Image1.Source = ImageSource.FromStream(() => file.GetStream());
var ocrResult = await AnalyzePictureAsync(file.GetStream());
this.BindingContext = ocrResult;
sourceLanguage = ocrResult.Language;
PopulateUIWithRegions(ocrResult);
}
有谁知道如何解决它?