Exemple #1
2
        public PixelType GetPixelType(ScanSettings scanSettings)
        {
            switch (scanSettings.Resolution.ColourSetting)
            {
                case ColourSetting.BlackAndWhite:
                    return PixelType.BlackAndWhite;

                case ColourSetting.GreyScale:
                    return PixelType.Grey;

                case ColourSetting.Colour:
                    return PixelType.Rgb;
            }

            throw new NotImplementedException();
        }
Exemple #2
0
        private void scan_Click(object sender, EventArgs e)
        {
            Enabled = false;

            _settings = new ScanSettings();
            _settings.UseDocumentFeeder = useAdfCheckBox.Checked;
            _settings.ShowTwainUI = useUICheckBox.Checked;
            _settings.ShowProgressIndicatorUI = showProgressIndicatorUICheckBox.Checked;
            _settings.UseDuplex = useDuplexCheckBox.Checked;
            _settings.Resolution =
                blackAndWhiteCheckBox.Checked
                ? ResolutionSettings.Fax : ResolutionSettings.ColourPhotocopier;
            _settings.Area = !checkBoxArea.Checked ? null : AreaSettings;
            _settings.ShouldTransferAllPages = true;

            _settings.Rotation = new RotationSettings()
            {
                AutomaticRotate = autoRotateCheckBox.Checked,
                AutomaticBorderDetection = autoDetectBorderCheckBox.Checked
            };

            try
            {
                _twain.StartScanning(_settings);
            }
            catch (TwainException ex)
            {
                MessageBox.Show(ex.Message);
                Enabled = true;
            }
        }
Exemple #3
0
        private void scanButton_Click(object sender, RoutedEventArgs e)
        {
            IsEnabled = false;

            _settings = new ScanSettings
                {
                    UseDocumentFeeder = UseAdfCheckBox.IsChecked,
                    ShowTwainUI = UseUICheckBox.IsChecked ?? false,
                    ShowProgressIndicatorUI = ShowProgressCheckBox.IsChecked,
                    UseDuplex = UseDuplexCheckBox.IsChecked,
                    Resolution = (BlackAndWhiteCheckBox.IsChecked ?? false)
                                     ? ResolutionSettings.Fax
                                     : ResolutionSettings.ColourPhotocopier,
                    Area = !(GrabAreaCheckBox.IsChecked ?? false) ? null : AreaSettings,
                    ShouldTransferAllPages = true,
                    Rotation = new RotationSettings
                        {
                            AutomaticRotate = AutoRotateCheckBox.IsChecked ?? false,
                            AutomaticBorderDetection = AutoDetectBorderCheckBox.IsChecked ?? false
                        }
                };

            try
            {
                if (SourceUserSelected.IsChecked == true)
                    _twain.SelectSource(ManualSource.SelectedItem.ToString());
                _twain.StartScanning(_settings);
            }
            catch (TwainException ex)
            {
                MessageBox.Show(ex.Message);
            }

            IsEnabled = true;
        }
Exemple #4
0
 public bool NegotiateDuplex(ScanSettings scanSettings)
 {
     if (scanSettings.UseDuplex)
     {
         var cap = new Capability(Capabilities.Duplex, TwainType.Int16, _applicationId, SourceId);
         if ((Duplex)cap.GetBasicValue().Int16Value == Duplex.None)
         {
             Capability.SetCapability(Capabilities.DuplexEnabled, (short)Duplex.OnePass, _applicationId, SourceId);
             return(true);
         }
     }
     return(false);
 }
Exemple #5
0
        private bool NegotiateArea(ScanSettings scanSettings)
        {
            var area = scanSettings.Area;

            if (area == null)
            {
                return(false);
            }

            try
            {
                var cap = new Capability(Capabilities.IUnits, TwainType.UInt16, _applicationId, SourceId);
                if ((Units)cap.GetBasicValue().Int16Value != area.Units)
                {
                    Capability.SetBasicCapability(Capabilities.IUnits, (int)area.Units, TwainType.UInt16, _applicationId, SourceId);
                }
            }
            catch
            {
                Logger.WriteLog(LOG_LEVEL.LL_SUB_FUNC, "DataSource failed to set Units");
                // Do nothing if the data source does not support the requested capability
            }

            var imageLayout = new ImageLayout
            {
                Frame = new Frame
                {
                    Left   = new Fix32(area.Left),
                    Top    = new Fix32(area.Top),
                    Right  = new Fix32(area.Right),
                    Bottom = new Fix32(area.Bottom)
                }
            };

            var result = Twain32Native.DsImageLayout(
                _applicationId,
                SourceId,
                DataGroup.Image,
                DataArgumentType.ImageLayout,
                Message.Set,
                imageLayout);

            if (result != TwainResult.Success)
            {
                Logger.WriteLog(LOG_LEVEL.LL_SUB_FUNC, "DataSource failed to set ImageLayout");
                // note: 我這裡總是失敗,但是實際上image layout是可以設定成功的,由掃描結果不同來推測。
                //throw new TwainException("DsImageLayout.GetDefault error", result);
            }

            return(true);
        }
Exemple #6
0
 /// <summary>
 /// Negotiates the indicator.
 /// </summary>
 /// <param name="scanSettings">The scan settings.</param>
 public void NegotiateProgressIndicator(ScanSettings scanSettings)
 {
     try
     {
         if (scanSettings.ShowProgressIndicatorUI.HasValue)
         {
             Capability.SetCapability(Capabilities.Indicators, scanSettings.ShowProgressIndicatorUI.Value, _applicationId, SourceId);
         }
     }
     catch
     {
         // Do nothing if the data source does not support the requested capability
     }
 }
