我正在尝试在xamarin中开发带有Tesseract API的ocr APP(Visual Studio 2019)。我不愿采取这些步骤http://thatcsharpguy.com/posts/tesseract-ocr-in-xamarin-en/。我做了他提到的所有事情。另外,我还在AndriodManifest.xml文件中创建了提供程序路径,但仍然出现文件uri暴露异常。请帮助我,我是xamarin的新手。
AndriodManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.textminer1">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<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" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:label="Textminer1.Android">
<provider android:name="android.support.v4.content.FileProvider"
android:authorities="com.companyname.textminer1.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>
file_paths.xml
<?xml version="1.0" encoding="utf-8" ?>
<paths xmlns:android=" http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.companyname.textminer1/files" />
</paths>
MainActivity.cs
var container = TinyIoCContainer.Current;
container.Register<IDevice>(AndroidDevice.CurrentDevice);
container.Register<ITesseractApi>((cont, parameters) =>
{
return new TesseractApi(ApplicationContext, AssetsDeployment.OncePerInitialization);
});
Resolver.SetResolver(new TinyResolver(container));
LoadApplication(new App());
OCR.xaml.cs
private async void GetPhotoButton_OnClicked(object sender, EventArgs e)
{
if (!_tesseract.Initialized)
await _tesseract.Init("eng");
var photo = await TakePic();
if (photo != null)
{
var imageBytes = new byte[photo.Source.Length];
photo.Source.Position = 0;
photo.Source.Read(imageBytes, 0, (int)photo.Source.Length);
photo.Source.Position = 0;
image.Source = ImageSource.FromStream(() => photo.Source);
var tessResult = await _tesseract.SetImage(imageBytes);
if (tessResult)
{
TextLabel.Text = _tesseract.Text;
}
}
}
private async Task<MediaFile> TakePic()
{
var mediaStorageOptions = new CameraMediaStorageOptions
{
DefaultCamera = CameraDevice.Rear,
SaveMediaOnCapture = true,
Directory = "Picture",
Name = string.Format("test.jpg", DateTime.Now.ToString("yyMMddhhmmss")),
PercentQuality = 85,
MaxPixelDimension = 400
};
var mediaFile = await _device.MediaPicker.TakePhotoAsync(mediaStorageOptions);
return mediaFile;
}
Android.OS.FileUriExposedException:'file:///storage/emulated/0/Android/data/com.companyname.textminer1/files/Pictures/Picture/test.jpg_1.jpg通过ClipData.Item在应用程序之外公开。 getUri()'