示例#1
0
        private static bool IsWia2AvailableImpl()
        {
            try
            {
                WIA.DeviceManagerClass dmc = new WIA.DeviceManagerClass();
                return(true);
            }

            catch
            {
                return(false);
            }
        }
示例#2
0
        internal static Webcam[] Enumerate()
        {
            List <Webcam> result = new List <Webcam>();

            // directshow enumeration
            try {
                DsDevice[] dsDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
                for (int x = 0; x < dsDevices.Length; x++)
                {
                    result.Add(new WebcamDirectShow(x, dsDevices[x].Name));
                }
            } catch (Exception) { }

            // wia enumeration
            try {
                WIA.DeviceManager deviceManager = new WIA.DeviceManagerClass();
                foreach (WIA.DeviceInfo deviceInfo in deviceManager.DeviceInfos)
                {
                    if ((deviceInfo.Type == WIA.WiaDeviceType.CameraDeviceType) || (deviceInfo.Type == WIA.WiaDeviceType.VideoDeviceType))
                    {
                        result.Add(new WebcamWIA(deviceInfo));
                    }
                }
            } catch (Exception) { }

            // avicap32 enumeration
            try {
                for (short x = 0; x < 10; x++)
                {
                    string name    = new string(' ', 255);
                    string version = new string(' ', 255);
                    if (NativeMethods.capGetDriverDescriptionA(x, ref name, name.Length, ref version, version.Length))
                    {
                        if (name.IndexOf('\x00') > -1)
                        {
                            name = name.Substring(0, name.IndexOf('\x00'));
                        }
                        if (version.IndexOf('\x00') > -1)
                        {
                            version = version.Substring(0, version.IndexOf('\x00'));
                        }
                        result.Add(new WebcamAVICap(x, name.Trim()));
                    }
                }
            } catch (Exception) { }

            return(result.ToArray());
        }
示例#3
0
        private static bool IsWia2AvailableImpl()
        {
            try
            {
                WIA.DeviceManagerClass dmc = new WIA.DeviceManagerClass();
                Marshal.ReleaseComObject(dmc);
                dmc = null;

                return(true);
            }

            catch
            {
                return(false);
            }
        }
示例#4
0
        private static bool IsWia2AvailableImpl()
        {
            try
            {
                WIA.DeviceManagerClass dmc = new WIA.DeviceManagerClass();
                return true;
            }

            catch
            {
                return false;
            }
        }
示例#5
0
        private static bool IsWia2AvailableImpl()
        {
            try
            {
                WIA.DeviceManagerClass dmc = new WIA.DeviceManagerClass();
                Marshal.ReleaseComObject(dmc);
                dmc = null;

                return true;
            }

            catch
            {
                return false;
            }
        }
示例#6
0
        internal static Webcam[] Enumerate()
        {
            List<Webcam> result = new List<Webcam>();

            // directshow enumeration
            try {
                DsDevice[] dsDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
                for (int x = 0; x < dsDevices.Length; x++) {
                    result.Add(new WebcamDirectShow(x, dsDevices[x].Name));
                }
            } catch (Exception) { }

            // wia enumeration
            try {
                WIA.DeviceManager deviceManager = new WIA.DeviceManagerClass();
                foreach (WIA.DeviceInfo deviceInfo in deviceManager.DeviceInfos) {
                    if ((deviceInfo.Type == WIA.WiaDeviceType.CameraDeviceType) || (deviceInfo.Type == WIA.WiaDeviceType.VideoDeviceType)) {
                        result.Add(new WebcamWIA(deviceInfo));
                    }
                }
            } catch (Exception) { }

            // avicap32 enumeration
            try {
                for (short x = 0; x < 10; x++) {
                    string name = new string(' ', 255);
                    string version = new string(' ', 255);
                    if (NativeMethods.capGetDriverDescriptionA(x, ref name, name.Length, ref version, version.Length)) {
                        if (name.IndexOf('\x00') > -1) name = name.Substring(0, name.IndexOf('\x00'));
                        if (version.IndexOf('\x00') > -1) version = version.Substring(0, version.IndexOf('\x00'));
                        result.Add(new WebcamAVICap(x, name.Trim()));
                    }
                }
            } catch (Exception) { }

            return result.ToArray();
        }