Exemple #7
0
 public void NegotiateDuplex(ScanSettings scanSettings)
 {
     try
     {
         if (scanSettings.UseDuplex.HasValue && SupportsDuplex)
         {
             Capability.SetCapability(Capabilities.DuplexEnabled, scanSettings.UseDuplex.Value, _applicationId, SourceId);
         }
     }
     catch
     {
         // Do nothing if the data source does not support the requested capability
     }
 }
Exemple #8
0
 /// <summary>
 /// Negotiates the automatic border detection capability.
 /// </summary>
 /// <param name="scanSettings">The scan settings.</param>
 public void NegotiateAutomaticBorderDetection(ScanSettings scanSettings)
 {
     try
     {
         if (scanSettings.Rotation.AutomaticBorderDetection)
         {
             Capability.SetCapability(Capabilities.Automaticborderdetection, true, _applicationId, SourceId);
         }
     }
     catch
     {
         // Do nothing if the data source does not support the requested capability
     }
 }
Exemple #9
0
        public void NegotiateFeeder(ScanSettings scanSettings)
        {
            if (scanSettings.UseDocumentFeeder)
            {
                // Enable the document feeder
                Capability.SetCapability(Capabilities.FeederEnabled, true, _applicationId, SourceId);

                if (!Capability.GetBoolCapability(Capabilities.FeederLoaded, _applicationId, SourceId))
                {
                    throw new FeederEmptyException();
                }

                Capability.SetCapability(Capabilities.AutoFeed, true, _applicationId, SourceId);
            }
        }
Exemple #10
0
 /// <summary>
 /// Negotiates the indicator.
 /// </summary>
 /// <param name="scanSettings">The scan settings.</param>
 public void NegotiateProgressIndicator(ScanSettings scanSettings)
 {
     try
     {
         if (scanSettings.ShowProgressIndicatorUI.HasValue)
         {
             Capability.SetCapability(Capabilities.Indicators, scanSettings.ShowProgressIndicatorUI.Value, _applicationId, SourceId);
         }
     }
     catch
     {
         Logger.WriteLog(LOG_LEVEL.LL_SUB_FUNC, "DataSource failed to set ProgressIndicator");
         // Do nothing if the data source does not support the requested capability
     }
 }
Exemple #11
0
 /// <summary>
 /// Negotiates the automatic border detection capability.
 /// </summary>
 /// <param name="scanSettings">The scan settings.</param>
 public void NegotiateAutomaticBorderDetection(ScanSettings scanSettings)
 {
     try
     {
         if (scanSettings.Rotation.AutomaticBorderDetection)
         {
             Capability.SetCapability(Capabilities.Automaticborderdetection, true, _applicationId, SourceId);
         }
     }
     catch
     {
         Logger.WriteLog(LOG_LEVEL.LL_SUB_FUNC, "DataSource failed to set Automaticborderdetection");
         // Do nothing if the data source does not support the requested capability
     }
 }
Exemple #12
0
 /// <summary>
 /// Negotiates the size of the page.
 /// </summary>
 /// <param name="scanSettings">The scan settings.</param>
 public void NegotiatePageSize(ScanSettings scanSettings)
 {
     try
     {
         var cap = new Capability(Capabilities.Supportedsizes, TwainType.Int16, _applicationId, SourceId);
         if ((PageType)cap.GetBasicValue().Int16Value != PageType.UsLetter)
         {
             Capability.SetBasicCapability(Capabilities.Supportedsizes, (ushort)scanSettings.Page.Size, TwainType.UInt16, _applicationId, SourceId);
         }
     }
     catch
     {
         // Do nothing if the data source does not support the requested capability
     }
 }
Exemple #13
0
 public void NegotiateDuplex(ScanSettings scanSettings)
 {
     try
     {
         if (scanSettings.UseDuplex.HasValue && SupportsDuplex)
         {
             Capability.SetCapability(Capabilities.DuplexEnabled, scanSettings.UseDuplex.Value, _applicationId, SourceId);
         }
     }
     catch
     {
         Logger.WriteLog(LOG_LEVEL.LL_SUB_FUNC, "DataSource failed to set UseDuplex");
         // Do nothing if the data source does not support the requested capability
     }
 }
Exemple #14
0
 public void NegotiateTransferCount(ScanSettings scanSettings)
 {
     try
     {
         scanSettings.TransferCount = Capability.SetCapability(
                 Capabilities.XferCount,
                 scanSettings.TransferCount,
                 _applicationId,
                 SourceId);
     }
     catch
     {
         // Do nothing if the data source does not support the requested capability
     }
 }
Exemple #15
0
 public void NegotiateFeeder(ScanSettings scanSettings)
 {
     if (scanSettings.UseDocumentFeeder.HasValue)
     {
         Capability.SetCapability(Capabilities.FeederEnabled, scanSettings.UseDocumentFeeder.Value, _applicationId, SourceId);
     }
     if (scanSettings.UseAutoFeeder.HasValue)
     {
         Capability.SetCapability(Capabilities.AutoFeed, scanSettings.UseAutoFeeder == true && scanSettings.UseDocumentFeeder == true, _applicationId, SourceId);
     }
     if (scanSettings.UseAutoScanCache.HasValue)
     {
         Capability.SetCapability(Capabilities.AutoScan, scanSettings.UseAutoScanCache.Value, _applicationId, SourceId);
     }
 }
Exemple #16
0
 public void NegotiateTransferCount(ScanSettings scanSettings)
 {
     try
     {
         scanSettings.TransferCount = Capability.SetCapability(
             Capabilities.XferCount,
             scanSettings.TransferCount,
             _applicationId,
             SourceId);
     }
     catch
     {
         // Do nothing if the data source does not support the requested capability
     }
 }
