using NTwain.Data; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace NTwain { // this contains all cap-related methods prefixed with Cap partial class DataSource : ICapControl { #region low-level cap stuff /// /// Gets the actual supported operations for a capability. /// /// The cap identifier. /// public QuerySupports CapQuerySupport(CapabilityId capId) { QuerySupports retVal = QuerySupports.None; using (TWCapability cap = new TWCapability(capId)) { var rc = _session.DGControl.Capability.QuerySupport(cap); if (rc == ReturnCode.Success) { var read = CapabilityReader.ReadValue(cap); if (read.ContainerType == ContainerType.OneValue) { retVal = read.OneValue.ConvertToEnum(); } } } return retVal; } /// /// Gets the current value for a capability. /// /// The cap id. /// public object CapGetCurrent(CapabilityId capId) { using (TWCapability cap = new TWCapability(capId)) { var rc = _session.DGControl.Capability.GetCurrent(cap); if (rc == ReturnCode.Success) { var read = CapabilityReader.ReadValue(cap); switch (read.ContainerType) { case ContainerType.Enum: if (read.CollectionValues != null && read.CollectionValues.Count > read.EnumCurrentIndex) { return read.CollectionValues[read.EnumCurrentIndex]; } break; case ContainerType.OneValue: return read.OneValue; case ContainerType.Range: return read.RangeCurrentValue; case ContainerType.Array: // no source should ever return an array but anyway if (read.CollectionValues != null) { return read.CollectionValues.FirstOrDefault(); } break; } } } return null; } /// /// Gets the default value for a capability. /// /// The cap id. /// public object CapGetDefault(CapabilityId capId) { using (TWCapability cap = new TWCapability(capId)) { var rc = _session.DGControl.Capability.GetDefault(cap); if (rc == ReturnCode.Success) { var read = CapabilityReader.ReadValue(cap); switch (read.ContainerType) { case ContainerType.Enum: if (read.CollectionValues != null && read.CollectionValues.Count > read.EnumDefaultIndex) { return read.CollectionValues[read.EnumDefaultIndex]; } break; case ContainerType.OneValue: return read.OneValue; case ContainerType.Range: return read.RangeDefaultValue; case ContainerType.Array: // no source should ever return an array but anyway if (read.CollectionValues != null) { return read.CollectionValues.FirstOrDefault(); } break; } } } return null; } /// /// A general method that tries to get capability values from current . /// /// The capability unique identifier. /// public IList CapGet(CapabilityId capabilityId) { var list = new List(); using (TWCapability cap = new TWCapability(capabilityId)) { var rc = _session.DGControl.Capability.Get(cap); if (rc == ReturnCode.Success) { cap.ReadMultiCapValues(list); } } return list; } /// /// Resets all values and constraint to power-on defaults. /// /// The capability identifier. /// public ReturnCode CapResetAll(CapabilityId capabilityId) { using (TWCapability cap = new TWCapability(capabilityId) { ContainerType = ContainerType.DoNotCare }) { var rc = DGControl.Capability.ResetAll(cap); return rc; } } /// /// Resets the current value to power-on default. /// /// The capability identifier. /// public ReturnCode CapReset(CapabilityId capabilityId) { using (TWCapability cap = new TWCapability(capabilityId) { ContainerType = ContainerType.DoNotCare }) { var rc = DGControl.Capability.Reset(cap); return rc; } } #endregion #region high-level caps stuff #region audio caps private CapWrapper _audXferMech; /// /// Gets the property to work with audio for the current source. /// /// /// The audio xfer mech. /// public CapWrapper CapAudioXferMech { get { return _audXferMech ?? (_audXferMech = new CapWrapper(this, CapabilityId.ACapXferMech, ValueExtensions.ConvertToEnum, value => new TWCapability(CapabilityId.ACapXferMech, new TWOneValue { Item = (uint)value, ItemType = ItemType.UInt16 }))); } } #endregion #region img caps private CapWrapper _imgXferMech; /// /// Gets the property to work with image for the current source. /// /// /// The image xfer mech. /// public CapWrapper CapImageXferMech { get { return _imgXferMech ?? (_imgXferMech = new CapWrapper(this, CapabilityId.ICapXferMech, ValueExtensions.ConvertToEnum, value => new TWCapability(CapabilityId.ICapXferMech, new TWOneValue { Item = (uint)value, ItemType = ItemType.UInt16 }))); } } private CapWrapper _compression; /// /// Gets the property to work with image for the current source. /// /// /// The image compression. /// public CapWrapper CapImageCompression { get { return _compression ?? (_compression = new CapWrapper(this, CapabilityId.ICapCompression, ValueExtensions.ConvertToEnum, value => new TWCapability(CapabilityId.ICapCompression, new TWOneValue { Item = (uint)value, ItemType = ItemType.UInt16 }))); } } private CapWrapper _fileFormat; /// /// Gets the property to work with image for the current source. /// /// /// The image file format. /// public CapWrapper CapImageFileFormat { get { return _fileFormat ?? (_fileFormat = new CapWrapper(this, CapabilityId.ICapImageFileFormat, ValueExtensions.ConvertToEnum, value => new TWCapability(CapabilityId.ICapImageFileFormat, new TWOneValue { Item = (uint)value, ItemType = ItemType.UInt16 }))); } } private CapWrapper _pixelType; /// /// Gets the property to work with image for the current source. /// /// /// The image pixel type. /// public CapWrapper CapImagePixelType { get { return _pixelType ?? (_pixelType = new CapWrapper(this, CapabilityId.ICapPixelType, ValueExtensions.ConvertToEnum, value => new TWCapability(CapabilityId.ICapPixelType, new TWOneValue { Item = (uint)value, ItemType = ItemType.UInt16 }))); } } private CapWrapper _supportSize; /// /// Gets the property to work with image for the current source. /// /// /// The image supported size. /// public CapWrapper CapImageSupportedSize { get { return _supportSize ?? (_supportSize = new CapWrapper(this, CapabilityId.ICapSupportedSizes, ValueExtensions.ConvertToEnum, value => new TWCapability(CapabilityId.ICapSupportedSizes, new TWOneValue { Item = (uint)value, ItemType = ItemType.UInt16 }))); } } private CapWrapper _autoDeskew; /// /// Gets the property to work with image auto deskew flag for the current source. /// /// /// The image auto deskew flag. /// public CapWrapper CapImageAutoDeskew { get { return _autoDeskew ?? (_autoDeskew = new CapWrapper(this, CapabilityId.ICapAutomaticDeskew, ValueExtensions.ConvertToEnum, value => new TWCapability(CapabilityId.ICapAutomaticRotate, new TWOneValue { Item = (uint)value, ItemType = ItemType.Bool }))); } } private CapWrapper _autoRotate; /// /// Gets the property to work with image auto rotate flag for the current source. /// /// /// The image auto rotate flag. /// public CapWrapper CapImageAutoRotate { get { return _autoRotate ?? (_autoRotate = new CapWrapper(this, CapabilityId.ICapAutomaticRotate, ValueExtensions.ConvertToEnum, value => new TWCapability(CapabilityId.ICapAutomaticRotate, new TWOneValue { Item = (uint)value, ItemType = ItemType.Bool }))); } } private CapWrapper _xResolution; /// /// Gets the property to work with image horizontal resolution for the current source. /// /// /// The image horizontal resolution. /// public CapWrapper CapImageXResolution { get { return _xResolution ?? (_xResolution = new CapWrapper(this, CapabilityId.ICapXResolution, ValueExtensions.ConvertToFix32, value => new TWCapability(CapabilityId.ICapXResolution, new TWOneValue { Item = (uint)value,// ((uint)dpi) << 16; ItemType = ItemType.Fix32 }))); } } private CapWrapper _yResolution; /// /// Gets the property to work with image vertical resolution for the current source. /// /// /// The image vertical resolution. /// public CapWrapper CapImageYResolution { get { return _yResolution ?? (_yResolution = new CapWrapper(this, CapabilityId.ICapYResolution, ValueExtensions.ConvertToFix32, value => new TWCapability(CapabilityId.ICapYResolution, new TWOneValue { Item = (uint)value,// ((uint)dpi) << 16; ItemType = ItemType.Fix32 }))); } } private CapWrapper _borderDetect; /// /// Gets the property to work with auto border detection flag for the current source. /// /// /// The auto border detection flag. /// public CapWrapper CapImageAutomaticBorderDetection { get { return _borderDetect ?? ( _borderDetect = new CapWrapper(this, CapabilityId.ICapAutomaticBorderDetection, ValueExtensions.ConvertToEnum, value => { var rc = ReturnCode.Failure; var one = new TWOneValue { Item = (uint)value, ItemType = ItemType.Bool }; using (TWCapability capValue = new TWCapability(CapabilityId.ICapUndefinedImageSize, one)) { rc = _session.DGControl.Capability.Set(capValue); } using (TWCapability capValue = new TWCapability(CapabilityId.ICapAutomaticBorderDetection, one)) { rc = _session.DGControl.Capability.Set(capValue); } return rc; })); } } #endregion #region other caps private CapWrapper _duplexEnabled; /// /// Gets the property to work with duplex enabled flag for the current source. /// /// /// The duplex enabled flag. /// public CapWrapper CapDuplexEnabled { get { return _duplexEnabled ?? (_duplexEnabled = new CapWrapper(this, CapabilityId.CapDuplexEnabled, ValueExtensions.ConvertToEnum, value => new TWCapability(CapabilityId.CapDuplexEnabled, new TWOneValue { Item = (uint)value, ItemType = ItemType.Bool }))); } } private CapWrapper _xferCount; /// /// Gets the property to work with xfer count for the current source. /// /// /// The xfer count. /// public CapWrapper CapXferCount { get { return _xferCount ?? (_xferCount = new CapWrapper(this, CapabilityId.CapXferCount, ValueExtensions.ConvertToEnum, value => new TWCapability(CapabilityId.CapXferCount, new TWOneValue { Item = value > 0 ? (uint)value : uint.MaxValue, ItemType = ItemType.UInt16 }))); } } private CapWrapper _feederEnabled; /// /// Gets the property to work with feeder enabled flag for the current source. /// /// /// The feeder enabled flag. /// public CapWrapper CapFeederEnabled { get { return _feederEnabled ?? (_feederEnabled = new CapWrapper(this, CapabilityId.CapFeederEnabled, ValueExtensions.ConvertToEnum, value => { var rc = ReturnCode.Failure; var one = new TWOneValue { Item = (uint)value, ItemType = ItemType.Bool }; // we will never set feeder off, only autofeed and autoscan // but if it is true then enable feeder needs to be set first if (value == BoolType.True) { using (TWCapability enabled = new TWCapability(CapabilityId.CapFeederEnabled, one)) { rc = _session.DGControl.Capability.Set(enabled); } } // to really use feeder we must also set autofeed or autoscan, but only // for one of them since setting autoscan also sets autofeed if (SupportedCaps.Contains(CapabilityId.CapAutoScan)) { using (TWCapability autoScan = new TWCapability(CapabilityId.CapAutoScan, one)) { rc = _session.DGControl.Capability.Set(autoScan); } } else if (SupportedCaps.Contains(CapabilityId.CapAutoFeed)) { using (TWCapability autoScan = new TWCapability(CapabilityId.CapAutoFeed, one)) { rc = _session.DGControl.Capability.Set(autoScan); } } return rc; })); } } #endregion #endregion } }