예제 #1
0
        /// <summary>
        /// Performs scan from the scanner ADF and produces image on both sides
        /// </summary>
        /// <param name="scanner_uri"></param>
        /// <returns></returns>
        public static List <byte[]> GetScannedImageADF(string scanner_uri, bool duplex, int type, int threshold)
        {
            var selected_scanner = GetDesiredScanner(scanner_uri).Connect();
            var scannerItem      = selected_scanner.Items[1];

            SetDeviceIntProperty(ref selected_scanner, 3096, 1);


            if (duplex)
            {
                // set the scanner to get papers from ADF on both sides
                SetDeviceIntProperty(ref selected_scanner, 3088, 5);
            }
            else
            {
                // set the scanner to get papers from ADF on one side
                SetDeviceIntProperty(ref selected_scanner, 3088, 1);
            }


            // greyscale instead of black and white
            SetWIAProperty(scannerItem.Properties, "6146", type);
            SetWIAProperty(scannerItem.Properties, "6159", threshold);
            try
            {
                // SET DPI to 150
                SetWIAProperty(scannerItem.Properties, "6147", 150);
                SetWIAProperty(scannerItem.Properties, "6148", 150);

                // Set paper A4 resolution to be
                SetWIAProperty(scannerItem.Properties, "6151", 1754);
                SetWIAProperty(scannerItem.Properties, "6152", 1240);
            }
            catch (Exception)
            {
            }

            System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.SaveFlag;
            System.Drawing.Imaging.Encoder encComp = System.Drawing.Imaging.Encoder.Compression;
            EncoderParameters encoderParameters    = new EncoderParameters(2);

            ImageCodecInfo encoderInfo = ImageCodecInfo.GetImageEncoders().First(i => i.MimeType == "image/tiff");

            encoderParameters.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.MultiFrame);


            encoderParameters.Param[1] = new EncoderParameter(encComp, (long)EncoderValue.CompressionLZW);


            var front  = ImageFileToStream((ImageFile)scannerItem.Transfer(FormatID.wiaFormatPNG));
            var tifImg = new TiffImage(front);

            while (true)
            {
                try
                {
                    var imgfrnt = new TiffImage(ImageFileToStream((ImageFile)scannerItem.Transfer(FormatID.wiaFormatPNG)));
                    tifImg.Append(imgfrnt);
                }
                catch (Exception)
                {
                    break;
                }
            }
            var ret = new List <byte[]>();

            foreach (var item in tifImg.ToByteArrayList())
            {
                var rotated_img = RotateIfHorizontal(item);
                if (type == 4)
                {
                    ret.Add(convertToBlackAndWhite(rotated_img));
                }
                else
                {
                    ret.Add(rotated_img);
                }
            }
            return(ret);
        }