Exemple #17
0
        private bool NegotiateArea(ScanSettings scanSettings)
        {
            var area = scanSettings.Area;

            if (area == null)
            {
                return(false);
            }

            try
            {
                var cap = new Capability(Capabilities.IUnits, TwainType.Int16, _applicationId, SourceId);
                if ((Units)cap.GetBasicValue().Int16Value != area.Units)
                {
                    Capability.SetCapability(Capabilities.IUnits, (short)area.Units, _applicationId, SourceId);
                }
            }
            catch
            {
                // Do nothing if the data source does not support the requested capability
            }

            var imageLayout = new ImageLayout
            {
                Frame = new Frame
                {
                    Left   = new Fix32(area.Left),
                    Top    = new Fix32(area.Top),
                    Right  = new Fix32(area.Right),
                    Bottom = new Fix32(area.Bottom)
                }
            };

            var result = Twain32Native.DsImageLayout(
                _applicationId,
                SourceId,
                DataGroup.Image,
                DataArgumentType.ImageLayout,
                Message.Set,
                imageLayout);

            if (result != TwainResult.Success)
            {
                throw new TwainException("DsImageLayout.GetDefault error", result);
            }

            return(true);
        }
Exemple #18
0
 /// <summary>
 /// Negotiates the size of the page.
 /// </summary>
 /// <param name="scanSettings">The scan settings.</param>
 public void NegotiatePageSize(ScanSettings scanSettings)
 {
     try
     {
         var cap = new Capability(Capabilities.Supportedsizes, TwainType.Int16, _applicationId, SourceId);
         if ((PageType)cap.GetBasicValue().Int16Value != PageType.UsLetter)
         {
             Capability.SetBasicCapability(Capabilities.Supportedsizes, (ushort)scanSettings.Page.Size, TwainType.UInt16, _applicationId, SourceId);
         }
     }
     catch
     {
         Logger.WriteLog(LOG_LEVEL.LL_SUB_FUNC, "DataSource failed to set Page.Size");
         // Do nothing if the data source does not support the requested capability
     }
 }
Exemple #19
0
        public bool Open(ScanSettings settings)
        {
            OpenSource();

            NegotiateTransferCount(settings);
            NegotiateFeeder(settings);
            NegotiateDuplex(settings);

            if (settings.Resolution != null)
            {
                NegotiateColour(settings);
                NegotiateResolution(settings);
            }

            return(Enable(settings));
        }
Exemple #20
0
        public PixelType GetPixelType(ScanSettings scanSettings)
        {
            switch (scanSettings.Resolution.ColourSetting)
            {
            case ColourSetting.BlackAndWhite:
                return(PixelType.BlackAndWhite);

            case ColourSetting.GreyScale:
                return(PixelType.Grey);

            case ColourSetting.Colour:
                return(PixelType.Rgb);
            }

            throw new NotImplementedException();
        }
Exemple #21
0
 public void NegotiateOrientation(ScanSettings scanSettings)
 {
     // Set orientation (default is portrait)
     try
     {
         var cap = new Capability(Capabilities.Orientation, TwainType.Int16, _applicationId, SourceId);
         if ((Orientation)cap.GetBasicValue().Int16Value != Orientation.Default)
         {
             Capability.SetBasicCapability(Capabilities.Orientation, (ushort)scanSettings.Page.Orientation, TwainType.UInt16, _applicationId, SourceId);
         }
     }
     catch
     {
         // Do nothing if the data source does not support the requested capability
     }
 }
Exemple #22
0
 public void NegotiateResolution(ScanSettings scanSettings)
 {
     try
     {
         if (scanSettings.Resolution.Dpi.HasValue)
         {
             int dpi = scanSettings.Resolution.Dpi.Value;
             Capability.SetBasicCapability(Capabilities.XResolution, dpi, TwainType.Fix32, _applicationId, SourceId);
             Capability.SetBasicCapability(Capabilities.YResolution, dpi, TwainType.Fix32, _applicationId, SourceId);
         }
     }
     catch
     {
         // Do nothing if the data source does not support the requested capability
     }
 }
Exemple #23
0
        public short GetBitDepth(ScanSettings scanSettings)
        {
            switch (scanSettings.Resolution.ColourSetting)
            {
            case ColourSetting.BlackAndWhite:
                return(1);

            case ColourSetting.GreyScale:
                return(8);

            case ColourSetting.Colour:
                return(16);
            }

            throw new NotImplementedException();
        }
Exemple #24
0
 public void NegotiateTransferCount(ScanSettings scanSettings)
 {
     try
     {
         scanSettings.TransferCount = Capability.SetCapability(
             Capabilities.XferCount,
             scanSettings.TransferCount,
             _applicationId,
             SourceId);
     }
     catch
     {
         Logger.WriteLog(LOG_LEVEL.LL_SUB_FUNC, "DataSource failed to set TransferCount");
         // Do nothing if the data source does not support the requested capability
     }
 }
Exemple #25
0
 public void NegotiateResolution(ScanSettings scanSettings)
 {
     try
     {
         if (scanSettings.Resolution.Dpi.HasValue)
         {
             int dpi = scanSettings.Resolution.Dpi.Value;
             Capability.SetBasicCapability(Capabilities.XResolution, dpi, TwainType.Fix32, _applicationId, SourceId);
             Capability.SetBasicCapability(Capabilities.YResolution, dpi, TwainType.Fix32, _applicationId, SourceId);
         }
     }
     catch
     {
         Logger.WriteLog(LOG_LEVEL.LL_SUB_FUNC, "DataSource failed to set Resolution");
         // Do nothing if the data source does not support the requested capability
     }
 }
