Progress making cap value reader wrapper.

This commit is contained in:
Eugene Wang
2021-04-24 23:05:38 -04:00
parent e0f1f96947
commit ab6a546bba
5 changed files with 169 additions and 28 deletions

133
NTwain/CapWrapper.cs Normal file
View File

@@ -0,0 +1,133 @@
using System;
using System.Collections.Generic;
using System.Linq;
using TWAINWorkingGroup;
namespace NTwain
{
/// <summary>
/// Contains operations for a <see cref="CAP"/>.
/// </summary>
public class CapWrapper
{
private readonly TWAIN _twain;
public CapWrapper(TWAIN twain, CAP cap)
{
_twain = twain;
Cap = cap;
var twCap = new TW_CAPABILITY
{
Cap = cap
};
var sts = _twain.DatCapability(DG.CONTROL, MSG.QUERYSUPPORT, ref twCap);
if (sts == STS.SUCCESS)
{
if (Enum.TryParse(_twain.CapabilityOneValueToString(twCap), out TWQC qc))
{
Supports = qc;
}
}
}
/// <summary>
/// The cap in question.
/// </summary>
public CAP Cap { get; }
/// <summary>
/// The operations supported by the cap.
/// Not all sources supports this so it may be unknown.
/// </summary>
public TWQC Supports { get; }
/// <summary>
/// Try to get string representation of the cap's supported values.
/// </summary>
/// <returns></returns>
public IList<string> GetValues()
{
var twCap = new TW_CAPABILITY
{
Cap = Cap
};
var sts = _twain.DatCapability(DG.CONTROL, MSG.GET, ref twCap);
if (sts == STS.SUCCESS)
{
switch (twCap.ConType)
{
case TWON.ONEVALUE:
return new[] { _twain.CapabilityOneValueToString(twCap) };
case TWON.ENUMERATION:
var csv = _twain.CapabilityToCsv(twCap, true);
return csv.Split(',').Skip(6).ToList();
default:
csv = _twain.CapabilityToCsv(twCap, true);
return csv.Split(',').Skip(4).ToList();
}
}
return new string[0];
}
/// <summary>
/// Try to get string representation of the cap's current value.
/// </summary>
/// <returns></returns>
public string GetCurrent()
{
var twCap = new TW_CAPABILITY
{
Cap = Cap
};
var sts = _twain.DatCapability(DG.CONTROL, MSG.GETCURRENT, ref twCap);
if (sts == STS.SUCCESS)
{
switch (twCap.ConType)
{
case TWON.ONEVALUE:
return _twain.CapabilityOneValueToString(twCap);
case TWON.ENUMERATION:
var csv = _twain.CapabilityToCsv(twCap, true);
return csv.Split(new[] { ',' }, 7)[6];
default:
csv = _twain.CapabilityToCsv(twCap, true);
return csv.Split(new[] { ',' }, 5)[4];
}
}
return null;
}
/// <summary>
/// Try to get string representation of the cap's default value.
/// </summary>
/// <returns></returns>
public string GetDefault()
{
var twCap = new TW_CAPABILITY
{
Cap = Cap
};
var sts = _twain.DatCapability(DG.CONTROL, MSG.GETDEFAULT, ref twCap);
if (sts == STS.SUCCESS)
{
switch (twCap.ConType)
{
case TWON.ONEVALUE:
return _twain.CapabilityOneValueToString(twCap);
case TWON.ENUMERATION:
var csv = _twain.CapabilityToCsv(twCap, true);
return csv.Split(new[] { ',' }, 7)[6];
default:
csv = _twain.CapabilityToCsv(twCap, true);
return csv.Split(new[] { ',' }, 5)[4];
}
}
return null;
}
}
}

View File

@@ -4278,6 +4278,7 @@ namespace TWAINWorkingGroup
[Flags]
public enum TWQC : ushort
{
Uknown = 0,
GET = 0x0001,
SET = 0x0002,
GETDEFAULT = 0x0004,

View File

@@ -287,13 +287,12 @@ namespace NTwain
}
/// <summary>
/// Queries current device for supported capabilities.
/// Not all devices supports this.
/// Get current device's capabilities.
/// </summary>
/// <returns></returns>
public IEnumerable<CAP> SupportedCaps()
public Dictionary<CAP, CapWrapper> Capabilities()
{
List<CAP> caps = null;
Dictionary<CAP, CapWrapper> caps = null;
if (State >= STATE.S4)
{
TW_CAPABILITY cap = default;
@@ -309,37 +308,30 @@ namespace NTwain
{
if (Enum.TryParse(val, out CAP c))
{
return c;
return new CapWrapper(_twain, c);
}
else if (val.StartsWith("0x"))
{
return (CAP)Convert.ToUInt16(val, 16);
return new CapWrapper(_twain, (CAP)Convert.ToUInt16(val, 16));
}
else if (ushort.TryParse(val, out ushort num))
{
return (CAP)num;
return new CapWrapper(_twain, (CAP)num);
}
return (CAP)0;
return null;
}).ToList();
})
.Where(cs => cs != null)
.ToDictionary(cs => cs.Cap, cs => cs);
}
else
{
// don't support list, just give everything
caps = Enum.GetValues(typeof(CAP)).Cast<CAP>()
.ToDictionary(c => c, c => new CapWrapper(_twain, c));
}
//foreach (CAP capId in Enum.GetValues(typeof(CAP)))
//{
// cap.ConType = TWON.ONEVALUE;
// cap.Cap = capId;
// var sts = _twain.DatCapability(DG.CONTROL, MSG.QUERYSUPPORT, ref cap);
// if (sts == STS.SUCCESS)
// {
// if (Enum.TryParse(_twain.CapabilityOneValueToString(cap), out TWQC qc))
// {
// }
// }
//}
}
return caps ?? Enumerable.Empty<CAP>();
return caps ?? new Dictionary<CAP, CapWrapper>();
}
/// <summary>

View File

@@ -21,6 +21,8 @@ namespace NTwain
/// <returns></returns>
public static string CapabilityOneValueToString(this TWAIN twain, TW_CAPABILITY cap)
{
if (cap.ConType != TWON.ONEVALUE) throw new InvalidOperationException($"Cannot read container {cap.ConType} as one value.");
if (cap.hContainer == IntPtr.Zero) return null;
var lockedPtr = twain.DsmMemLock(cap.hContainer);

View File

@@ -74,11 +74,24 @@ namespace Net5Console
Console.WriteLine($"\t{session.CurrentDevice}");
Console.WriteLine();
var caps = session.SupportedCaps();
var caps = session.Capabilities();
Console.WriteLine("Device supports these caps:");
foreach (var cap in caps.OrderBy(c => c))
foreach (var cap in caps.Keys.OrderBy(c => c))
{
Console.WriteLine($"\t{cap}");
Console.WriteLine($"\t{cap}: {caps[cap].Supports}");
}
Console.WriteLine();
if (caps.TryGetValue(CAP.ICAP_BITDEPTH, out CapWrapper wrapper))
{
Console.WriteLine($"Details on {wrapper.Cap}:");
Console.WriteLine($"\tDefault: {wrapper.GetDefault()}");
Console.WriteLine($"\tCurrent: {wrapper.GetCurrent()}");
Console.WriteLine($"\tValues:");
foreach (var val in wrapper.GetValues())
{
Console.WriteLine($"\t\t{val}");
}
}
Console.WriteLine();