Skip to content
This repository has been archived by the owner on May 9, 2019. It is now read-only.

Commit

Permalink
No longer uses temporary files (In memory streams instead)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeio committed Jul 31, 2016
1 parent d3af4da commit 56257cc
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions VoidRewardParser/Logic/ScreenCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
using Windows.Globalization;
using Windows.Media.Ocr;
using Windows.Storage;
using Windows.Storage.Streams;
using System.Runtime;
using System.Runtime.InteropServices.WindowsRuntime;

namespace VoidRewardParser.Logic
{
Expand All @@ -15,25 +18,23 @@ public class ScreenCapture

public static async Task<string> ParseTextAsync()
{

string fileName = string.Empty;
try
{
fileName = await Task.Run(() => Path.GetTempFileName());
await Task.Run(() => SaveScreenshot(fileName));
return await RunOcr(fileName);
using (var memoryStream = new MemoryStream())
using (var memoryRandomAccessStream = new InMemoryRandomAccessStream())
{
await Task.Run(() => SaveScreenshot(memoryStream));
await memoryRandomAccessStream.WriteAsync(memoryStream.ToArray().AsBuffer());
return await RunOcr(memoryRandomAccessStream);
}
}
finally
{
if(!string.IsNullOrWhiteSpace(fileName) && File.Exists(fileName))
{
await Task.Run(() => File.Delete(fileName));
}
GC.Collect(0);
}
}

public static void SaveScreenshot(string stream)
public static void SaveScreenshot(Stream stream)
{
int width = (int)SystemParameters.FullPrimaryScreenWidth;
int height = (int)SystemParameters.FullPrimaryScreenHeight;
Expand All @@ -46,12 +47,10 @@ public static void SaveScreenshot(string stream)
}
}

private static async Task<string> RunOcr(string file)
private static async Task<string> RunOcr(IRandomAccessStream stream)
{
OcrEngine engine = OcrEngine.TryCreateFromUserProfileLanguages();
Uri uri = new Uri(file);
var storageFile = await StorageFile.GetFileFromPathAsync(file);
var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(await storageFile.OpenAsync(FileAccessMode.Read));
var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
var result = await engine.RecognizeAsync(await decoder.GetSoftwareBitmapAsync());
return result.Text;
}
Expand Down

0 comments on commit 56257cc

Please sign in to comment.