Exemple #26
0
        public bool Open(ScanSettings settings)
        {
            OpenSource();
            CheckDeviceOnline();

            if (settings.AbortWhenNoPaperDetectable && !PaperDetectable)
            {
                throw new FeederEmptyException();
            }

            // Set whether or not to show progress window
            NegotiateProgressIndicator(settings);
            NegotiateTransferCount(settings);
            NegotiateFeeder(settings);
            NegotiateDuplex(settings);

            if (settings.UseDocumentFeeder == true &&
                settings.Page != null)
            {
                NegotiatePageSize(settings);
                NegotiateOrientation(settings);
            }

            if (settings.Area != null)
            {
                NegotiateArea(settings);
            }

            if (settings.Resolution != null)
            {
                NegotiateColour(settings);
                NegotiateResolution(settings);
            }

            // Configure automatic rotation and image border detection
            if (settings.Rotation != null)
            {
                NegotiateAutomaticRotate(settings);
                NegotiateAutomaticBorderDetection(settings);
            }

            return(Enable(settings));
        }
Exemple #27
0
        public void NegotiateFeeder(ScanSettings scanSettings)
        {
            try
            {
                if (scanSettings.UseDocumentFeeder.HasValue)
                {
                    Capability.SetCapability(Capabilities.FeederEnabled, scanSettings.UseDocumentFeeder.Value, _applicationId, SourceId);
                }
            }
            catch
            {
                Logger.WriteLog(LOG_LEVEL.LL_SUB_FUNC, "DataSource failed to set UseDocumentFeeder");
                // Do nothing if the data source does not support the requested capability
            }

            try
            {
                if (scanSettings.UseAutoFeeder.HasValue)
                {
                    Capability.SetCapability(Capabilities.AutoFeed, scanSettings.UseAutoFeeder == true && scanSettings.UseDocumentFeeder == true, _applicationId, SourceId);
                }
            }
            catch
            {
                Logger.WriteLog(LOG_LEVEL.LL_SUB_FUNC, "DataSource failed to set UseAutoFeeder");
                // Do nothing if the data source does not support the requested capability
            }

            try
            {
                if (scanSettings.UseAutoScanCache.HasValue)
                {
                    Capability.SetCapability(Capabilities.AutoScan, scanSettings.UseAutoScanCache.Value, _applicationId, SourceId);
                }
            }
            catch
            {
                Logger.WriteLog(LOG_LEVEL.LL_SUB_FUNC, "DataSource failed to set UseAutoScanCache");
                // Do nothing if the data source does not support the requested capability
            }
        }
Exemple #28
0
        public void NegotiateFeeder(ScanSettings scanSettings)
        {

            try
            {
                if (scanSettings.UseDocumentFeeder.HasValue)
                {
                    Capability.SetCapability(Capabilities.FeederEnabled, scanSettings.UseDocumentFeeder.Value, _applicationId, SourceId);
                }
            }
            catch
            {
                // Do nothing if the data source does not support the requested capability
            }

            try
            {
                if (scanSettings.UseAutoFeeder.HasValue)
                {
                    Capability.SetCapability(Capabilities.AutoFeed, scanSettings.UseAutoFeeder == true && scanSettings.UseDocumentFeeder == true, _applicationId, SourceId);
                }
            }
            catch
            {
                // Do nothing if the data source does not support the requested capability
            }

            try
            {
                if (scanSettings.UseAutoScanCache.HasValue)
                {
                    Capability.SetCapability(Capabilities.AutoScan, scanSettings.UseAutoScanCache.Value, _applicationId, SourceId);
                }
            }
            catch
            {
                // Do nothing if the data source does not support the requested capability
            }

        }
Exemple #29
0
        public void NegotiateFeeder(ScanSettings scanSettings)
        {
            try
            {
                if (scanSettings.UseDocumentFeeder.HasValue)
                {
                    Capability.SetCapability(Capabilities.FeederEnabled, scanSettings.UseDocumentFeeder.Value, _applicationId, SourceId);
                }
            }
            catch
            {
                // Do nothing if the data source does not support the requested capability
            }

            try
            {
                if (scanSettings.UseAutoFeeder.HasValue)
                {
                    Capability.SetCapability(Capabilities.AutoFeed, scanSettings.UseAutoFeeder == true && scanSettings.UseDocumentFeeder == true, _applicationId, SourceId);
                }
            }
            catch
            {
                // Do nothing if the data source does not support the requested capability
            }

            try
            {
                if (scanSettings.UseAutoScanCache.HasValue)
                {
                    Capability.SetCapability(Capabilities.AutoScan, scanSettings.UseAutoScanCache.Value, _applicationId, SourceId);
                }
            }
            catch
            {
                // Do nothing if the data source does not support the requested capability
            }
        }
