VIDAR Scanner problem with PInvoke

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi everyone,<br/>
I am having problems creating a C# interface with a C++ API for Vidar Scanners.
Here is the code Im using:
using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using System.Text;<br/>
using System.Runtime.InteropServices;
namespace Novarad.Common.Multimedia.Scanning {<br/>
public class VidarScanning {<br/>
// Import the methods from the unmanaged code<br/>
<br/>
// for x86 apps on x64 machine<br/>
[DllImport("Vscsi32.dll", EntryPoint = "findDigitizer", CallingConvention = CallingConvention.Cdecl)]<br/>
//public static extern long findDigitizer(unsigned char *digitizerName, unsigned short *host_adapter_ID, unsigned short *scsi_ID);<br/>
public static extern unsafe Int32 findDigitizerX86_X64(void* digitizerName, ref ushort host_adapter_ID, ref ushort scsi_ID);<br/>
[DllImport("Vscsi32.dll", EntryPoint = "getDigitizerInfo", CallingConvention = CallingConvention.Cdecl)]<br/>
//public static extern long getDigitizerInfo(DIGITIZERINFO* pDI);<br/>
public static extern unsafe Int32 getDigitizerInfoX86_X64(ref DigitizerInfo pDI);<br/>
[DllImport("Vscsi32.dll", EntryPoint = "Scan", CallingConvention = CallingConvention.Cdecl)]<br/>
//public static extern long Scan(DIGITIZERINFO *di, SCANPARAMETERS *sp, unsigned char **image_buffer, unsigned long *total_bytes_received);<br/>
public static extern unsafe Int32 ScanX86_X64(ref DigitizerInfo di, ref ScanParameters sp, void* image_buffer, ref UInt32 total_bytes_received);<br/>
[DllImport("Vscsi32.dll", EntryPoint = "getVidarError", CallingConvention = CallingConvention.Cdecl)]<br/>
//public static extern short getVidarError(long Err, VIDARERRORINFO* ErrInfo, unsigned short* ErrCount);<br/>
public static extern unsafe short getVidarErrorX86_X64(Int32 Err, ref ErrorInfo ErrInfo, ref ushort ErrCount);<br/>
[DllImport("Vscsi32.dll", EntryPoint = "StartLogging", CallingConvention = CallingConvention.Cdecl)]<br/>
public static extern unsafe short StartLoggingX86_X64();<br/>
[DllImport("Vscsi32.dll", EntryPoint = "StopLogging", CallingConvention = CallingConvention.Cdecl)]<br/>
public static extern unsafe short StopLoggingX86_X64();
// Layout and define the managed structures<br/>
[StructLayout(LayoutKind.Sequential)]<br/>
public struct DigitizerInfo {<br/>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]<br/>
public ushort[] resolutions;<br/>
public ushort resolutionsCount;<br/>
public ushort currentResolution;<br/>
public ushort opticalResolution;<br/>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]<br/>
public ushort[] bitDepths;<br/>
public ushort bitDepthsCount;<br/>
public ushort currentBitDepth;<br/>
public float maxWidth;<br/>
public ushort lineTime;<br/>
public ushort lineTimeStart;<br/>
public ushort lineTimeEnd;<br/>
public ushort currentLineTime;<br/>
public ushort lineTimeFixed;<br/>
public ushort darkEnhance;<br/>
public ushort lineFilter;<br/>
public ushort maxFilms;<br/>
public ushort backupFilm;<br/>
public ushort dnldBlocks;<br/>
public ushort translationTables;<br/>
public ushort locationZero;<br/>
public ushort ejectMode;<br/>
public ushort feederType;<br/>
public ushort digitizerModel;<br/>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]<br/>
public string serialNumber;<br/>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]<br/>
public string fwVer;<br/>
public ushort hwVer;<br/>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]<br/>
public string corePsd;<br/>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]<br/>
public string extPswd;<br/>
public ushort lampType;<br/>
public ushort isPAYG;<br/>
}<br/>
[StructLayout(LayoutKind.Sequential)]<br/>
public struct ScanParameters {<br/>
public ushort bitdepth;<br/>
public ushort resolution;<br/>
public ushort pixel_width;<br/>
public UInt32 pixel_length;<br/>
public byte output_mode;<br/>
public Int32 scan_mode;<br/>
public ushort bit_order;<br/>
public UInt32 bitDepthBytes;<br/>
public ushort avgCount;<br/>
public short transTableNum;<br/>
public UInt32 bUseDetectedWidth;<br/>
public UInt32 bScanDone;<br/>
public ushort detected_pixel_width;<br/>
public UInt32 detected_pixel_length;<br/>
public UInt32 bScanAbort;<br/>
public UInt32 scan_byte_count;<br/>
public ushort Yresolution;<br/>
public UInt32 xOffset;<br/>
public UInt32 yOffset;<br/>
public ushort turboMode;<br/>
}
[StructLayout(LayoutKind.Sequential)]<br/>
public struct ErrorInfo {<br/>
public ushort errCode;<br/>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] <br/>
public string message;<br/>
}<br/>
}<br/>
}
/* These are the structures as they appear in the C++ header file<br/>
typedef struct _DIGITIZERINFO<br/>
{<br/>
unsigned short resolutions[20];<br/>
unsigned short resolutionsCount;<br/>
unsigned short currentResolution;<br/>
unsigned short opticalResolution;<br/>
unsigned short bitDepths[10];<br/>
unsigned short bitDepthsCount;<br/>
unsigned short currentBitDepth;<br/>
float maxWidth;<br/>
unsigned short lineTime;<br/>
unsigned short lineTimeStart;<br/>
unsigned short lineTimeEnd;<br/>
unsigned short currentLineTime;<br/>
unsigned short lineTimeFixed;<br/>
unsigned short darkEnhance;<br/>
unsigned short lineFilter;<br/>
unsigned short maxFilms;<br/>
unsigned short backupFilm;<br/>
unsigned short dnldBlocks;<br/>
unsigned short translationTables;<br/>
unsigned short locationZero;<br/>
unsigned short ejectMode;<br/>
unsigned short feederType;<br/>
unsigned short digitizerModel;<br/>
unsigned char serialNumber[7];<br/>
unsigned char fwVer[5];<br/>
unsigned short hwVer;<br/>
unsigned char corePswd[11];<br/>
unsigned char extPswd[7];<br/>
unsigned short lampType;<br/>
unsigned short isPAYG;<br/>
} DIGITIZERINFO;<br/>
typedef struct _SCANPARAMETERS<br/>
{<br/>
unsigned short bitdepth;<br/>
unsigned short resolution;<br/>
unsigned short pixel_width;<br/>
unsigned long pixel_length;<br/>
unsigned char output_mode;<br/>
int scan_mode;<br/>
unsigned short bit_order;<br/>
unsigned long bitDepthBytes;<br/>
unsigned short avgCount;<br/>
short transTableNum;<br/>
unsigned int bUseDetectedWidth;<br/>
unsigned int bScanDone;<br/>
unsigned short detected_pixel_width;<br/>
unsigned long detected_pixel_length;<br/>
unsigned int bScanAbort;<br/>
unsigned int scan_byte_count;<br/>
unsigned short Yresolution;<br/>
unsigned long xOffset;<br/>
unsigned long yOffset;<br/>
unsigned short turboMode;<br/>
} SCANPARAMETERS;<br/>
typedef struct _VIDARERRORINFO<br/>
{<br/>
unsigned short ErrCode;<br/>
char Message[80];<br/>
} VIDARERRORINFO;<br/>
*/
________________________________________________________________________________________________
using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using System.Text;<br/>
using System.Runtime.InteropServices;
namespace Novarad.Common.Multimedia.Scanning {<br/>
public class VidarAPIManager {<br/>
#region consts<br/>
public const ushort LINE_TIME_FIXED = 1;<br/>
public const ushort DARK_ENHANCE_SUPPORTED = 1;<br/>
public const ushort TRANS_TABLES_FULL_SET = 1;<br/>
public const short TRANS_TABLE_ZERO_LINEAR = 0;<br/>
public const short TRANS_TABLE_ZERO_LOG = 1;<br/>
public const ushort EJECT_MODE_DOUBLE = 1;
#region model types<br/>
public const ushort MODEL_UNKNOWN = 0;<br/>
public const ushort VXR12 = 1;<br/>
public const ushort VXR12_PLUS = 2;<br/>
public const ushort VXR16 = 3;<br/>
public const ushort VXR16_DOS = 4;<br/>
public const ushort DIAG_PRO = 5;<br/>
public const ushort DIAG_PRO_PLUS_USA = 6;<br/>
public const ushort DIAG_PRO_PLUS_EURO = 7;<br/>
public const ushort DIAG_PRO_M = 8;<br/>
public const ushort SIERRA = 9;<br/>
public const ushort SIERRA_PLUS = 10;<br/>
public const ushort DIAG_PRO_ADVANTAGE_USA = 11;<br/>
public const ushort DIAG_PRO_ADVANTAGE_EURO = 12;<br/>
public const ushort DOS_PRO_ADVANTAGE = 13;<br/>
public const ushort CAD_PRO_ADVANTAGE = 14;<br/>
public const ushort KERNEL = 15;<br/>
public const ushort SIERRA_ADVANTAGE = 16;<br/>
public const ushort DENTAL_FILM_DIGITIZER = 17;<br/>
public const ushort TELERAD_PRO = 19;<br/>
public const ushort TELERAD_PRO_EDGE = 22;<br/>
public const ushort VETRAD_PRO_EDGE = 23;<br/>
#endregion model types
public const byte GRAYSCALE_8 = 4;<br/>
public const byte GRAYSCALE_12 = 7;<br/>
public const byte GRAYSCALE_16 = 6;<br/>
public const Int32 SM_LINES = 0;<br/>
public const Int32 SM_SOFTSENSOR = 1;<br/>
public const ushort BO_MOTOROLA = 0;<br/>
public const ushort BO_INTEL = 1;<br/>
#endregion consts
#region private member variables<br/>
private bool _is64BitApp = false;<br/>
private bool _is64BitOS = false;<br/>
private ushort _errCount = 0;<br/>
private ushort _hostAdapterID = 0;<br/>
private ushort _scsiID = 0;<br/>
private UInt32 _totalBytesReceived = 0;<br/>
private string _digitizerName = null;<br/>
private IntPtr _imageBuffer;<br/>
private VidarScanning.ErrorInfo _errorInfo;<br/>
private VidarScanning.DigitizerInfo _digitizerInfo;<br/>
private VidarScanning.ScanParameters _scanParameters;<br/>
#endregion private member variables
#region constructors<br/>
public VidarAPIManager() {<br/>
_is64BitApp = Environment.Is64BitProcess;<br/>
_is64BitOS = Environment.Is64BitOperatingSystem;<br/>
_errorInfo = new VidarScanning.ErrorInfo();<br/>
_digitizerInfo = new VidarScanning.DigitizerInfo();<br/>
_scanParameters = new VidarScanning.ScanParameters();<br/>
}<br/>
#endregion contructors
#region methods<br/>
public unsafe bool lookForDigitizer() {<br/>
bool success = false;<br/>
IntPtr digNamePtr = IntPtr.Zero;
try {<br/>
// alloc mem for digitizer pointer<br/>
digNamePtr = Marshal.AllocHGlobal(IntPtr.Size);
// call external method<br/>
if (_is64BitApp) {<br/>
//getScanningError(VidarScanning.findDigitizerX64_X64(digNamePtr.ToPointer(), ref _hostAdapterID, ref _scsiID), ref _errorInfo, ref _errCount);<br/>
}<br/>
else if (_is64BitOS) {<br/>
getScanningError(VidarScanning.findDigitizerX86_X64(digNamePtr.ToPointer(), ref _hostAdapterID, ref _scsiID), ref _errorInfo, ref _errCount);<br/>
}<br/>
else {<br/>
//getScanningError(VidarScanning.findDigitizerX86_X86(digNamePtr.ToPointer(), ref _hostAdapterID, ref _scsiID), ref _errorInfo, ref _errCount);<br/>
}
_digitizerName = Marshal.PtrToStringAnsi(digNamePtr);<br/>
if (_digitizerName.Length > 1) {<br/>
_digitizerName = _digitizerName.Substring(0, _digitizerName.IndexOf( ));<br/>
}<br/>
if (_errorInfo.errCode == 0) {<br/>
success = true;<br/>
}<br/>
return success;<br/>
}<br/>
finally {<br/>
// clean up memory<br/>
if (digNamePtr != IntPtr.Zero) {<br/>
Marshal.FreeHGlobal(digNamePtr);<br/>
}<br/>
}<br/>
}
public unsafe bool getDigitizerInformation() {<br/>
bool success = false;
// call external method<br/>
if (_is64BitApp) {<br/>
//getScanningError(VidarScanning.getDigitizerInfoX64_X64(ref _digitizerInfo), ref _errorInfo, ref _errCount);<br/>
}<br/>
else if (_is64BitOS) {<br/>
getScanningError(VidarScanning.getDigitizerInfoX86_X64(ref _digitizerInfo), ref _errorInfo, ref _errCount);<br/>
}<br/>
else {<br/>
//getScanningError(VidarScanning.getDigitizerInfoX86_X86(ref _digitizerInfo), ref _errorInfo, ref _errCount);<br/>
}<br/>
if (_errorInfo.errCode == 0) {<br/>
success = true;<br/>
}
return success;<br/>
}
public unsafe bool scanImage() {<br/>
bool success = false;<br/>
IntPtr imgPtr = IntPtr.Zero;
try {<br/>
// alloc mem for image buffer pointer<br/>
imgPtr = Marshal.AllocHGlobal((int)(_scanParameters.pixel_width * _scanParameters.pixel_length * _scanParameters.bitDepthBytes));
// call external method<br/>
if (_is64BitApp) {<br/>
//getScanningError(VidarScanning.ScanX64_X64(ref _digitizerInfo, ref _scanParameters, imgPtr.ToPointer(), ref _totalBytesReceived), ref _errorInfo, ref _errCount);<br/>
} <br/>
else if (_is64BitOS) {<br/>
getScanningError(VidarScanning.ScanX86_X64(ref _digitizerInfo, ref _scanParameters, imgPtr.ToPointer(), ref _totalBytesReceived), ref _errorInfo, ref _errCount);<br/>
}<br/>
else {<br/>
//getScanningError(VidarScanning.ScanX86_X86(ref _digitizerInfo, ref _scanParameters, imgPtr.ToPointer(), ref _totalBytesReceived), ref _errorInfo, ref _errCount);<br/>
}
_imageBuffer = Marshal.ReadIntPtr(imgPtr);
if (_errorInfo.errCode == 0) {<br/>
success = true;<br/>
}
return success;<br/>
}<br/>
finally {<br/>
if (imgPtr != IntPtr.Zero) {<br/>
Marshal.FreeHGlobal(imgPtr);<br/>
}<br/>
}<br/>
}
private unsafe short getScanningError(Int32 error, ref VidarScanning.ErrorInfo info, ref ushort count) {<br/>
short result = 0;
// call external method<br/>
if (_is64BitApp) {<br/>
//result = VidarScanning.getVidarErrorX64_X64(error, ref info, ref count);<br/>
}<br/>
else if (_is64BitOS) {<br/>
result = VidarScanning.getVidarErrorX86_X64(error, ref info, ref count);<br/>
}<br/>
else {<br/>
//result = VidarScanning.getVidarErrorX86_X86(error, ref info, ref count);<br/>
}<br/>
<br/>
return result;<br/>
}
public unsafe short startLogging() {<br/>
short result = 0;
// call external method<br/>
if (_is64BitApp) {<br/>
//result = VidarScanning.StartLoggingX64_X64();<br/>
}<br/>
else if (_is64BitOS) {<br/>
result = VidarScanning.StartLoggingX86_X64();<br/>
}<br/>
else {<br/>
//result = VidarScanning.StartLoggingX86_X86();<br/>
}
return result;<br/>
}
public unsafe short stopLogging() {<br/>
short result = 0;
// call external method<br/>
if (_is64BitApp) {<br/>
//result = VidarScanning.StopLoggingX64_X64();<br/>
}<br/>
else if (_is64BitOS) {<br/>
result = VidarScanning.StopLoggingX86_X64();<br/>
}<br/>
else {<br/>
//result = VidarScanning.StopLoggingX86_X86();<br/>
}
return result;<br/>
}<br/>
#endregion methods
#region properties<br/>
public string DigitizerName {<br/>
get {<br/>
return _digitizerName;<br/>
}<br/>
}
public ushort ErrorCount {<br/>
get {<br/>
return _errCount;<br/>
}<br/>
}
public ushort HostAdapterID {<br/>
get {<br/>
return _hostAdapterID;<br/>
}<br/>
}
public ushort SCSI_ID {<br/>
get {<br/>
return _scsiID;<br/>
}<br/>
}
public IntPtr ImageBuffer {<br/>
get {<br/>
return _imageBuffer;<br/>
}<br/>
}
public UInt32 TotalBytesReceived {<br/>
get {<br/>
return _totalBytesReceived;<br/>
}<br/>
}
public ushort[] Resolutions { // resolutions available<br/>
get {<br/>
ushort[] resolutions = new ushort[this.ResolutionsCount];<br/>
for (int i = 0; i < this.ResolutionsCount; i++) {<br/>
resolutions = _digitizerInfo.resolutions;<br/>
}<br/>
return resolutions; <br/>
}<br/>
}
public ushort ResolutionsCount { // number of resolutions available<br/>
get {<br/>
return _digitizerInfo.resolutionsCount;<br/>
}<br/>
}
public ushort CurrentResolution { // current resolution that will be used for scanning<br/>
get {<br/>
return _digitizerInfo.currentResolution;<br/>
}<br/>
}
public ushort XResolution {<br/>
set {<br/>
_scanParameters.resolution = value;<br/>
}<br/>
get {<br/>
return _scanParameters.resolution;<br/>
}<br/>
}
public ushort YResolution {<br/>
set {<br/>
_scanParameters.Yresolution = value;<br/>
}<br/>
get {<br/>
return _scanParameters.Yresolution;<br/>
}<br/>
}
public ushort[] BitDepths { // bit depths available<br/>
get {<br/>
ushort[] bitDepths = new ushort[this.BitDepthsCount];<br/>
for (int i = 0; i < this.BitDepthsCount; i++) {<br/>
bitDepths = _digitizerInfo.bitDepths;<br/>
}<br/>
return bitDepths; <br/>
}<br/>
}
public ushort BitDepthsCount { // number of bit depths available<br/>
get {<br/>
return _digitizerInfo.bitDepthsCount;<br/>
}<br/>
}
public ushort CurrentBitDepth { // current bit depth value that will be used for scanning<br/>
get {<br/>
return _digitizerInfo.currentBitDepth;<br/>
}<br/>
set {<br/>
_scanParameters.bitdepth = value;<br/>
}<br/>
}
public float MaxWidth { // widest scan width possible in pixels<br/>
get {<br/>
return _digitizerInfo.maxWidth;<br/>
}<br/>
}
public ushort LineTime { // exposure time in milliseconds<br/>
get {<br/>
return _digitizerInfo.lineTime;<br/>
}<br/>
}
public ushort CurrentLineTime { // current exposure time that will be used for scanning in milliseconds<br/>
get {<br/>
return _digitizerInfo.currentLineTime;<br/>
}<br/>
}
public bool LineTimeFixed { // whether or not the line time value is fixed<br/>
get {<br/>
return _digitizerInfo.lineTimeFixed == LINE_TIME_FIXED; <br/>
}<br/>
}
public bool DarkEnhanceSupported {<br/>
get {<br/>
return _digitizerInfo.darkEnhance == DARK_ENHANCE_SUPPORTED;<br/>
}<br/>
}
public ushort MaxFilms {<br/>
get {<br/>
return _digitizerInfo.maxFilms;<br/>
}<br/>
}
public bool FullTranslationTables {<br/>
get {<br/>
return _digitizerInfo.translationTables == TRANS_TABLES_FULL_SET;<br/>
}<br/>
}
public bool LocationZeroIsLog {<br/>
get {<br/>
return _digitizerInfo.locationZero == TRANS_TABLE_ZERO_LOG;<br/>
}<br/>
}
public bool EjectModeDouble {<br/>
get {<br/>
return _digitizerInfo.ejectMode == EJECT_MODE_DOUBLE;<br/>
}<br/>
}
public string Model {<br/>
get {<br/>
switch (_digitizerInfo.digitizerModel) {<br/>
case VXR12:<br/>
return "VXR-12";<br/>
case VXR12_PLUS:<br/>
return "VXR-12 plus";<br/>
case VXR16:<br/>
return "VXR-16";<br/>
case VXR16_DOS:<br/>
return "VXR-16 Dosimetry";<br/>
case DIAG_PRO:<br/>
return "DiagnosticPRO";<br/>
case DIAG_PRO_PLUS_USA:<br/>
return "DiagnosticPRO plus (USA)";<br/>
case DIAG_PRO_PLUS_EURO:<br/>
return "DiagnosticPRO plus (European)";<br/>
case DIAG_PRO_M:<br/>
return "DiagnosticPRO - M";<br/>
case SIERRA:<br/>
return "Sierra";<br/>
case SIERRA_PLUS:<br/>
return "Sierra plus";<br/>
case SIERRA_ADVANTAGE:<br/>
return "Sierra Advantage";<br/>
case DIAG_PRO_ADVANTAGE_USA:<br/>
return "DiagnosticPRO Advantage (USA)";<br/>
case DIAG_PRO_ADVANTAGE_EURO:<br/>
return "DiagnosticPRO Advantage (European)";<br/>
case DOS_PRO_ADVANTAGE:<br/>
return "DosimetryPRO Advantage";<br/>
case CAD_PRO_ADVANTAGE:<br/>
return "CAD PRO Advantage";<br/>
case DENTAL_FILM_DIGITIZER:<br/>
return "Dental Film Digitizer";<br/>
case TELERAD_PRO:<br/>
return "TeleradPRO";<br/>
case TELERAD_PRO_EDGE:<br/>
return "TeleradPRO Edge";<br/>
case VETRAD_PRO_EDGE:<br/>
return "VETradPRO Edge";<br/>
case KERNEL:<br/>
return "Kernel mode";<br/>
case MODEL_UNKNOWN:<br/>
default:<br/>
return "Unknown Model";<br/>
}<br/>
}<br/>
}
public string SerialNumber {<br/>
get {<br/>
return _digitizerInfo.serialNumber;<br/>
}<br/>
}
public string FirmwareVersion {<br/>
get {<br/>
return _digitizerInfo.fwVer;<br/>
}<br/>
}
public ushort HardwareVersion {<br/>
get {<br/>
return _digitizerInfo.hwVer;<br/>
}<br/>
}
public string CorePassword {<br/>
get {<br/>
return _digitizerInfo.corePsd;<br/>
}<br/>
}
public string ExtendedPassword {<br/>
get {<br/>
return _digitizerInfo.extPswd;<br/>
}<br/>
}
public ushort PixelWidth {<br/>
set {<br/>
_scanParameters.pixel_width = value;<br/>
}<br/>
}
public UInt32 PixelLength {<br/>
set {<br/>
_scanParameters.pixel_length = value;<br/>
}<br/>
}
public byte OutputMode {<br/>
set {<br/>
_scanParameters.output_mode = value;<br/>
}<br/>
}
public Int32 ScanMode {<br/>
set {<br/>
_scanParameters.scan_mode = value;<br/>
}<br/>
}
public ushort BitOrder {<br/>
set {<br/>
_scanParameters.bit_order = value;<br/>
}<br/>
}
public UInt32 BitDepthBytes {<br/>
set {<br/>
_scanParameters.bitDepthBytes = value;<br/>
}<br/>
}
public ushort AvgCount {<br/>
set {<br/>
_scanParameters.avgCount = value;<br/>
}<br/>
}
public short TransTableNum {<br/>
set {<br/>
_scanParameters.transTableNum = value;<br/>
}<br/>
}
public bool UseDetectedWidth {<br/>
set {<br/>
if (value == true) {<br/>
_scanParameters.bUseDetectedWidth = 1;<br/>
}<br/>
else {<br/>
_scanParameters.bUseDetectedWidth = 0;<br/>
}<br/>
}<br/>
}
public bool ScanDone {<br/>
get {<br/>
if (_scanParameters.bScanDone == 1) {<br/>
return true;<br/>
}<br/>
else {<br/>
return false;<br/>
}<br/>
}<br/>
}
public ushort DetectedPixelWidth {<br/>
get {<br/>
return _scanParameters.detected_pixel_width;<br/>
}<br/>
}
public UInt32 DetectedPixelLength {<br/>
get {<br/>
return _scanParameters.detected_pixel_length;<br/>
}<br/>
}
public bool AbortScan {<br/>
set {<br/>
if (value) {<br/>
_scanParameters.bScanAbort = 1;<br/>
}<br/>
else {<br/>
_scanParameters.bScanAbort = 0;<br/>
}<br/>
}<br/>
}
public UInt32 ScanByteCount {<br/>
get {<br/>
return _scanParameters.scan_byte_count;<br/>
}<br/>
}
public string ErrorMessage {<br/>
get {<br/>
return _errorInfo.message;<br/>
}<br/>
}
public ushort ErrorCode {<br/>
get {<br/>
return _errorInfo.errCode;<br/>
}<br/>
}<br/>
#endregion properties<br/>
}<br/>
}
__________________________________________________________________________________
// Heres the code that calls the methods<br/>
VidarAPIManager vidarManager = new VidarAPIManager();<br/>
SetupVidarAPIEnvironment(vidarManager);<br/>
vidarManager.scanImage();
private void SetupVidarAPIEnvironment(VidarAPIManager vidarManager) {<br/>
try {<br/>
if (vidarManager.lookForDigitizer()) {<br/>
if (vidarManager.getDigitizerInformation()) {<br/>
// setup Vidar scan parameters
// set bitdepth, output mode, and bitDepthBytes<br/>
bool has16Bit = false;<br/>
bool has12Bit = false;<br/>
bool has8Bit = false;<br/>
for (int i = 0; i < vidarManager.BitDepthsCount; i++) {<br/>
if (vidarManager.BitDepths == 16) {<br/>
has16Bit = true;<br/>
}<br/>
else if (vidarManager.BitDepths == 12) {<br/>
has12Bit = true;<br/>
}<br/>
else if (vidarManager.BitDepths == 8) {<br/>
has8Bit = true;<br/>
}<br/>
}<br/>
if (_currentProfile.Use16Bit) {<br/>
if (has16Bit) { // use 16-bit if available<br/>
vidarManager.CurrentBitDepth = 16;<br/>
vidarManager.BitDepthBytes = 2;<br/>
if (_currentProfile.ForceGrayscale) {<br/>
vidarManager.OutputMode = VidarAPIManager.GRAYSCALE_16;<br/>
}<br/>
}<br/>
else if (has12Bit) { // otherwise use 12-bit if available<br/>
vidarManager.CurrentBitDepth = 12;<br/>
vidarManager.BitDepthBytes = 2;<br/>
if (_currentProfile.ForceGrayscale) {<br/>
vidarManager.OutputMode = VidarAPIManager.GRAYSCALE_12;<br/>
}<br/>
}<br/>
else if (has8Bit) { // we must use 8-bit if available<br/>
vidarManager.CurrentBitDepth = 8;<br/>
vidarManager.BitDepthBytes = 1;<br/>
if (_currentProfile.ForceGrayscale) {<br/>
vidarManager.OutputMode = VidarAPIManager.GRAYSCALE_8;<br/>
}<br/>
}<br/>
else {<br/>
throw new Exception("Scanner does not support 8-, 12- or 16-bit depths.");<br/>
}<br/>
}<br/>
else {<br/>
if (has8Bit) { // use 8-bit if available<br/>
vidarManager.CurrentBitDepth = 8;<br/>
vidarManager.BitDepthBytes = 1;<br/>
if (_currentProfile.ForceGrayscale) {<br/>
vidarManager.OutputMode = VidarAPIManager.GRAYSCALE_8;<br/>
}<br/>
}<br/>
else if (has16Bit) { // otherwise use 16-bit if available<br/>
vidarManager.CurrentBitDepth = 16;<br/>
vidarManager.BitDepthBytes = 2;<br/>
if (_currentProfile.ForceGrayscale) {<br/>
vidarManager.OutputMode = VidarAPIManager.GRAYSCALE_16;<br/>
}<br/>
}<br/>
else if (has12Bit) {<br/>
vidarManager.CurrentBitDepth = 12;<br/>
vidarManager.BitDepthBytes = 2;<br/>
if (_currentProfile.ForceGrayscale) {<br/>
vidarManager.OutputMode = VidarAPIManager.GRAYSCALE_12;<br/>
}<br/>
}<br/>
else {<br/>
throw new Exception("Scanner does not support 8-, 12- or 16-bit depths.");<br/>
}<br/>
}<br/>
<br/>
// set resolution<br/>
ushort highestSupportedResolution = 0;<br/>
for (int i = 0; i < vidarManager.ResolutionsCount; i++) {<br/>
if (vidarManager.Resolutions > highestSupportedResolution) {<br/>
highestSupportedResolution = vidarManager.Resolutions;<br/>
}<br/>
}<br/>
vidarManager.XResolution = highestSupportedResolution;<br/>
vidarManager.YResolution = highestSupportedResolution;
// set scan mode<br/>
//vidarManager.ScanMode = VidarAPIManager.SM_SOFTSENSOR | VidarAPIManager.SM_LINES;<br/>
vidarManager.ScanMode = VidarAPIManager.SM_SOFTSENSOR | VidarAPIManager.SM_LINES;
// set bit order<br/>
vidarManager.BitOrder = VidarAPIManager.BO_MOTOROLA;
// set avgCount<br/>
vidarManager.AvgCount = 1;
// set trans table number<br/>
vidarManager.TransTableNum = vidarManager.LocationZeroIsLog ? VidarAPIManager.TRANS_TABLE_ZERO_LOG : VidarAPIManager.TRANS_TABLE_ZERO_LINEAR;
// set pixel width to auto detect<br/>
vidarManager.UseDetectedWidth = true;<br/>
vidarManager.PixelWidth = (ushort)(vidarManager.XResolution * 14);<br/>
vidarManager.PixelLength = (uint)(vidarManager.YResolution * 17);
}<br/>
else {<br/>
throw new Exception(string.Format("Failed to retrieve Vidar Scanner information. Vidar Error Code: {0}, Error Message: {1}", vidarManager.ErrorCode.ToString(), vidarManager.ErrorMessage));<br/>
}<br/>
}<br/>
else {<br/>
throw new Exception(string.Format("Failed to find a Vidar Scanner. Vidar Error Code: {0}, Error Message: {1}", vidarManager.ErrorCode.ToString(), vidarManager.ErrorMessage));<br/>
}<br/>
}<br/>
catch (Exception ex) {<br/>
throw new Exception(Strings.VidarEnvironmentSetupFailed, ex);<br/>
}<br/>
}
Now, I can get the calls to lookForDigitizer() and getDigitizerInformation() and getScanningError() to work fine, but when I call ScanImage() the code gets stuck when it calls VidarScanning.ScanX86_X64() and never returns. The scanner even appears to be
scanning, but it never returns from the method. Do you think its a problem with some of the datatypes Ive used in the C# code? Or maybe the image buffer was created improperly? Any help would be greatly appreciated. Im pulling my hair out trying to get
this working. <hr class="sig Aaron Sellers

View the full article
 
Back
Top