Azure 认知服务 Xamarin.Forms 访问问题

Azure Cognitive Services Xamarin.Forms access problem

我对 Microsoft Azure 认知服务有疑问。

当我使用相机在 phone 上拍照时,默认情况下它保存在路径中:

DCIM/Camera

,但是当我使用我的应用程序拍照时,它保存在

Integral storage/Pictures/temp

情况简述:

1) 我正在使用默认相机拍照,我可以使用 Plugin.Media 在我的应用程序中打开它并且 MCS 完美运行,代码:

private async void btnPick_Clicked(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();

            var file = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions());
            Image = ImageSource.FromStream(() => file.GetStream());

            var result = client.RecognizeTextAsync(file.GetStream()).Result;
            var words = from r in result.Regions
                        from l in r.Lines
                        from w in l.Words
                        select w.Text;

            OutputText = string.Join(" ", words.ToArray());

            await Navigation.PushAsync(new TextFromPhoto(OutputText, Image));
        }

2) 当我使用此代码使用我的应用程序拍照时:

private async void btnTake_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 = true
            });

            Image = ImageSource.FromStream(() => file.GetStream());


            var myStream = file.GetStream();

            var result = client.RecognizeTextAsync(myStream).Result;
            var words = from r in result.Regions
                        from l in r.Lines
                        from w in l.Words
                        select w.Text;

            OutputText = string.Join(" ", words.ToArray());

            await Navigation.PushAsync(new TextFromPhoto(OutputText, Image));
        }

应用程序进入中断模式并且中断从这一行开始:

                var result = client.RecognizeTextAsync(myStream).Result;

这条线在前面的方法中有效

这里是androidmanifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.OCRScannerForms.Android" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />

  <uses-permission android:name="android.permission.CAMERA"/>
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

  <application android:label="OCRScannerForms.Android">
        <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
        </provider>
    </application>
</manifest>

这里是 Resources/xml/file_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <external-files-path name="my_images" path="Pictures" />
  <external-files-path name="my_movies" path="Movies" />
</paths>

有趣的是我无法在我的应用程序中打开,照片在我的应用程序中较早时拍摄。

我怀疑照片路径有问题,但我无法修复

您应该使用新的 Computer Vision API 而不是 Microsoft.ProjectOxford.Vision

例如:

    private async void btnTake_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 = true,
            PhotoSize = PhotoSize.Small
        });

        var Image = ImageSource.FromStream(() => file.GetStream());


        var client = new HttpClient();
        var queryString = HttpUtility.ParseQueryString(string.Empty);

        // Request headers
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

        // Request parameters
        queryString["mode"] = "Printed";

        var uri = "https://eastus.api.cognitive.microsoft.com/vision/v2.0/recognizeText?" + queryString;

        HttpResponseMessage response;

        var myStream = file.GetStream();

        BinaryReader binaryReader = new BinaryReader(myStream);
        var byteData = binaryReader.ReadBytes((int)myStream.Length);

        using (var content = new ByteArrayContent(byteData))
        {
            content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            response = await client.PostAsync(uri, content);
        }

        string operationLocation = "";
        operationLocation = response.Headers.GetValues("Operation-Location").FirstOrDefault();
        string contentString;
        int i = 0;
        do
        {
            System.Threading.Thread.Sleep(1000);
            response = await client.GetAsync(operationLocation);
            contentString = await response.Content.ReadAsStringAsync();
            ++i;
        }
        while (i < 10 && contentString.IndexOf("\"status\":\"Succeeded\"") == -1);

        Label1.Text = JToken.Parse(contentString).ToString();
    }

结果是:

请查看以下内容link了解更多信息: https://westus.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/587f2c6a154055056008f200