I am implementing OCR in xamarin forms. I am using TesseractApi following Tesseract OCR for Xamarin (part 1), xocr and MediaPlugin. The implementation is straightforward from saved/captured image, but I am looking is to perform OCR from the frames not from the saved pic.
private async void Button_Clicked(object sender, EventArgs e)
{
if (
!CrossMedia.Current.IsPickPhotoSupported)
{
await DisplayAlert("No Upload", "Picking photo is not supported!", "OK");
return;
}
if (!_tesseractApi.Initialized)
await _tesseractApi.Init("eng");
var photo = await TakePic(); //capture picture
if (photo != null)
{
var tessResult = await _tesseractApi.SetImage(photo);
if (tessResult)
{
recognizedTextLabel.Text = _tesseractApi.Text;
}
}
}
private async Task<Stream> TakePic()
{
var file = await CrossMedia.Current.PickPhotoAsync();
if (file != null)
{
//show stream in image - this is for test only
img.Source = ImageSource.FromStream(() =>
{
return file.GetStream();
});
//return stream for ocr reading
return file.GetStream();
}
return null;
}
Related
I started a windows form application which imports images from files and store them and specify a radio button to each one.
it have a button with the name 'Lock'
so if user select one radio button and then press the button the app should change the lock screen image and then Lock the screen.
But I don't know how to set the image and lock the screen.
I googled later and the answer about the LockScreen.blabla didn't work for me because I can't do using windows.system.userprofile;
If some one get me the assembly i will do the thing.
there is the events:
private void rB_CheckedChanged(object sender, EventArgs e)
{
MyRadioButton radioButton = sender as MyRadioButton;
pictureBox1.Image = radioButton.Image;
}
private void btnBrowse_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
foreach (Control control in FindForm().Controls)
{
if (control.GetType() == typeof(MyRadioButton))
{
MyRadioButton mrb = control as MyRadioButton;
if (mrb.Checked == true)
{
mrb.Image = pictureBox1.Image;
}
}
}
}
}
private void btnLock_Click(object sender, EventArgs e)
{
//should set the lock screen background
LockScreen.LockScreen.SetImageFileAsync(imagefile);//shows error 'the name lock screen does not exist
}
I have made a UWP application to use the LockScreen class provided by windows.userProfile.dll And the event's changed to:
private Dictionary<string, string> Images = new Dictionary<string, string>();
private async void btnLock_Click(object sender, RoutedEventArgs e)
{
string path;
if (Images.TryGetValue(selectedRadioButton.Name, out path))
{
StorageFile file = await StorageFile.GetFileFromPathAsync(path);
await LockScreen.SetImageFileAsync(file);
try
{
HttpClient httpClient = new HttpClient();
Uri uri = new Uri("http://localhost:8080/lock/");
HttpStringContent content = new HttpStringContent(
"{ \"pass\": \"theMorteza#1378App\" }",
UnicodeEncoding.Utf8,
"application/json");
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(
uri,
content);
httpResponseMessage.EnsureSuccessStatusCode();
var httpResponseBody = await httpResponseMessage.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
throw ex;
}
}
}
private async void btnBrowse_Click(object sender, RoutedEventArgs e)
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
using (var imageStream = await file.OpenReadAsync())
{
var bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(imageStream);
image.Source = bitmapImage;
}
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
await file.CopyAsync(storageFolder, selectedRadioButton.Name+file.FileType,NameCollisionOption.ReplaceExisting);
addOrUpdate(selectedRadioButton.Name, Path.Combine(storageFolder.Path, selectedRadioButton.Name + file.FileType));
save();
}
}
private async void rb_Checked(object sender, RoutedEventArgs e)
{
RadioButton rb = sender as RadioButton;
selectedRadioButton = rb;
btnBrowse.IsEnabled = true;
string path;
if (Images.TryGetValue(rb.Name, out path))
{
StorageFile file = await StorageFile.GetFileFromPathAsync(path);
using (var imageStream = await file.OpenReadAsync())
{
var bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(imageStream);
image.Source = bitmapImage;
}
}
}
private void addOrUpdate(string key, string image)
{
if (Images.Keys.Contains(key))
{
Images.Remove(key);
Images.Add(key, image);
}
else
{
Images.Add(key, image);
}
}
private async void save()
{
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile file = await storageFolder.CreateFileAsync("sample.txt", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, JsonConvert.SerializeObject(Images));
}
private async void load()
{
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.GetFileAsync("sample.txt");
string fileText = await FileIO.ReadTextAsync(sampleFile);
try
{
Images = JsonConvert.DeserializeObject<Dictionary<string, string>>(fileText);
}
catch (Exception)
{
}
}
and you can see in the lock button click event there is a request to a web server cause you cannot directly lock the screen in a UWP app and you can follow the rest of your question in my next answer to the question "why I can't directly lock screen in UWP app?and how to do so?".
I tried picking a picture from my gallery from an app I developed using Xamarin-forms, visual studio 2017. After selecting a picture, i keep getting that error. same as when using camera to take the picture.
I tried debugging it. But i was not able to get anything concrete from it.
It crashed when it got to var mediaFile
using Plugin.Media;
using Plugin.Media.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace App11.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Levels : ContentPage
{
public Levels ()
{
InitializeComponent ();
}
private async void TakePhoto_Clicked(object sender, EventArgs e)
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("camera", "not supported", "OK");
return;
}
var mediaFile= await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg"
});
if (file == null)
return;
await DisplayAlert("File Location", file.Path, "OK");
image.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
return stream;
});
}
private async void FromPhone_Clicked(object sender, EventArgs e)
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsPickPhotoSupported)
{
await DisplayAlert("No pickphoto", "Not available", "OK");
return;
}
var mediaFile = await CrossMedia.Current.PickPhotoAsync();
if (mediaFile == null)
return;
image.Source = ImageSource.FromStream(() =>
{
return mediaFile.GetStream();
});
}
}
}
Please take the following code about taking photos:
private async void Takephoto_Clicked(object sender, EventArgs e)
{
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
DisplayAlert("No Camera", ":( No camera available.", "OK");
return;
}
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Test",
SaveToAlbum = true,
CompressionQuality = 75,
CustomPhotoSize = 50,
PhotoSize = PhotoSize.MaxWidthHeight,
MaxWidthHeight = 2000,
DefaultCamera = CameraDevice.Front
});
if (file == null)
return;
DisplayAlert("File Location", file.Path, "OK");
image.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
file.Dispose();
return stream;
});
}
Here is the sample that you can take a look:
https://github.com/jamesmontemagno/MediaPlugin
I have a PCL type app and I'm using the Xam.Plugin.Media plugin. I need it to ensure a user submits a photo from the camera before they can continue.
To do this I show the camera page from a button click event and I want to ensure that in case the user cancels out of this that the app launches the camera again, this would repeat until a photograph is stored.
Currenty my app falls in the onActivityResumed method of the MainApplication file when the user cancels out of the camera
Attached photo of my code, My code.
private async void TakePicture()
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await App.Current.MainPage.DisplayAlert("No Camera", ":( No camera available.", "Aceptar");
}
file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg",
PhotoSize = PhotoSize.Small,
});
//IsRunning = true;
if (file != null)
{
ImageSource = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
return stream;
});
}
IsRunning = false;
}
Aside from the fact that it is usually a bit of a UX issue to force a user into anything nowadays, the question still has some merits.
this is the approach that I would consider, it involves recursion.
private async void TakePicture()
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await App.Current.MainPage.DisplayAlert("No Camera", ":( No camera available.", "Aceptar");
}
file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg",
PhotoSize = PhotoSize.Small,
});
//IsRunning = true;
if (file != null)
{
ImageSource = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
return stream;
});
}
else
{
// Recursion - I believe that this would continue until the file is not null, then it would carry on.
TakePicture();
}
IsRunning = false;
}
I can't say I use recursion that often, but I think it might do the trick here.
I'm trying to implement some logical to share the image of my webView and some extras informations. If I do that without capture screen, it works perfectly:
private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
if (await GetShareContent(e.Request))
{
if (String.IsNullOrEmpty(e.Request.Data.Properties.Title))
{
e.Request.FailWithDisplayText("Nenhum título adicionado");
}
}
}
private async Task<bool> GetShareContent(DataRequest request, StorageFile file)
{
bool succeeded = false;
string text = "Dados do Arquivo:" + Environment.NewLine + webViewModel.Name;
string dataPackageText = text;
if (!String.IsNullOrEmpty(dataPackageText))
{
DataPackage requestData = request.Data;
requestData.Properties.Title = "Target";
requestData.Properties.Description = webViewModel.Name;
requestData.SetText(dataPackageText);
succeeded = true;
}
else
{
request.FailWithDisplayText("Não há nada para compartilhar");
}
return succeeded;
}
But, if I try the same thing justing adding the captured image, it doesn't work, doesn't show any Excepetion, just the message: "Não há nada para compartilhar agora" (There's nothing to share right now)
I don't know what is going on. The code that doesn't work:
private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
StorageFile file = await captureScreen();
if (await GetShareContent(e.Request, file))
{
if (String.IsNullOrEmpty(e.Request.Data.Properties.Title))
{
e.Request.FailWithDisplayText("Nenhum título adicionado");
}
}
}
private async Task<StorageFile> captureScreen()
{
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(webView, (int)webView.Width, (int)webView.Height);
Image myImage = new Image();
myImage.Source = renderTargetBitmap;
var file = await App.rootDir.CreateFileAsync("screenCapture.jpg", CreationCollisionOption.ReplaceExisting);
var pixels = await renderTargetBitmap.GetPixelsAsync();
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
byte[] bytes = pixels.ToArray();
encoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)webView.Width, (uint)webView.Height,
96, 96, bytes);
await encoder.FlushAsync();
}
return file;
}
I though it could be happening because the image was not read when the share is called, but I'm using await as it should be. And my jpeg is created perfectly.
The OnDataRequested callback needs to take a deferral, using DataRequest.GetDeferral, when calling asynchronous APIs.
private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
DataRequestDeferral deferral = e.Request.GetDeferral();
// Code to do screen capture...
deferral.Complete();
}
But, per MSDN, "[the share operation] function must return a DataPackage object within 200ms to prevent the operation from timing out". It is definitely possible for the screen capture to take longer than 200 ms. Use the DataPackage.SetDataProvider for operations that may take longer such as screen capture.
private void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
DataPackage requestData = e.request.Data;
requestData.Properties.Title = "Target";
requestData.Properties.Description = webViewModel.Name;
// Set up the data provider for a long running share operation...
requestData.SetDataProvider(StandardDataFormats.Bitmap, OnDeferredRequestedHandler);
}
private async void OnDeferredRequestedHandler(DataProviderRequest providerRequest)
{
// Again, get a deferral as an asynchronous method is called
DataProviderDeferral deferral = providerRequest.GetDeferral();
// Code to do screen capture...
deferral.Complete();
}
The Share content source sample on MSDN shows how this can be performed in full detail.
well i try a lot of things but i don't really understand everything of save in localstorage. I know how its work since the camera, but i don't know how to make it with inkManager. If you're any ideas ?
This is my code to save where the user wants, but i would like to "auto-save" in localstorage :
private async void save_Button(object sender, RoutedEventArgs e)
{
if (_inkManager.GetStrokes().Count > 0)
{
try
{
Windows.Storage.Pickers.FileSavePicker save = new Windows.Storage.Pickers.FileSavePicker();
save.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
save.DefaultFileExtension = ".jpg";
save.FileTypeChoices.Add("JPG", new string[] { ".jpg" });
StorageFile filesave = await save.PickSaveFileAsync();
IOutputStream ab = await filesave.OpenAsync(FileAccessMode.ReadWrite);
if (ab != null)
await _inkManager.SaveAsync(ab);
// await save.CopyAsync(ApplicationData.Current.LocalFolder, "merge1.jpg");
if (save != null)
{
Clipboard.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appdata:///local/merge1.jpg"));
}
}
catch (Exception)
{
}
}
}
The solution is pretty easy finally :
StorageFile myMerge = await ApplicationData.Current.LocalFolder.CreateFileAsync("myimg.png");
IOutputStream ac = await myMerge.OpenAsync(FileAccessMode.ReadWrite);
if (ac != null)
await _inkManager.SaveAsync(ac);