首页 > 解决方案 > scanner become slow after i AdjustScannerSettings

问题描述

first of all , I can reach full speed if i don't adjust scanner settings like (dpi, width,height,color) via wia. However, i can reach full speed using "window fax and scan" application even adjust scanner settngs.

apparently something is wrong with my code . i'm using the library from jeske (https://github.com/jeske/WIADemo/blob/master/WIADemo/WIAScanner.cs)

right now, i can connect to my scanner (epson ds-780N) and scan via automatic document feeder successfully . the nightmare is that it will cost me 5 sec to transfer data from my scanner (ShowTransfer function). this scanner should scanner 45 pages per minute, but all i get is 10 pages.

        public static List<PendingFile> Scan(string scannerId, WIAScanQuality quality, WIAPageSize pageSize,string category)
                {

                    List<PendingFile> results = new List<PendingFile>();
                    bool hasMorePages = true;
                    int numbrPages = 0;
                    WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog();

                    // connect the correct scanner using the provided scannerId parameter
                    var device = connect(scannerId);

                    WIA.Item item = device.Items[1] as WIA.Item;

int dpi = 100;
            int width_pixels = (int)(8.3f * dpi);
            int height_pixels = (int)(11.7f * dpi);
            // if i call this function , scanner will become very slow.
            AdjustScannerSettings(item, dpi, 0, 0, width_pixels, height_pixels, 0, 0, 1);
                    do
                    {
                        PendingFile fileInfo = genPendingInfo(category);

                        try
                        {
                            // scan image
                            WIA.ImageFile image = item.Transfer(wiaFormatJPEG);
                            //WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatJPEG, false);

                            // save to temp file
                            Byte[] imageBytes = (byte[])image.FileData.get_BinaryData(); // <– Converts the ImageFile to a byte array
                            MemoryStream ms = new MemoryStream(imageBytes);
                            Image img = Image.FromStream(ms);
                            string fileName = Utils.genFileName(fileInfo);

                            using (EncoderParameters encoderParameters = new EncoderParameters(1))
                            using (EncoderParameter encoderParameter = new EncoderParameter(Encoder.Quality, 90L))
                            {
                                ImageCodecInfo codecInfo = ImageCodecInfo.GetImageDecoders().First(codec => codec.FormatID == ImageFormat.Jpeg.Guid);
                                encoderParameters.Param[0] = encoderParameter;
                                img.Save(fileName, codecInfo, encoderParameters);
                                img.Dispose();
                            }

                            Image temp = Image.FromFile(fileName);
                            Image thumb = temp.GetThumbnailImage(126, 180, () => false, IntPtr.Zero);
                            thumb.Save(Path.ChangeExtension(fileName, ".thumbnail.jpg"));
                            thumb.Dispose();

                            image = null;
                            // add file to output list
                            results.Add(fileInfo);
                            temp.Dispose();
                            numbrPages++;

                        }
                        catch (System.Runtime.InteropServices.COMException ex)
                        {
                            switch ((uint)ex.ErrorCode)
                            {
                                case WIA_ERRORS.WIA_ERROR_PAPER_EMPTY:
                                    hasMorePages = false;                            
                                    break;

                                case WIA_ERRORS.WIA_ERROR_PAPER_JAM:
                                    MessageBox.Show("Paper jam inside the scanner feeder");
                                    hasMorePages = false;
                                    break;

                                default:
                                    throw ex;
                            }
                        }

                    } while (hasMorePages);
                    device = null;
                    return results;
                }

    private static void SetWIAProperty(WIA.IProperties properties,
               object propName, object propValue)
        {
            WIA.Property prop = properties.get_Item(ref propName);
            prop.set_Value(ref propValue);
        }


    private static void AdjustScannerSettings(IItem scannnerItem, int scanResolutionDPI, int scanStartLeftPixel, int scanStartTopPixel,
             int scanWidthPixels, int scanHeightPixels, int brightnessPercents, int contrastPercents, int colorMode )
            {
                const string WIA_SCAN_COLOR_MODE = "6146";
                const string WIA_HORIZONTAL_SCAN_RESOLUTION_DPI = "6147";
                const string WIA_VERTICAL_SCAN_RESOLUTION_DPI = "6148";
                const string WIA_HORIZONTAL_SCAN_START_PIXEL = "6149";
                const string WIA_VERTICAL_SCAN_START_PIXEL = "6150";
                const string WIA_HORIZONTAL_SCAN_SIZE_PIXELS = "6151";
                const string WIA_VERTICAL_SCAN_SIZE_PIXELS = "6152";
                const string WIA_SCAN_BRIGHTNESS_PERCENTS = "6154";
                const string WIA_SCAN_CONTRAST_PERCENTS = "6155";

                SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
                SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
                SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_START_PIXEL, scanStartLeftPixel);
                SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_START_PIXEL, scanStartTopPixel);
                SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_SIZE_PIXELS, scanWidthPixels);
                SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_SIZE_PIXELS, scanHeightPixels);
                SetWIAProperty(scannnerItem.Properties, WIA_SCAN_BRIGHTNESS_PERCENTS, brightnessPercents);
                SetWIAProperty(scannnerItem.Properties, WIA_SCAN_CONTRAST_PERCENTS, contrastPercents);
                SetWIAProperty(scannnerItem.Properties, WIA_SCAN_COLOR_MODE, colorMode);
            }

标签: c#epsonwia

解决方案


推荐阅读