/// <summary> /// Use scanner provided scanner id to scan an image. /// </summary> /// <returns>Scanned images.</returns> public static List<Image> Scan(String scannerId, ScanSettings settings) { // select the correct scanner using the provided scannerId parameter DeviceManager manager = new DeviceManager(); Device device = null; foreach (DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == scannerId) { try { //Check connection to scanner device = info.Connect(); } catch (Exception) { throw new Exception("Cannot connect to scanner, please check your device and try again."); } break; } } // device was not found if (device == null) { throw new Exception("The provided scanner device could not be found."); } return Acquire(device, settings); }
/// <summary> /// Use scanner to scan an image (scanner is selected by its unique id). /// </summary> /// <param name="scannerId"></param> /// <returns>List of scanned images.</returns> private static List <Bitmap> Acquire(Device device, ScanSettings settings) { String description = device.Properties["Name"].get_Value().ToString(); if (description.ToLower().Contains("brother") || description.Contains("Canon MF4500")) { Logger.Log("Starting acquisition (Brother)"); return(AcquireBrother(device, settings)); } else { Logger.Log("Starting acquisition (Normal)"); return(AcquireNormal(device, settings)); } }
public static List <Bitmap> StartScan() { List <Bitmap> result = new List <Bitmap>(); if (Program.config.SavePath == "") { Program.ShowError("Vui lòng chọn thư mục lưu kết quả"); return(result); } if (!PathWritable(Program.config.SavePath)) { Program.ShowError("Thư mục lưu kết quả không được quyền ghi dữ liệu"); return(result); } if (Program.config.ScannerID == "") { Program.ShowError("Vui lòng chọn máy quét!"); return(result); } //Load settings ScanSettings settings = new ScanSettings(); settings.color = Program.config.ScanColor; settings.dpi = Program.config.ScanDpi; settings.adf = Program.config.ScanADF; settings.tryFlatbed = Program.config.ScanTryFlatbed; try { result = WIAScanner.Scan(Program.config.ScannerID, settings); //Call garbage collector GC.Collect(); } catch (Exception ex) { Program.ShowError(ex); } return(result); }
/*=================== * Scanning routines *===================*/ private static void SetDeviceHandling(ref Device device, ScanSettings settings) { Logger.Log("Setup device handling"); try { //Setup ADF vs Flatbed if (settings.adf) { SetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT, WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER); } else { SetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT, WIA_DPS_DOCUMENT_HANDLING_SELECT.FLATBED); } } catch (Exception ex) { Logger.Log("Cannot configure scanner mode properly\n " + ex); } }
private static void SetDeviceProperties(ref Device device, ScanSettings settings) { Logger.Log("Setup device properties"); // configure scanning properties Item scan = device.Items[1] as Item; foreach (Property prop in scan.Properties) { switch (prop.PropertyID) { case 6146: //4 is Black-white, gray is 2, color 1 SetProperty(prop, settings.color); break; case 6147: //dots per inch/horizontal SetProperty(prop, settings.dpi); break; case 6148: //dots per inch/vertical SetProperty(prop, settings.dpi); break; case 6149: //x point where to start scan SetProperty(prop, 0); break; case 6150: //y-point where to start scan SetProperty(prop, 0); break; case 6151: //horizontal exent SetProperty(prop, (int)2480); break; case 6152: //vertical extent SetProperty(prop, (int)3507); break; } } }
public static void StartScan() { if (Program.config.SavePath == "") { Program.ShowError("Cannot start scanning, no save path defined"); return; } if (!PathWritable(Program.config.SavePath)) { Program.ShowError("Provided path is not writable"); return; } if (Program.config.ScannerID == "") { Program.ShowError("Cannot start scanning, no scanner device selected."); return; } //Load settings ScanSettings settings = new ScanSettings(); settings.color = Program.config.ScanColor; settings.dpi = Program.config.ScanDpi; settings.adf = Program.config.ScanADF; settings.tryFlatbed = Program.config.ScanTryFlatbed; try { SaveImages(WIAScanner.Scan(Program.config.ScannerID, settings)); //Call garbage collector GC.Collect(); } catch (Exception ex) { Program.ShowError(ex); } }
/// <summary> /// Use scanner to scan an image (with user selecting the scanner from a dialog). /// </summary> /// <returns>Scanned images.</returns> public static List<Image> Scan(ScanSettings settings) { ICommonDialog dialog = new CommonDialog(); Device device = null; try { device = dialog.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false); } catch (Exception) { throw new Exception("Cannot initialize scanner selection window. No WIA scanner installed?"); } if (device != null) { return Acquire(device, settings); } else { throw new Exception("You must first select a WIA scanner."); } }
/// <summary> /// Use scanner to scan an image (with user selecting the scanner from a dialog). /// </summary> /// <returns>Scanned images.</returns> public static List <Bitmap> Scan(ScanSettings settings) { ICommonDialog dialog = new CommonDialog(); Device device = null; try { device = dialog.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false); } catch (Exception) { throw new Exception("Không thể khởi tạo máy scan. Vui lòng kiểm tra lại!"); } if (device != null) { return(Acquire(device, settings)); } else { throw new Exception("Vui lòng chọn máy quét."); } }
/// <summary> /// Use scanner to scan an image (with user selecting the scanner from a dialog). /// </summary> /// <returns>Scanned images.</returns> public static List <Image> Scan(ScanSettings settings) { ICommonDialog dialog = new CommonDialog(); Device device = null; try { device = dialog.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false); } catch (Exception) { throw new Exception("Cannot initialize scanner selection window. No WIA scanner installed?"); } if (device != null) { return(Acquire(device, settings)); } else { throw new Exception("You must first select a WIA scanner."); } }
private static List <Bitmap> AcquireNormal(Device device, ScanSettings settings) { DeviceManager manager = new DeviceManager(); List <Bitmap> images = new List <Bitmap>(); bool hasMorePages = true; Item scan = null; //Acquisition iteration ICommonDialog wiaCommonDialog = new CommonDialog(); while (hasMorePages) { try { //Looks like these need to be done for each iteration SetDeviceHandling(ref device, settings); scan = device.Items[1] as Item; SetDeviceProperties(ref device, settings); } catch (Exception) { throw new Exception("Cannot connect to scanner, please check your device and try again."); } Logger.Log("DEBUG: document handling " + GetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)); Logger.Log("DEBUG: feeder status " + GetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)); try { //Scan image ImageFile image = (ImageFile)scan.Transfer(wiaFormatBMP); if (image != null) { // convert to byte array Byte[] imageBytes = (byte[])image.FileData.get_BinaryData(); // add file to output list Bitmap bmp = new Bitmap(Image.FromStream(new MemoryStream(imageBytes))); images.Add(bmp); //Cleanup image = null; imageBytes = null; } else { Logger.Log("Scan cancelled"); break; } // assume there are no more pages hasMorePages = false; if (settings.adf) { try { //try to read feed ready property (some scanners report ready even if no more pages) int status = GetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS); hasMorePages = (status & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0; Logger.Log("ADF has more pages: " + (hasMorePages ? "Yes" : "No")); } catch { } } } catch (System.Runtime.InteropServices.COMException ex) { switch ((uint)ex.ErrorCode) { case WIA_ERRORS.WIA_ERROR_PAPER_EMPTY: Logger.Log("Paper feed empty"); if (images.Count == 0 && settings.adf && settings.tryFlatbed) { //if no page scanned try try flatbed settings.adf = false; } else { hasMorePages = false; } break; case WIA_ERRORS.WIA_ERROR_PAPER_JAM: Program.ShowError("Paper jam inside the scanner feeder"); break; case WIA_ERRORS.WIA_ERROR_BUSY: Logger.Log("Device is busy, retrying in 2s..."); System.Threading.Thread.Sleep(2000); break; default: break; } } } device = null; return(images); }
private static void SetDeviceProperties(ref Device device, ScanSettings settings) { Logger.Log("Setup device properties"); // configure scanning properties Item scan = device.Items[1] as Item; foreach (Property prop in scan.Properties) { switch (prop.PropertyID) { case 6146: //4 is Black-white, gray is 2, color 1 SetProperty(prop, settings.color); break; case 6147: //dots per inch/horizontal SetProperty(prop, settings.dpi); break; case 6148: //dots per inch/vertical SetProperty(prop, settings.dpi); break; case 6149: //x point where to start scan SetProperty(prop, 0); break; case 6150: //y-point where to start scan SetProperty(prop, 0); break; case 6151: //horizontal exent SetProperty(prop, (int)(8.5 * settings.dpi)); break; case 6152: //vertical extent SetProperty(prop, 11 * settings.dpi); break; } } }
/*=================== * Scanning routines *===================*/ private static void SetDeviceHandling(ref Device device, ScanSettings settings) { Logger.Log("Setup device handling"); try { //Setup ADF vs Flatbed if (settings.adf) SetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT, WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER); else SetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT, WIA_DPS_DOCUMENT_HANDLING_SELECT.FLATBED); } catch (Exception ex) { Logger.Log("Cannot configure scanner mode properly\n " + ex); } }
private static List<Image> AcquireNormal(Device device, ScanSettings settings) { DeviceManager manager = new DeviceManager(); List<Image> images = new List<Image>(); bool hasMorePages = true; Item scan = null; //Acquisition iteration ICommonDialog wiaCommonDialog = new CommonDialog(); while (hasMorePages) { try { //Looks like these need to be done for each iteration SetDeviceHandling(ref device, settings); scan = device.Items[1] as Item; SetDeviceProperties(ref device, settings); } catch (Exception) { throw new Exception("Cannot connect to scanner, please check your device and try again."); } Logger.Log("DEBUG: document handling " + GetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)); Logger.Log("DEBUG: feeder status " + GetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)); try { //Scan image ImageFile image = (ImageFile)wiaCommonDialog.ShowTransfer(scan, wiaFormatBMP, false); if (image != null) { // convert to byte array Byte[] imageBytes = (byte[])image.FileData.get_BinaryData(); // add file to output list images.Add(Image.FromStream(new MemoryStream(imageBytes))); //Cleanup image = null; imageBytes = null; } else { Logger.Log("Scan cancelled"); break; } // assume there are no more pages hasMorePages = false; if (settings.adf) { try { //try to read feed ready property (some scanners report ready even if no more pages) int status = GetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS); hasMorePages = (status & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0; Logger.Log("ADF has more pages: " + (hasMorePages ? "Yes" : "No")); } catch { } } } catch (System.Runtime.InteropServices.COMException ex) { switch ((uint)ex.ErrorCode) { case WIA_ERRORS.WIA_ERROR_PAPER_EMPTY: Logger.Log("Paper feed empty"); if (images.Count == 0 && settings.adf && settings.tryFlatbed) { //if no page scanned try try flatbed settings.adf = false; } else hasMorePages = false; break; case WIA_ERRORS.WIA_ERROR_PAPER_JAM: Program.ShowError("Paper jam inside the scanner feeder"); break; case WIA_ERRORS.WIA_ERROR_BUSY: Logger.Log("Device is busy, retrying in 2s..."); System.Threading.Thread.Sleep(2000); break; default: throw ex; } } } device = null; return images; }
/// <summary> /// Use scanner to scan an image (scanner is selected by its unique id). /// </summary> /// <param name="scannerId"></param> /// <returns>List of scanned images.</returns> private static List<Image> Acquire(Device device, ScanSettings settings) { String description = device.Properties["Name"].get_Value().ToString(); if (description.ToLower().Contains("brother") || description.Contains("Canon MF4500")) { Logger.Log("Starting acquisition (Brother)"); return AcquireBrother(device, settings); } else { Logger.Log("Starting acquisition (Normal)"); return AcquireNormal(device, settings); } }