Exemple #30
0
        public bool Enable(ScanSettings settings)
        {
            UserInterface ui = new UserInterface();

            ui.ShowUI     = (short)(settings.ShowTwainUI ? 1 : 0);
            ui.ModalUI    = 1;
            ui.ParentHand = _messageHook.WindowHandle;

            var result = Twain32Native.DsUserInterface(
                _applicationId,
                SourceId,
                DataGroup.Control,
                DataArgumentType.UserInterface,
                Message.EnableDS,
                ui);

            if (result != TwainResult.Success)
            {
                Dispose();
                return(false);
            }
            return(true);
        }
        /// <summary>
        /// 做open→設定各種CAPABILITY→EnableDS→等回傳圖→end
        /// </summary>
        /// <param name="settings"></param>
        public void StartScan(ScanSettings settings)
        {
            bool scanning = false;

            try
            {
                _messageHook.UseFilter = true;
                scanning = DataSource.Open(settings, UseIncrementalMemoryXfer);
            }
            catch (TwainException e)
            {
                DataSource.Close();
                EndingScan();
                throw e;
            }
            finally
            {
                // Remove the message hook if scan setup failed
                if (!scanning)
                {
                    EndingScan();
                }
            }
        }
        public void StartScan(ScanSettings settings)
        {
            bool scanning = false;

            try
            {
                _messageHook.UseFilter = true;
                scanning = DataSource.Open(settings);
            }
            catch (TwainException e)
            {
                DataSource.Close();
                EndingScan();
                throw e;
            }
            finally
            {
                // Remove the message hook if scan setup failed
                if (!scanning)
                {
                    EndingScan();
                }
            }
        }
Exemple #33
0
        public void NegotiateColour(ScanSettings scanSettings)
        {
            try
            {
                Capability.SetBasicCapability(Capabilities.IPixelType, (ushort)GetPixelType(scanSettings), TwainType.UInt16, _applicationId, SourceId);
            }
            catch
            {
                // Do nothing if the data source does not support the requested capability
            }

            // TODO: Also set this for colour scanning
            try
            {
                if (scanSettings.Resolution.ColourSetting != ColourSetting.Colour)
                {
                    Capability.SetCapability(Capabilities.BitDepth, GetBitDepth(scanSettings), _applicationId, SourceId);
                }
            }
            catch
            {
                // Do nothing if the data source does not support the requested capability
            }
        }
Exemple #34
0
 public void NegotiateResolution(ScanSettings scanSettings)
 {
     if (scanSettings.Resolution.Dpi.HasValue)
     {
         int dpi = scanSettings.Resolution.Dpi.Value;
         Capability.SetBasicCapability(Capabilities.XResolution, dpi, TwainType.Fix32, _applicationId, SourceId);
         try
         {
             Capability.SetBasicCapability(Capabilities.YResolution, dpi, TwainType.Fix32, _applicationId, SourceId);
         }
         catch (Exception)
         {
             // some scanners do not support YResolution change
         }
     }
 }
Exemple #35
0
 /// <summary>
 /// Negotiates the automatic rotation capability.
 /// </summary>
 /// <param name="scanSettings">The scan settings.</param>
 public void NegotiateAutomaticRotate(ScanSettings scanSettings)
 {
     RotationSettings rotationSettings = scanSettings.Rotation;
     Capability.SetCapability(Capabilities.Automaticrotate, rotationSettings.AutomaticRotate, _applicationId, SourceId);
 }
        public void NegotiateColour(ScanSettings scanSettings)
        {
            Capability.SetBasicCapability(Capabilities.IPixelType, (ushort)GetPixelType(scanSettings), TwainType.UInt16, _applicationId, SourceId);

            // TODO: Also set this for colour scanning
            if (scanSettings.Resolution.ColourSetting != ColourSetting.Colour)
            {
                Capability.SetCapability(Capabilities.BitDepth, GetBitDepth(scanSettings), _applicationId, SourceId);
            }
        }
