public WIA.ImageFile ScanAndSaveOnePage() { WIA.CommonDialog Dialog1 = new WIA.CommonDialogClass(); WIA.DeviceManager DeviceManager1 = new WIA.DeviceManagerClass(); System.Object Object1 = null; System.Object Object2 = null; WIA.Device Scanner = null; try { Scanner = Dialog1.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType, false, false); } catch { MessageBox.Show("请确认是否联系设备"); return(null); throw; } WIA.Item Item1 = Scanner.Items[1]; setItem(Item1, "4104", 24); setItem(Item1, "6146", 2); setItem(Item1, "6147", 150); setItem(Item1, "6148", 150); setItem(Item1, "6151", 150 * 8.5); setItem(Item1, "6152", 150 * 11); WIA.ImageFile Image1 = new WIA.ImageFile(); WIA.ImageProcess ImageProcess1 = new WIA.ImageProcess(); Object1 = (Object)"Convert"; ImageProcess1.Filters.Add(ImageProcess1.FilterInfos.get_Item(ref Object1).FilterID, 0); Object1 = (Object)"FormatID"; Object2 = (Object)WIA.FormatID.wiaFormatBMP; ImageProcess1.Filters[1].Properties.get_Item(ref Object1).set_Value(ref Object2); Object1 = null; Object2 = null; Image1 = (WIA.ImageFile)Item1.Transfer(WIA.FormatID.wiaFormatBMP); return(Image1); //string DestImagePath = @"C:\test.bmp"; //File.Delete(DestImagePath); //Image1.SaveFile(DestImagePath); }
/// <summary> /// Start scan /// </summary> public void ScanAsync(ScannerInfo source) { if (source == null) { return; } WIA.Device wiaDevice = source.GetDevice(); // Manager WIA.DeviceManager wiaManager = new WIA.DeviceManager(); bool hasMorePages = true; while (hasMorePages) { try { // Get items WIA.Item wiaItem = wiaDevice.Items[1]; int inColor = 2, dpi = 300; wiaItem.Properties["6146"].set_Value((int)inColor); //Item MUST be stored in a variable THEN the properties must be set. wiaItem.Properties["6147"].set_Value(dpi); wiaItem.Properties["6148"].set_Value(dpi); // var imageFile = (ImageFile)(new CommonDialog()).ShowTransfer(wiaItem, "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}", false); //wiaItem.Transfer("{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}") as WIA.ImageFile; var imageFile = wiaItem.Transfer("{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}") as WIA.ImageFile; if (this.ScanCompleted != null) { this.ScanCompleted(this, new ScanCompletedEventArgs(imageFile.FileData.get_BinaryData())); } } catch (Exception) { break; } finally { //determine if there are any more pages waiting WIA.Property documentHandlingSelect = null; WIA.Property documentHandlingStatus = null; foreach (WIA.Property prop in wiaDevice.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; } } hasMorePages = false; //assume there are no more pages if (documentHandlingSelect != null) //may not exist on flatbed scanner but required for feeder { //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); } } if (hasMorePages && this.SingleOnly) { hasMorePages = false; } } } }
private void DoScan() { WIA.Item item = default(WIA.Item); WIA.ImageFile Img = default(WIA.ImageFile); WIA.CommonDialog WiaCommonDialog = new CommonDialogClass(); bool hasMorePages = true; int x = 0; int numPages = 0; string ImgMain = null; string ImgMainName = null; try { if (treealfresco.SelectedNode == null) { MessageBox.Show("Please select a Space to store your data into", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ResultSetRowNode node = (ResultSetRowNode)treealfresco.SelectedNode.Tag; LocationUuid = node.id; LocationName = treealfresco.SelectedNode.Text; } catch (Exception ex) { MessageBox.Show(ex.Message); } while (hasMorePages) { //Create DeviceManager DeviceManager manager = new DeviceManagerClass(); Device WiaDev = null; foreach (DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == this.DeviceID) { WIA.Properties infoprop = null; infoprop = info.Properties; //connect to scanner WiaDev = info.Connect(); break; // TODO: might not be correct. Was : Exit For } } //Start Scan Img = null; item = WiaDev.Items[1] as WIA.Item; try { Img = (ImageFile)WiaCommonDialog.ShowTransfer(item, wiaFormatTIFF, false); //process image: //one would do image processing here //Save to file string jpegGuid = null; //retrieves jpegKey from registry, used in saving JPEG Microsoft.Win32.RegistryKey jpegKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("CLSID\\{D2923B86-15F1-46FF-A19A-DE825F919576}\\SupportedExtension\\.jpg"); jpegGuid = (string)jpegKey.GetValue("FormatGUID"); //loops through available formats for the captured item, looking for the JPG format foreach (string format in item.Formats) { if ((format == jpegGuid)) { //transfers image to an imagefile object Img = (WIA.ImageFile)item.Transfer(WIA.FormatID.wiaFormatTIFF); int Counter = 0; //counter in loop appended to filename bool LoopAgain = true; //searches directory, gets next available picture name while (!(LoopAgain == false)) { string File = SavePath + "\\"; File += txtPrefix.Text; File += Counter; File += ".tiff"; string Filename = txtPrefix.Text; Filename += Counter; Filename += ".tiff"; if (System.IO.File.Exists(Filename)) { //file exists, delete it System.IO.File.Delete(Filename); } if (numPages == 0) { Img.SaveFile(Filename); } ImgMain = File; ImgMainName = Filename; if (numPages > 0) { int y = Counter; y = Counter - 1; ImgMain = SavePath + "\\"; ImgMain += txtPrefix.Text; ImgMain += y; ImgMain += ".tiff"; ImgMainName = txtPrefix.Text; ImgMainName += y; ImgMainName += ".tiff"; //createtiff(ImgMain, File); } numPages += 1; Counter = Counter + 1; } } } } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } finally { item = null; //determine if there are any more pages waiting WIA.Property documentHandlingSelect = null; WIA.Property documentHandlingStatus = null; foreach (WIA.Property prop in WiaDev.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; } } hasMorePages = false; UploadNow(ImgMainName, ImgMain); MessageBox.Show(ImgMain + " uploaded"); //assume there are no more pages if (documentHandlingSelect != null) { //may not exist on flatbed scanner but required for feeder //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); } } x += 1; } } }
protected void btnEscanear_Click(object sender, EventArgs e) { WIA.CommonDialog Dialog1 = new WIA.CommonDialog(); WIA.DeviceManager DeviceManager1 = new WIA.DeviceManager(); WIA.Device Scanner = null; Scanner = Dialog1.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType, false, true); WIA.Item Item1 = Scanner.Items[1]; WIA.ImageFile Imagen = new WIA.ImageFile(); Imagen = (WIA.ImageFile)Item1.Transfer("{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"); string DestImagePath = @"~\imagenes\temporal\Scan.png"; File.Delete(DestImagePath); Imagen.SaveFile(DestImagePath); Image1.ImageUrl = @"~\imagenes\temporal\Scan.png"; string nombreArchivo = "Scan.png"; selectedTipo = dropTipoDocumento.SelectedValue.ToString(); ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0); Bitmap image1 = new Bitmap(Server.MapPath(@"imagenes").ToString() + @"\" + nombreArchivo); Bitmap image2 = null; if (dropTipoDocumento.SelectedValue == "acta") { image2 = new Bitmap(Server.MapPath(@"imagenes").ToString() + @"\templer.jpg"); } else if (dropTipoDocumento.SelectedValue == "curp") { image2 = new Bitmap(Server.MapPath(@"imagenes").ToString() + @"\curptemp.jpg"); } else { } TemplateMatch[] matchings = tm.ProcessImage(image1, image2, new Rectangle(new Point(0, 0), new Size(250, 250))); if (matchings[0].Similarity > 0.8f) { int idDocumentos = Convert.ToInt32(datos.SelectValor("select count(*) from Documentos")) + 1; if (dropCategoria.SelectedValue == "InfoPersonal") { // Son Similares this.Response.Write("<script language='JavaScript'>window.alert('la imagen se subio correctamente')</script>"); Imagen.SaveFile(Server.MapPath(@"imagenes").ToString() + @"\InfoPersonal\" + selectedTipo + "_" + Session["userName"] + "_" + "Scan.png"); string idEmpleado = datos.SelectValor("select idEmpleado from Empleado where correoElectronico='" + Session["userName"] + "'"); datos.Comand("insert into Documentos values(" + idDocumentos + "," + idEmpleado + ",'" + selectedTipo + "','" + dropCategoria.SelectedValue + "','" + Server.MapPath(@"imagenes").ToString() + @"\InfoPersonal\" + selectedTipo + "_" + Session["userName"] + "_" + "Scan.png" + "')"); } else if (dropCategoria.SelectedValue == "InfoAcademica") { this.Response.Write("<script language='JavaScript'>window.alert('la imagen se subio correctamente')</script>"); Imagen.SaveFile(Server.MapPath(@"imagenes").ToString() + @"\InfoAcademica\" + selectedTipo + "_" + Session["userName"] + "_" + "Scan.png"); string idEmpleado = datos.SelectValor("select idEmpleado from Empleado where correoElectronico='" + Session["userName"] + "'"); datos.Comand("insert into Documentos values(" + idDocumentos + "," + idEmpleado + ",'" + selectedTipo + "','" + dropCategoria.SelectedValue + "','" + Server.MapPath(@"imagenes").ToString() + @"\InfoAcademica\" + selectedTipo + "_" + Session["userName"] + "_" + "Scan.png" + "')"); } else if (dropCategoria.SelectedValue == "InfoLaboral") { this.Response.Write("<script language='JavaScript'>window.alert('la imagen se subio correctamente')</script>"); Imagen.SaveFile(Server.MapPath(@"imagenes").ToString() + @"\InfoLaboral\" + selectedTipo + "_" + Session["userName"] + "_" + "Scan.png"); string idEmpleado = datos.SelectValor("select idEmpleado from Empleado where correoElectronico='" + Session["userName"] + "'"); datos.Comand("insert into Documentos values(" + idDocumentos + "," + idEmpleado + ",'" + selectedTipo + "','" + dropCategoria.SelectedValue + "','" + Server.MapPath(@"imagenes").ToString() + @"\InfoLaboral\" + selectedTipo + "_" + Session["userName"] + "_" + "Scan.png" + "')"); } } else { //No son similares this.Response.Write("<script language='JavaScript'>window.alert('la imagen que subiste no coincide con el tipo de archivo que seleccionaste')</script>"); Image1.ImageUrl = ""; } }
private void btncapture_Click(object sender, EventArgs e) { if (treealfresco.SelectedNode == null) { MessageBox.Show("Please select a Space to store your data into", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); //exit btncapture_Click; } if (rdscanner.Checked == true) { Enabled = false; _settings = new ScanSettings() { UseDocumentFeeder = useAdfCheckBox.Checked, ShowTwainUI = useUICheckBox.Checked, Resolution = blackAndWhiteCheckBox.Checked ? ResolutionSettings.Fax : ResolutionSettings.ColourPhotocopier }; try { _twain.StartScanning(_settings); } catch (TwainException ex) { MessageBox.Show(ex.Message); Enabled = true; } } else { WIA.Item item = default(WIA.Item); WIA.CommonDialog WiaCommonDialog = new CommonDialogClass(); try { //Check if the device is scanner or not if (rdscanner.Checked == true) { //scans the image using the Scanner only (ADF or Flatbed) DoScan(); } else { item = SelectedDevice.ExecuteCommand(WIA.CommandID.wiaCommandTakePicture); } } catch (System.Exception ex) { MessageBox.Show("Problem Taking Picture. Please make sure that the camera is plugged in and is not in use by another application. " + "\r\n" + "Extra Info:" + ex.Message, "Problem Grabbing Picture", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); return; } try { //Validate if a image location is selected or not if (treealfresco.SelectedNode == null) { MessageBox.Show("Please select a Space to store your data into", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ResultSetRowNode node = (ResultSetRowNode)treealfresco.SelectedNode.Tag; LocationUuid = node.id; LocationName = treealfresco.SelectedNode.Text; } catch (Exception ex) { MessageBox.Show(ex.Message); } try { //Executes the device's TakePicture command based on selected image format if (rdjpeg.Checked == true) { string jpegGuid = null; //retrieves jpegKey from registry, used in saving JPEG Microsoft.Win32.RegistryKey jpegKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("CLSID\\{D2923B86-15F1-46FF-A19A-DE825F919576}\\SupportedExtension\\.jpg"); jpegGuid = (string)jpegKey.GetValue("FormatGUID"); //loops through available formats for the captured item, looking for the JPG format foreach (string format in item.Formats) { if ((format == jpegGuid)) { //transfers image to an imagefile object WIA.ImageFile imagefile = (WIA.ImageFile)item.Transfer(format); int Counter = 0; //counter in loop appended to filename bool LoopAgain = true; //searches directory, gets next available picture name while (!(LoopAgain == false)) { string File = SavePath + "\\"; File += txtPrefix.Text; File += Counter; File += ".jpg"; string Filename = txtPrefix.Text; Filename += Counter; Filename += ".jpg"; //if file doesnt exist, save the file if (!System.IO.File.Exists(Filename)) { SavedFilePath = Filename; imagefile.SaveFile(Filename); //saves file to disk //Upload the file to Alfresco UploadNow(Filename, File); MessageBox.Show(File + " uploaded"); LoopAgain = false; } Counter = Counter + 1; } } } } else if (rdgif.Checked == true) { string jpegGuid = null; //retrieves jpegKey from registry, used in saving JPEG Microsoft.Win32.RegistryKey jpegKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("CLSID\\{D2923B86-15F1-46FF-A19A-DE825F919576}\\SupportedExtension\\.jpg"); jpegGuid = (string)jpegKey.GetValue("FormatGUID"); //loops through available formats for the captured item, looking for the JPG format foreach (string format in item.Formats) { if ((format == jpegGuid)) { //transfers image to an imagefile object WIA.ImageFile imagefile = (WIA.ImageFile)item.Transfer(WIA.FormatID.wiaFormatGIF); int Counter = 0; //counter in loop appended to filename bool LoopAgain = true; //searches directory, gets next available picture name while (!(LoopAgain == false)) { string File = SavePath + "\\"; File += txtPrefix.Text; File += Counter; File += ".gif"; string Filename = txtPrefix.Text; Filename += Counter; Filename += ".gif"; //if file doesnt exist, save the file if (!System.IO.File.Exists(Filename)) { SavedFilePath = Filename; imagefile.SaveFile(Filename); //saves file to disk // Upload the file to Alfresco UploadNow(Filename, File); MessageBox.Show(File + " uploaded"); LoopAgain = false; } Counter = Counter + 1; } } } } else if (rdtiff.Checked == true) { string jpegGuid = null; //retrieves jpegKey from registry, used in saving JPEG Microsoft.Win32.RegistryKey jpegKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("CLSID\\{D2923B86-15F1-46FF-A19A-DE825F919576}\\SupportedExtension\\.jpg"); jpegGuid = (string)jpegKey.GetValue("FormatGUID"); //loops through available formats for the captured item, looking for the JPG format foreach (string format in item.Formats) { if ((format == jpegGuid)) { //transfers image to an imagefile object WIA.ImageFile imagefile = (WIA.ImageFile)item.Transfer(WIA.FormatID.wiaFormatTIFF); int Counter = 0; //counter in loop appended to filename bool LoopAgain = true; //searches directory, gets next available picture name while (!(LoopAgain == false)) { string File = SavePath + "\\"; File += txtPrefix.Text; File += Counter; File += ".tiff"; string Filename = txtPrefix.Text; Filename += Counter; Filename += ".tiff"; //if file doesnt exist, save the file if (!System.IO.File.Exists(Filename)) { SavedFilePath = Filename; imagefile.SaveFile(Filename); //saves file to disk UploadNow(Filename, File); MessageBox.Show(File + " uploaded"); LoopAgain = false; } Counter = Counter + 1; } } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }