public ScanResult Execute(object markerAsynchrone) { _log.Info("======================================= SCAN COMMAND ========================================"); _log.Info(string.Format("Start execute with scan params: " + "source={0}, sourceFeed={1}, dpi={2}, colorMode={3}, compressionFormat={4}, format={5}, " + "isPackage={6}, saveAs={7}, CurrentSourceIndex={8}, Index={9}", _command.Source, _command.DocumentHandlingCap, _command.DPI, _command.ColorMode, _command.CompressionFormat.ImgFormat, _command.Format.Name, _command.IsPackage, _command.SaveAs, _scannerManager.CurrentSourceIndex, _scannerManager.CurrentSource.Index )); ScanResult scanResult; try { var scannedImages = new List <Image>(); lock (markerAsynchrone) { if (_scannerManager.CurrentSourceIndex != _command.Source) { new AsyncWorker <int>().RunWorkAsync(_command.Source, _scannerManager.ChangeSource, WaitTimeForChangeSource); if (_scannerManager.CurrentSourceIndex != _command.Source) { return(new SingleScanResult("Не удается изменить источник данных")); } } var settingAcquire = new SettingsAcquire { Format = _command.Format, Resolution = _command.DPI, PixelType = _command.ColorMode, ScanSource = _command.DocumentHandlingCap }; //создаем отдельный поток и передаем в него метод сканирования(callback) и параметры для него var images = new AsyncWorker <SettingsAcquire, List <Image> >().RunWorkAsync(settingAcquire, _scannerManager.CurrentSource.Scan, WaitTimaeForScan); if (images != null) { foreach (var image in images) { var clonedImage = (Image)image.Clone(); image.Dispose(); ((Bitmap)clonedImage).SetResolution(_command.DPI, _command.DPI); scannedImages.Add(clonedImage); } } } if (scannedImages.Count == 0) { return(new SingleScanResult( "Сканирование завершилось неудачей! Попробуйте переподключить сканер либо повторить сканирование с помощью другого устройства.")); } if (scannedImages.Count == 1) { var image = scannedImages[0]; var downloadFile = SaveImage(image); var singleScanResult = new SingleScanResult(); singleScanResult.FillContent(downloadFile); scanResult = singleScanResult; image.Dispose(); } else { var downloadFiles = new List <DownloadFile>(); int counter; try { counter = int.Parse(_command.FileCounter); } catch (Exception) { counter = 1; } foreach (var scannedImage in scannedImages) { var downloadFile = SaveImage(scannedImage, counter++); downloadFiles.Add(downloadFile); scannedImage.Dispose(); } var multipleScanResult = new MultipleScanResult(); multipleScanResult.FillContent(downloadFiles); scanResult = multipleScanResult; } } catch (TwainException ex) { return(new SingleScanResult(ex.Message)); } _log.Info("Scan command executed"); return(scanResult); }
public ActionResult ConvertToBase64File(DownloadFileParam fileParam) { foreach (var fileCheck in fileParam.ListFiles) { if (!GlobalDictionaries.Scans.Exists(x => x == fileCheck.TempFile)) { throw new Exception("Не найден файл для скачивания!"); } } var tempDir = Path.GetTempPath(); byte[] response; var fileId = fileParam.ListFiles[0].TempFile; var fileName = fileParam.ListFiles[0].FileName; string mimeType = null; if (fileParam.SaveAs == (int)GlobalDictionaries.SaveAsValues.Pdf) { fileId = MakePdf(fileParam.ListFiles); mimeType = _mimeTypes["pdf"]; fileName = Path.ChangeExtension(fileName, ".pdf"); } else { var extension = Path.GetExtension(fileName); if (!string.IsNullOrEmpty(extension)) { mimeType = _mimeTypes[extension.Substring(1).ToLower()]; } } var file = Path.Combine(tempDir, fileId); string base64str = ""; if (File.Exists(file)) { response = File.ReadAllBytes(file); //массив байт конвертируем в строку base64str = Convert.ToBase64String(response); File.Delete(file); GlobalDictionaries.Scans.Remove(fileId); foreach (var img in fileParam.ListFiles) { if (File.Exists(tempDir + img.TempFile)) { File.Delete(tempDir + img.TempFile); GlobalDictionaries.Scans.Remove(img.TempFile); } } } else { response = Encoding.UTF8.GetBytes("Not found"); } var singleScanResult = new SingleScanResult(); singleScanResult.FillContent(new DownloadFile(fileName, fileId), base64str); //отправляем не файл а base64 return (new ActionResult { Content = singleScanResult.Content, ContentType = "text/json" }); }