Exemple #37
0
        private ScanSettings ParseScanSettings(JObject request)
        {
            log.Debug("create default scan settings");
            ScanSettings settings = new ScanSettings();
            settings.UseDocumentFeeder = false;
            settings.ShowTwainUI = true;
            settings.ShowProgressIndicatorUI = true;
            settings.UseDuplex = false;
            settings.Resolution = ResolutionSettings.ColourPhotocopier;
            settings.Area = null;
            settings.ShouldTransferAllPages = true;
            settings.Rotation = new RotationSettings()
            {
                AutomaticRotate = false,
                AutomaticBorderDetection = false
            };

            if (request["settings"] != null)
            {
                log.Debug("parse request scan settings");
                if (request["settings"]["UseDocumentFeeder"] != null) settings.UseDocumentFeeder = (bool)request["settings"]["UseDocumentFeeder"];
                if (request["settings"]["ShowTwainUI"] != null) settings.ShowTwainUI = (bool)request["settings"]["ShowTwainUI"];
                if (request["settings"]["ShowProgressIndicatorUI"] != null) settings.ShowProgressIndicatorUI = (bool)request["settings"]["ShowProgressIndicatorUI"];
                if (request["settings"]["UseDuplex"] != null) settings.UseDuplex = (bool)request["settings"]["UseDuplex"];
                if (request["settings"]["ShouldTransferAllPages"] != null) settings.ShouldTransferAllPages = (bool)request["settings"]["ShouldTransferAllPages"];

                if (request["settings"]["Resolution"] != null)
                {
                    if ((string)request["settings"]["Resolution"] == "Fax") settings.Resolution = ResolutionSettings.Fax;
                    else if ((string)request["settings"]["Resolution"] == "Photocopier") settings.Resolution = ResolutionSettings.Photocopier;
                    else if ((string)request["settings"]["Resolution"] == "ColourPhotocopier") settings.Resolution = ResolutionSettings.ColourPhotocopier;
                    else
                    {
                        settings.Resolution = new ResolutionSettings();
                        if (request["settings"]["Resolution"]["Dpi"] != null) settings.Resolution.Dpi = (int)request["settings"]["Resolution"]["Dpi"];
                        if (request["settings"]["Resolution"]["ColourSetting"] != null)
                        {
                            if ((string)request["settings"]["Resolution"]["ColourSetting"] == "BlackAndWhite") settings.Resolution.ColourSetting = ColourSetting.BlackAndWhite;
                            else if ((string)request["settings"]["Resolution"]["ColourSetting"] == "Colour") settings.Resolution.ColourSetting = ColourSetting.Colour;
                            else if ((string)request["settings"]["Resolution"]["ColourSetting"] == "GreyScale") settings.Resolution.ColourSetting = ColourSetting.GreyScale;
                            else throw new ScanSettingsException("Invalid Resolution.ColourSetting setting.");
                        }
                    }
                }

                if (request["settings"]["Area"] != null)
                {
                    if (request["settings"]["Area"]["units"] == null) throw new ScanSettingsException("Units setting is required in Area.");
                    if (request["settings"]["Area"]["top"] == null) throw new ScanSettingsException("Top setting is required in Area.");
                    if (request["settings"]["Area"]["left"] == null) throw new ScanSettingsException("Left setting is required in Area.");
                    if (request["settings"]["Area"]["bottom"] == null) throw new ScanSettingsException("Bottom setting is required in Area.");
                    if (request["settings"]["Area"]["right"] == null) throw new ScanSettingsException("Right setting is required in Area.");

                    Units units;
                    if ((string)request["settings"]["Area"]["units"] == "Centimeters") units = Units.Centimeters;
                    else if ((string)request["settings"]["Area"]["units"] == "Inches") units = Units.Inches;
                    else if ((string)request["settings"]["Area"]["units"] == "Millimeters") units = Units.Millimeters;
                    else if ((string)request["settings"]["Area"]["units"] == "Picas") units = Units.Picas;
                    else if ((string)request["settings"]["Area"]["units"] == "Pixels") units = Units.Pixels;
                    else if ((string)request["settings"]["Area"]["units"] == "Points") units = Units.Points;
                    else if ((string)request["settings"]["Area"]["units"] == "Twips") units = Units.Twips;
                    else throw new ScanSettingsException("Invalid Area.units setting.");

                    float top, left, bottom, right;
                    top = (float)request["settings"]["Area"]["top"];
                    left = (float)request["settings"]["Area"]["left"];
                    bottom = (float)request["settings"]["Area"]["bottom"];
                    right = (float)request["settings"]["Area"]["right"];

                    settings.Area = new AreaSettings(units, top, left, bottom, right);
                }

                if (request["settings"]["Rotation"] != null)
                {
                    settings.Rotation = new RotationSettings();
                    if (request["settings"]["Rotation"]["AutomaticBorderDetection"] != null) settings.Rotation.AutomaticBorderDetection = (bool)request["settings"]["Rotation"]["AutomaticBorderDetection"];
                    if (request["settings"]["Rotation"]["AutomaticDeskew"] != null) settings.Rotation.AutomaticDeskew = (bool)request["settings"]["Rotation"]["AutomaticDeskew"];
                    if (request["settings"]["Rotation"]["AutomaticRotate"] != null) settings.Rotation.AutomaticRotate = (bool)request["settings"]["Rotation"]["AutomaticRotate"];
                    if (request["settings"]["Rotation"]["FlipSideRotation"] != null)
                    {
                        if ((string)request["settings"]["Rotation"]["FlipSideRotation"] == "Book") settings.Rotation.FlipSideRotation = FlipRotation.Book;
                        else if ((string)request["settings"]["Rotation"]["FlipSideRotation"] == "FanFold") settings.Rotation.FlipSideRotation = FlipRotation.FanFold;
                        else throw new ScanSettingsException("Invalid Rotation.FlipSideRotation setting.");
                    }
                }
            }

            return settings;
        }
 public void NegotiateResolution(ScanSettings scanSettings)
 {
     if (scanSettings.Resolution.Dpi.HasValue)
     {
         int dpi = scanSettings.Resolution.Dpi.Value;
         Capability.SetBasicCapability(Capabilities.XResolution, dpi, TwainType.Fix32, _applicationId, SourceId);
         Capability.SetBasicCapability(Capabilities.YResolution, dpi, TwainType.Fix32, _applicationId, SourceId);
     }
 }
 /// <summary>
 /// Negotiates the size of the page.
 /// </summary>
 /// <param name="scanSettings">The scan settings.</param>
 public void NegotiatePageSize(ScanSettings scanSettings)
 {
     var cap = new Capability(Capabilities.Supportedsizes, TwainType.Int16, _applicationId, SourceId);
     if ((PageType)cap.GetBasicValue().Int16Value != PageType.UsLetter)
     {
         Capability.SetBasicCapability(Capabilities.Supportedsizes, (ushort)scanSettings.Page.Size, TwainType.UInt16, _applicationId, SourceId);
     }
 }
Exemple #40
0
        private void ScanTwainFixedSize()
        {
            AreaSettings AreaSettings = new AreaSettings(Units.Centimeters, 0f, 0f, 29.7f, 21.01f);
            ScanSettings _settings = new ScanSettings();
            ResolutionSettings rs = new ResolutionSettings();
            rs.ColourSetting = ColourSetting.GreyScale;
            rs.Dpi = 100;
            _settings.Resolution = rs;
            _settings.ShowTwainUI = false;
            _settings.ShowProgressIndicatorUI = true;
            _settings.Area = AreaSettings;

                _twain.StartScanning(_settings);
        }
Exemple #41
0
        private static ScanSettings CreateDefaultSettings(int dpi, ColourSetting colour, bool adf)
        {
            ScanSettings scanSettings = new ScanSettings
            {
                ShowTwainUI = false,
                Resolution = new ResolutionSettings(),
                AbortWhenNoPaperDetectable = adf,
                ShouldTransferAllPages = adf,
                UseAutoFeeder = adf,
                UseDocumentFeeder = adf
            };
            scanSettings.Resolution.ColourSetting = colour;
            scanSettings.Resolution.Dpi = dpi;

            return scanSettings;
        }
