Beispiel #1
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);
        }
Beispiel #2
0
        public SourceSettings GetAwailableSourceSettings()
        {
            var pixelTypes    = new List <ushort>();
            var pixelTypesCap = Capability.GetCapability(Capabilities.IPixelType, _applicationId, SourceId);

            if (pixelTypesCap != null)
            {
                foreach (var pt in pixelTypesCap)
                {
                    pixelTypes.Add((ushort)pt);
                }
            }
            _log.Debug("Get pixel types: success, count: " + pixelTypes.Count);

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

            if (physicalHeightCap != null && physicalHeightCap.Count == 1)
            {
                physicalHeight = ValueConverter.ConvertToFix32(physicalHeightCap[0]);
                _log.Debug("Get physical height: success, value: " + physicalHeight);
            }

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

            if (physicalWidthCap != null && physicalWidthCap.Count == 1)
            {
                physicalWidth = ValueConverter.ConvertToFix32(physicalWidthCap[0]);
                _log.Debug("Get physical width: success, value: " + physicalWidth);
            }

            bool hasADF, hasFlatbed;
            var  flatbedResolutions = new List <float>();
            var  feederResolutions  = new List <float>();

            try
            {
                var documentFeederEnabled = Capability.GetBoolCapability(Capabilities.FeederEnabled, _applicationId, SourceId);
                if (documentFeederEnabled)
                {
                    feederResolutions = GetResolutions();
                    Capability.SetCapability(Capabilities.FeederEnabled, false, _applicationId, SourceId);
                    var newDocumentFeederEnabled = Capability.GetBoolCapability(Capabilities.FeederEnabled, _applicationId, SourceId);

                    hasADF     = true;
                    hasFlatbed = !newDocumentFeederEnabled;

                    _log.Debug(string.Format("Get document feeder enabled: success, hasFlatbed: {0}, hasADF: {1}, feederEnabledInitial: {2}", hasFlatbed, hasADF, documentFeederEnabled));

                    if (hasFlatbed)
                    {
                        flatbedResolutions = GetResolutions();
                    }
                }
                else
                {
                    flatbedResolutions = GetResolutions();
                    Capability.SetCapability(Capabilities.FeederEnabled, true, _applicationId, SourceId);
                    var newDocumentFeederEnabled = Capability.GetBoolCapability(Capabilities.FeederEnabled, _applicationId, SourceId);

                    hasADF     = newDocumentFeederEnabled;
                    hasFlatbed = true;
                    _log.Debug(string.Format("Get document feeder enabled: success, hasFlatbed: {0}, hasADF: {1}, feederEnabledInitial: {2}", hasFlatbed, hasADF, documentFeederEnabled));


                    if (hasADF)
                    {
                        feederResolutions = GetResolutions();
                    }
                }
            }
            catch (Exception)
            {
                hasADF     = false;
                hasFlatbed = true;

                _log.Debug(string.Format("Get document feeder enabled: not supported, hasFlatbed: {0}, hasADF: {1}",
                                         hasFlatbed, hasADF));
                flatbedResolutions = GetResolutions();
            }


            var hasDuplex = false;

            try
            {
                var duplexCap = Capability.GetCapability(Capabilities.Duplex, _applicationId, SourceId);
                if (duplexCap == null)
                {
                    hasDuplex = false;
                }
                else
                {
                    foreach (var value in duplexCap)
                    {
                        if ((Duplex)value == Duplex.None)
                        {
                            hasDuplex = false;
                            break;
                        }
                        if ((Duplex)value == Duplex.OnePass || (Duplex)value == Duplex.TwoPass)
                        {
                            hasDuplex = true;
                        }
                    }
                }
                _log.Debug(string.Format("Has duplex: success, value: {0}", hasDuplex));
            }
            catch (Exception)
            {
                _log.Debug(string.Format("Has duplex: not supported, value: false"));
                hasDuplex = false;
            }
            _log.Debug("GetCapabilities, result: Success");

            return(new SourceSettings(flatbedResolutions, feederResolutions, pixelTypes, physicalHeight, physicalWidth, hasADF, hasFlatbed, hasDuplex));
        }