/// <summary> /// /// </summary> /// <param name="dispositivo"></param> /// <param name="tipo"></param> private static void ConfiguraTipoScan(ref Device dispositivo, TipoLeituraDocumento tipo) { //bool canDuplex = dispositivo. //DeviceSettings.DocumentHandlingCapabilities.HasFlag(DocumentHandlingCapabilities.Dup) int requested = (int)tipo; int supported = (int)LePropriedade(ref dispositivo, DEVICE_PROPERTY_DOCUMENT_HANDLING_CAPABILITIES_ID); if (supported > WIA_DPS_DOCUMENT_HANDLING_SELECT.AUTO_ADVANCE && requested == (int)TipoLeituraDocumento.FeederDuplex) //Device doesn't support Feed/duplex { Mensagens.Add("Configuração de digitalização dupla face não é suportada pelo dispositivo"); requested = (int)TipoLeituraDocumento.Feeder; } if ((requested & supported) != 0) { if ((requested & (int)TipoLeituraDocumento.Feeder) != 0) { GravaPropriedade(ref dispositivo, DEVICE_PROPERTY_PAGES_ID, 1); } GravaPropriedade(ref dispositivo, DEVICE_PROPERTY_DOCUMENT_HANDLING_SELECT_ID, requested); } }
/// <summary> /// /// </summary> /// <param name="scannerId"></param> /// <param name="qualidade"></param> /// <param name="tamanho"></param> /// <param name="tipo"></param> /// <returns></returns> private static List <KeyValuePair <string, Image> > DigitalizaItens(string scannerId, WIAScanQuality qualidade, WIAPageSize tamanho, TipoLeituraDocumento tipo) { List <KeyValuePair <string, Image> > images = new List <KeyValuePair <string, Image> >(); //Dictionary<string,Image> images = new Dictionary<string,Image>(); bool hasMorePages = true; WIA.Item item; Mensagens = new HashSet <string>(); while (hasMorePages) { // select the correct scanner using the provided scannerId parameter WIA.Device device = LocalizaDispositio(scannerId); try { //SetWIAProperty(device.Properties, WIA_DEVICE_PROPERTY_PAGES_ID, 1); //Escolha da forma de scaneamento, Feeder ou Flatbad ConfiguraTipoScan(ref device, tipo); item = device.Items[1] as WIA.Item; AjustaPropriedadesDispositivo(item, 0, 0, 0, 0, 1, qualidade, tamanho); // scan image WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog(); WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, WIA.FormatID.wiaFormatPNG, false); if (image != null) { // save to file string fileName = SalvarPNG(image); images.Add(new KeyValuePair <string, Image>(fileName, Image.FromFile(fileName))); } //determine if there are any more pages waiting WIA.Property documentHandlingSelect = null; WIA.Property documentHandlingStatus = null; foreach (WIA.Property prop in device.Properties) { if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT) { documentHandlingSelect = prop; } if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS) { documentHandlingStatus = prop; } } // assume there are no more pages hasMorePages = false; // may not exist on flatbed scanner but required for feeder if (documentHandlingSelect != null) { // check for document feeder if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0) { hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0); } } } catch (Exception exc) { int error = CodigoErroWIA(exc); Mensagens.Add(DescricaoErro(exc)); if (error == WIA_ERROR_PAPER_EMPTY) { hasMorePages = false; } if (error == 0) { throw exc; } } finally { item = null; } } return(images); }
/// <summary> /// /// </summary> /// <param name="scannerId"></param> /// <param name="qualidade"></param> /// <param name="tamanho"></param> /// <param name="tipo"></param> /// <returns></returns> private static List <KeyValuePair <string, Image> > DigitalizaItensEspecifico(string scannerId, WIAScanQuality qualidade, WIAPageSize tamanho, TipoLeituraDocumento tipo) { List <KeyValuePair <string, Image> > images = new List <KeyValuePair <string, Image> >(); //Dictionary<string,Image> images = new Dictionary<string,Image>(); bool hasMorePages = true; WIA.Item item; Mensagens = new HashSet <string>(); // select the correct scanner using the provided scannerId parameter WIA.DeviceManager manager = new WIA.DeviceManager(); WIA.Device device = null; foreach (WIA.DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == scannerId) { device = info.Connect(); //AcquireNormal(device); Dispositivos[scannerId].Propriedades.Clear(); CarregaPropriedades(device, Dispositivos[scannerId].Propriedades); break; } } // device was not found if (device == null) { // enumerate available devices string availableDevices = ""; foreach (WIA.DeviceInfo info in manager.DeviceInfos) { availableDevices += info.DeviceID + "\n"; } // show error with available devices Mensagens.Add("Não foi possível conectar-se ao o dispositivo especificado\n" + availableDevices); } ConfiguraTipoScan(ref device, tipo); item = device.Items[1] as WIA.Item; AjustaPropriedadesDispositivo(item, 0, 0, 0, 0, 1, qualidade, tamanho); WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog(); while (hasMorePages) { try { //Some scanner need WIA_DPS_PAGES to be set to 1, otherwise all pages are acquired but only one is returned as ImageFile GravaPropriedade(ref device, DEVICE_PROPERTY_PAGES_ID, 1); //Scan image WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, WIA.FormatID.wiaFormatPNG, false); if (image != null) { // save to file string fileName = SalvarPNG(image); images.Add(new KeyValuePair <string, Image>(fileName, Image.FromFile(fileName))); } //determine if there are any more pages waiting WIA.Property documentHandlingSelect = null; WIA.Property documentHandlingStatus = null; foreach (WIA.Property prop in device.Properties) { if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT) { documentHandlingSelect = prop; } if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS) { documentHandlingStatus = prop; } } // assume there are no more pages hasMorePages = false; // may not exist on flatbed scanner but required for feeder if (documentHandlingSelect != null) { // check for document feeder if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0) { hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0); } } } catch (Exception exc) { int error = CodigoErroWIA(exc); Mensagens.Add(DescricaoErro(exc)); if (error == WIA_ERROR_PAPER_EMPTY) { hasMorePages = false; } if (error == 0) { throw exc; } } } device = null; return(images); }
/// <summary> /// Inicia a digitalização dos documentos depositados no dispositivo selecionado /// </summary> /// <param name="scannerId"></param> /// <param name="tamanho"></param> /// <param name="tipoDigitalizacao"></param> /// <returns></returns> public static List <KeyValuePair <string, Image> > Scan(string scannerId, WIAPageSize tamanho, TipoLeituraDocumento tipoDigitalizacao) { WIA.Device device = LocalizaDispositio(scannerId); String description = device.Properties["Name"].get_Value().ToString(); if (description.ToLower().Contains("brother") || description.Contains("Canon MF4500")) { return(DigitalizaItensEspecifico(scannerId, WIAScanQuality.Final, tamanho, tipoDigitalizacao)); } else { return(DigitalizaItens(scannerId, WIAScanQuality.Final, tamanho, tipoDigitalizacao)); } }