Exemple #42
0
 /// <summary>
 /// Starts scanning.
 /// </summary>
 public void StartScanning(ScanSettings settings)
 {
     _dataSourceManager.StartScan(settings);
 }
 /// <summary>
 /// Negotiates the automatic rotation capability.
 /// </summary>
 /// <param name="scanSettings">The scan settings.</param>
 public void NegotiateAutomaticRotate(ScanSettings scanSettings)
 {
     if (scanSettings.Rotation.AutomaticRotate)
     {
         Capability.SetCapability(Capabilities.Automaticrotate, true, _applicationId, SourceId);
     }
 }
        private bool NegotiateArea(ScanSettings scanSettings)
        {
            var area = scanSettings.Area;

            if (area == null)
            {
                return false;
            }

            var cap = new Capability(Capabilities.IUnits, TwainType.Int16, _applicationId, SourceId);
            if ((Units)cap.GetBasicValue().Int16Value != area.Units)
            {
                Capability.SetCapability(Capabilities.IUnits, (short)area.Units, _applicationId, SourceId);
            }

            var imageLayout = new ImageLayout
            {
                Frame = new Frame
                {
                    Left = new Fix32(area.Left),
                    Top = new Fix32(area.Top),
                    Right = new Fix32(area.Right),
                    Bottom = new Fix32(area.Bottom)
                }
            };

            var result = Twain32Native.DsImageLayout(
                _applicationId,
                SourceId,
                DataGroup.Image,
                DataArgumentType.ImageLayout,
                Message.Set,
                imageLayout);

            if (result != TwainResult.Success)
            {
                throw new TwainException("DsImageLayout.GetDefault error", result);
            }

            return true;
        }
        public bool Open(ScanSettings settings)
        {
            OpenSource();

            if (settings.AbortWhenNoPaperDetectable && !PaperDetectable)
                throw new FeederEmptyException();

            // Set whether or not to show progress window
            NegotiateProgressIndicator(settings);
            NegotiateTransferCount(settings);
            NegotiateFeeder(settings);
            NegotiateDuplex(settings);

            if (settings.UseDocumentFeeder == true &&
                settings.Page != null)
            {
                NegotiatePageSize(settings);
                NegotiateOrientation(settings);
            }

            if (settings.Resolution != null)
            {
                NegotiateColour(settings);
                NegotiateResolution(settings);
            }

            if (settings.Area != null)
            {
                NegotiateArea(settings);
            }

            // Configure automatic rotation and image border detection
            if (settings.Rotation != null)
            {
                NegotiateAutomaticRotate(settings);
                NegotiateAutomaticBorderDetection(settings);
            }

            return Enable(settings);
        }
 public void NegotiateTransferCount(ScanSettings scanSettings)
 {
     scanSettings.TransferCount = Capability.SetCapability(
         Capabilities.XferCount,
         scanSettings.TransferCount,
         _applicationId,
         SourceId);
 }
Exemple #47
0
 /// <summary>
 /// Negotiates the automatic border detection capability.
 /// </summary>
 /// <param name="scanSettings">The scan settings.</param>
 public void NegotiateAutomaticBorderDetection(ScanSettings scanSettings)
 {
     RotationSettings rotationSettings = scanSettings.Rotation;
     Capability.SetCapability(Capabilities.Automaticborderdetection, rotationSettings.AutomaticBorderDetection, _applicationId, SourceId);
 }
Exemple #48
0
 /// <summary>
 /// Negotiates the automatic border detection capability.
 /// </summary>
 /// <param name="scanSettings">The scan settings.</param>
 public void NegotiateAutomaticBorderDetection(ScanSettings scanSettings)
 {
     try
     {
         if (scanSettings.Rotation.AutomaticBorderDetection)
         {
             Capability.SetCapability(Capabilities.Automaticborderdetection, true, _applicationId, SourceId);
         }
     }
     catch
     {
         // Do nothing if the data source does not support the requested capability
     }
 }
 /// <summary>
 /// Negotiates the automatic border detection capability.
 /// </summary>
 /// <param name="scanSettings">The scan settings.</param>
 public void NegotiateAutomaticBorderDetection(ScanSettings scanSettings)
 {
     if (scanSettings.Rotation.AutomaticBorderDetection)
     {
         Capability.SetCapability(Capabilities.Automaticborderdetection, true, _applicationId, SourceId);
     }
 }
 /// <summary>
 /// Negotiates the indicator.
 /// </summary>
 /// <param name="scanSettings">The scan settings.</param>
 public void NegotiateProgressIndicator(ScanSettings scanSettings)
 {
     if (scanSettings.ShowProgressIndicatorUI.HasValue)
     {
         Capability.SetCapability(Capabilities.Indicators, scanSettings.ShowProgressIndicatorUI.Value, _applicationId, SourceId);
     }
 }
Exemple #51
0
        private void ScanTwainA4()
        {
            ScanSettings _settings = new ScanSettings();
            ResolutionSettings rs = new ResolutionSettings();
            rs.ColourSetting = ColourSetting.GreyScale;
            rs.Dpi = 100;
            _settings.Resolution = rs;
            _settings.ShowTwainUI = false;
            _settings.ShowProgressIndicatorUI = true;
            PageSettings ps = new PageSettings();
            ps.Orientation = TwainDotNet.TwainNative.Orientation.Portrait;
            ps.Size = PageType.MaxSize;

            _settings.Page = ps;

                _twain.StartScanning(_settings);
        }
        public short GetBitDepth(ScanSettings scanSettings)
        {
            switch (scanSettings.Resolution.ColourSetting)
            {
                case ColourSetting.BlackAndWhite:
                    return 1;

                case ColourSetting.GreyScale:
                    return 8;

                case ColourSetting.Colour:
                    return 16;
            }

            throw new NotImplementedException();
        }