示例#7
0
        private void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                // parametros de scanner
                int resolucion  = 150;
                int anchoImagen = 1200;
                int altoImagen  = 1500;
                int modoColor   = 1;

                // la mas importante
                bool HayMasPaginas = true; // tiene mas paginas :)

                // ruta principal donde se guardan los archivos
                string rutaBase = string.Format("{0}\\{1}", Application.StartupPath, DateTime.Now.ToString("yyyMMddhhmmss"));

                Directory.CreateDirectory(rutaBase);

                // elegir escanner
                WIA.CommonDialog dialogoSeleccionaEscanner = new WIA.CommonDialog();
                WIA.Device       escaner = dialogoSeleccionaEscanner.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType, true, false);

                if (escaner != null)
                {
                    idEscaner = escaner.DeviceID;
                }
                else
                {
                    MessageBox.Show("No se eligió Escaner");
                    return;
                }

                int x             = 0;
                int numeroPaginas = 0;

                while (HayMasPaginas)
                {
                    // mientras la bandera de mas paginas este activa
                    // conectar escaner
                    // mostrando avance de escaneo
                    WIA.CommonDialog  dialogoEscaneo = new WIA.CommonDialogClass();
                    WIA.DeviceManager manager        = new WIA.DeviceManagerClass();
                    WIA.Device        dispoWIA       = null;

                    // cargando el dispositivo
                    foreach (WIA.DeviceInfo scannerInfo in manager.DeviceInfos)
                    {
                        if (scannerInfo.DeviceID == idEscaner)
                        {
                            WIA.Properties infoprop = null;
                            infoprop = scannerInfo.Properties;

                            dispoWIA = scannerInfo.Connect();

                            break;
                        }
                    }

                    // inicia escaneo
                    WIA.ImageFile imgEscaneo  = null;
                    WIA.Item      escanerItem = dispoWIA.Items[1] as WIA.Item;

                    AdjustScannerSettings(escanerItem, resolucion, 0, 0, anchoImagen, altoImagen, 0, 0, modoColor);

                    // tomando la imagen
                    imgEscaneo = (WIA.ImageFile)dialogoEscaneo.ShowTransfer(escanerItem, WIA.FormatID.wiaFormatPNG, false);

                    string RutaNombreEscaneo = String.Format("{0}\\{1}.png", rutaBase, x.ToString());

                    if (File.Exists(RutaNombreEscaneo))
                    {
                        //file exists, delete it
                        File.Delete(RutaNombreEscaneo);
                    }

                    imgEscaneo.SaveFile(RutaNombreEscaneo);
                    numeroPaginas++;
                    x++;
                    imgEscaneo = null;

                    // preguntar si hay mas documentos pendientes de escaneo
                    if (MessageBox.Show("¿Deseas escanear otro documento?. Antes de seleccionar 'Yes' Por favor introduzca el documento a escanear.", "Escaner", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    {
                        HayMasPaginas = false;
                    }
                }

                // una vez escaneado los dosucmentos, se hace un pdf con ellos para despues hacerlo zip
                // rutaBase es el nombre de la variable de la carpeta
                MessageBox.Show("Escaneo finalizado. Armando archivo pdf");
                string nombreArchivoPdf = string.Format("{0}\\{1}.pdf", rutaBase, DateTime.Now.ToString("yyyMMddhhmmss"));

                Document  pdfDoc = new Document(PageSize.LETTER, 2, 2, 2, 2);
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(nombreArchivoPdf, FileMode.CreateNew));

                pdfDoc.Open();

                // tomar las imagenes del escaneo
                for (int i = 0; i < x; i++)
                {
                    // tomar la imagen
                    string rutaImagen = String.Format("{0}\\{1}.png", rutaBase, i.ToString());

                    PdfContentByte cb = writer.DirectContent;

                    using (FileStream fsSource = new FileStream(rutaImagen, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        Image img = Image.GetInstance(fsSource);
                        img.ScaleToFit(pdfDoc.PageSize.Width, pdfDoc.PageSize.Height);
                        pdfDoc.Add(img);
                    }
                }

                pdfDoc.Close();

                MessageBox.Show("Archivo pdf generado. Generando archivo zip");

                // comprimir
                FileInfo        pdfFileInfo      = new FileInfo(nombreArchivoPdf);
                string          nombreArchivoZip = string.Format("{0}\\{1}.zip", rutaBase, DateTime.Now.ToString("yyyMMddhhmmss"));
                ZipOutputStream zipStream        = new ZipOutputStream(File.Create(nombreArchivoZip));
                zipStream.SetLevel(6);

                // abrir el archivo para escribirlo
                FileStream pdfFile = File.OpenRead(nombreArchivoPdf);
                byte[]     buffer  = new byte[pdfFile.Length];
                pdfFile.Read(buffer, 0, buffer.Length);

                // agregando el archivo a la carpeta comprimida
                ZipEntry entry = new ZipEntry(Path.GetFileName(nombreArchivoPdf))
                {
                    DateTime = pdfFileInfo.LastWriteTime,
                    Size     = pdfFile.Length
                };

                pdfFile.Close();

                Crc32 objCrc32 = new Crc32();
                objCrc32.Reset();
                objCrc32.Update(buffer);

                entry.Crc = objCrc32.Value;
                zipStream.PutNextEntry(entry);
                zipStream.Write(buffer, 0, buffer.Length);

                zipStream.Finish();
                zipStream.Close();

                MessageBox.Show("Archivo zip generado. Enviando archivo por FTP");

                // enviar el documento en ftp

                MessageBox.Show("proceso completo.");
            }
            catch (Exception ex)
            {
                // cachando errores com
                if (ex is COMException)
                {
                    // Convert the error code to UINT
                    uint errorCode = (uint)((COMException)ex).ErrorCode;

                    // See the error codes
                    if (errorCode == 0x80210006)
                    {
                        MessageBox.Show("The scanner is busy or isn't ready");
                    }
                    else if (errorCode == 0x80210064)
                    {
                        MessageBox.Show("The scanning process has been cancelled.");
                    }
                    else if (errorCode == 0x8021000C)
                    {
                        MessageBox.Show("There is an incorrect setting on the WIA device.");
                    }
                    else if (errorCode == 0x80210005)
                    {
                        MessageBox.Show("The device is offline. Make sure the device is powered on and connected to the PC.");
                    }
                    else if (errorCode == 0x80210001)
                    {
                        MessageBox.Show("An unknown error has occurred with the WIA device.");
                    }
                }
                else
                {
                    MessageBox.Show("Error " + ex.Message);
                }
            }
        }