private async Task DoSaveImage(ScannedImage.Snapshot snapshot, string path, ImageFormat format) { PathHelper.EnsureParentDirExists(path); if (Equals(format, ImageFormat.Tiff)) { await tiffHelper.SaveMultipage(new List <ScannedImage.Snapshot> { snapshot }, path, imageSettingsContainer.ImageSettings.TiffCompression, (i, j) => { }, CancellationToken.None); } else if (Equals(format, ImageFormat.Jpeg)) { var quality = Math.Max(Math.Min(imageSettingsContainer.ImageSettings.JpegQuality, 100), 0); var encoder = ImageCodecInfo.GetImageEncoders().First(x => x.FormatID == ImageFormat.Jpeg.Guid); var encoderParams = new EncoderParameters(1); encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, quality); using (Bitmap bitmap = await scannedImageRenderer.Render(snapshot)) { bitmap.Save(path, encoder, encoderParams); } } else { using (Bitmap bitmap = await scannedImageRenderer.Render(snapshot)) { bitmap.Save(path, format); } } }
public OcrRequestParams(ScannedImage.Snapshot snapshot, IOcrEngine ocrEngine, OcrParams ocrParams) { ScannedImage = snapshot.Source; TransformState = snapshot.TransformList.Count == 0 ? -1 : snapshot.TransformState; Engine = ocrEngine; OcrParams = ocrParams; }
public async Task <OcrResult> QueueForeground(IOcrEngine ocrEngine, ScannedImage.Snapshot snapshot, string tempImageFilePath, OcrParams ocrParams, CancellationToken cancelToken) { OcrRequest req; lock (this) { ocrEngine = ocrEngine ?? ocrManager.ActiveEngine ?? throw new ArgumentException("No OCR engine available"); ocrParams = ocrParams ?? ocrManager.DefaultParams; var reqParams = new OcrRequestParams(snapshot, ocrEngine, ocrParams); req = requestCache.GetOrSet(reqParams, () => new OcrRequest(reqParams)); // Fast path for cached results if (req.Result != null) { SafeDelete(tempImageFilePath); return(req.Result); } // Manage ownership of the provided temp file if (req.TempImageFilePath == null) { req.TempImageFilePath = tempImageFilePath; } else { SafeDelete(tempImageFilePath); } // Increment the reference count req.ForegroundCount += 1; queueWaitHandle.Release(); } // If no worker threads are running, start them EnsureWorkerThreads(); // Wait for completion or cancellation await Task.Factory.StartNew(() => { try { WaitHandle.WaitAny(new[] { req.WaitHandle, cancelToken.WaitHandle }); } catch (Exception e) { Log.ErrorException("Error in OcrRequestQueue.QueueForeground response task", e); } }, TaskCreationOptions.LongRunning); lock (this) { // Decrement the reference count req.ForegroundCount -= 1; // If all requestors have cancelled and there's no result to cache, delete the request DestroyRequest(req); } // If no requests are pending, stop the worker threads EnsureWorkerThreads(); // May return null if cancelled return(req.Result); }
public byte[] RenderThumbnail(ScannedImage.Snapshot snapshot, int size) { var stream = new MemoryStream(); using (var bitmap = Task.Factory.StartNew(() => thumbnailRenderer.RenderThumbnail(snapshot, size)).Unwrap().Result) { bitmap.Save(stream, ImageFormat.Png); } return(stream.ToArray()); }
public bool HasCachedResult(IOcrEngine ocrEngine, ScannedImage.Snapshot snapshot, OcrParams ocrParams) { ocrEngine = ocrEngine ?? ocrManager.ActiveEngine ?? throw new ArgumentException("No OCR engine available"); ocrParams = ocrParams ?? ocrManager.DefaultParams; var reqParams = new OcrRequestParams(snapshot, ocrEngine, ocrParams); lock (this) { return(requestCache.ContainsKey(reqParams) && requestCache[reqParams].Result != null); } }
public void QueueBackground(ScannedImage.Snapshot snapshot, string tempImageFilePath, OcrParams ocrParams) { OcrRequest req; CancellationTokenSource cts = new CancellationTokenSource(); lock (this) { var ocrEngine = ocrManager.ActiveEngine; if (ocrEngine == null) { return; } ocrParams = ocrParams ?? ocrManager.DefaultParams; var reqParams = new OcrRequestParams(snapshot, ocrEngine, ocrParams); req = requestCache.GetOrSet(reqParams, () => new OcrRequest(reqParams)); // Fast path for cached results if (req.Result != null) { return; } // Manage ownership of the provided temp file if (req.TempImageFilePath == null) { req.TempImageFilePath = tempImageFilePath; } else { SafeDelete(tempImageFilePath); } // Increment the reference count req.BackgroundCount += 1; snapshot.Source.ThumbnailInvalidated += (sender, args) => cts.Cancel(); snapshot.Source.FullyDisposed += (sender, args) => cts.Cancel(); queueWaitHandle.Release(); } // If no worker threads are running, start them EnsureWorkerThreads(); var op = StartingOne(); Task.Factory.StartNew(() => { try { WaitHandle.WaitAny(new[] { req.WaitHandle, cts.Token.WaitHandle, op.CancelToken.WaitHandle }); lock (this) { // Decrement the reference count req.BackgroundCount -= 1; // If all requestors have cancelled and there's no result to cache, delete the request DestroyRequest(req); } FinishedOne(); // If no requests are pending, stop the worker threads EnsureWorkerThreads(); } catch (Exception e) { Log.ErrorException("Error in OcrRequestQueue.QueueBackground response task", e); } }, TaskCreationOptions.LongRunning); }