Exemple #53
0
 /// <summary>
 /// Negotiates the indicator.
 /// </summary>
 /// <param name="scanSettings">The scan settings.</param>
 public void NegotiateProgressIndicator(ScanSettings scanSettings)
 {
     try
     {
         if (scanSettings.ShowProgressIndicatorUI.HasValue)
         {
             Capability.SetCapability(Capabilities.Indicators, scanSettings.ShowProgressIndicatorUI.Value, _applicationId, SourceId);
         }
     }
     catch
     {
         // Do nothing if the data source does not support the requested capability
     }
 }
        public bool Enable(ScanSettings settings)
        {
            UserInterface ui = new UserInterface();
            ui.ShowUI = (short)(settings.ShowTwainUI ? 1 : 0);
            ui.ModalUI = 1;
            ui.ParentHand = _messageHook.WindowHandle;

            var result = Twain32Native.DsUserInterface(
                _applicationId,
                SourceId,
                DataGroup.Control,
                DataArgumentType.UserInterface,
                Message.EnableDS,
                ui);

            if (result != TwainResult.Success)
            {
                Dispose();
                return false;
            }
            return true;
        }
Exemple #55
0
        private bool NegotiateArea(ScanSettings scanSettings)
        {
            var area = scanSettings.Area;

            if (area == null)
            {
                return(false);
            }

            try
            {
                var cap = new Capability(Capabilities.IUnits, TwainType.Int16, _applicationId, SourceId);
                if ((Units)cap.GetBasicValue().Int16Value != area.Units)
                {
                    Capability.SetCapability(Capabilities.IUnits, (short)area.Units, _applicationId, SourceId);
                }
            }
            catch
            {
                // Do nothing if the data source does not support the requested capability
                return(false);
            }

            float right;
            float bottom;

            float physicalHeight    = 0;
            var   physicalHeightCap = Capability.GetCapability(Capabilities.PhysicalHeight, _applicationId, SourceId);

            if (physicalHeightCap != null && physicalHeightCap.Count == 1)
            {
                physicalHeight = ValueConverter.ConvertToFix32(physicalHeightCap[0]);
            }

            float physicalWidth    = 0;
            var   physicalWidthCap = Capability.GetCapability(Capabilities.PhysicalWidth, _applicationId, SourceId);

            if (physicalWidthCap != null && physicalWidthCap.Count == 1)
            {
                physicalWidth = ValueConverter.ConvertToFix32(physicalWidthCap[0]);
            }

            right  = physicalWidth < area.Right ? physicalWidth : area.Right;
            bottom = physicalHeight < area.Bottom ? physicalHeight : area.Bottom;

            var imageLayout = new ImageLayout
            {
                Frame = new Frame
                {
                    Left   = new Fix32(area.Left),
                    Top    = new Fix32(area.Top),
                    Right  = new Fix32(right),
                    Bottom = new Fix32(bottom)
                }
            };



            var result = Twain32Native.DsImageLayout(
                _applicationId,
                SourceId,
                DataGroup.Image,
                DataArgumentType.ImageLayout,
                Message.Set,
                imageLayout);

            if (result != TwainResult.Success && result != TwainResult.CheckStatus)
            {
                var condition = DataSourceManager.GetConditionCode(_applicationId, SourceId);
                return(false);
                //throw new TwainException("DsImageLayout.GetDefault error", result, condition);
            }

            return(true);
        }
Exemple #56
0
 /// <summary>
 /// Starts scanning.
 /// </summary>
 public void StartScanning(ScanSettings settings)
 {
     _dataSourceManager.StartScan(settings);
 }
 public void NegotiateFeeder(ScanSettings scanSettings)
 {
     if (scanSettings.UseDocumentFeeder.HasValue)
     {
         Capability.SetCapability(Capabilities.FeederEnabled, scanSettings.UseDocumentFeeder.Value, _applicationId, SourceId);
     }
     if (scanSettings.UseAutoFeeder.HasValue)
     {
         Capability.SetCapability(Capabilities.AutoFeed, scanSettings.UseAutoFeeder == true && scanSettings.UseDocumentFeeder == true, _applicationId, SourceId);
     }
     if (scanSettings.UseAutoScanCache.HasValue)
     {
         Capability.SetCapability(Capabilities.AutoScan, scanSettings.UseAutoScanCache.Value, _applicationId, SourceId);
     }
 }
 public void NegotiateDuplex(ScanSettings scanSettings)
 {
     if (scanSettings.UseDuplex.HasValue && SupportsDuplex)
     {
         Capability.SetCapability(Capabilities.DuplexEnabled, scanSettings.UseDuplex.Value, _applicationId, SourceId);
     }
 }
Exemple #59
0
 /// <summary>
 /// Starts scanning.
 /// </summary>
 public bool StartScanning(ScanSettings settings, object extTag)
 {
     return(_dataSourceManager.StartScan(settings, extTag));
 }
 public void NegotiateOrientation(ScanSettings scanSettings)
 {
     // Set orientation (default is portrait)
     var cap = new Capability(Capabilities.Orientation, TwainType.Int16, _applicationId, SourceId);
     if ((Orientation)cap.GetBasicValue().Int16Value != Orientation.Default)
     {
         Capability.SetBasicCapability(Capabilities.Orientation, (ushort)scanSettings.Page.Orientation, TwainType.UInt16, _applicationId, SourceId);
     }
 }