1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639 |
- // ============================================================================
- // Copyright (c) 2010 IOSS GmbH
- // All Rights Reserved.
- // ============================================================================
- // ============================================================================
- //
- // Wid110Lib - WID110 C# based library
- //
- // ============================================================================
- //
- // File: Wid110Lib.cs Type: Implementation
- //
- // Date: 05.02.2010 Last Change: 09.11.2011
- //
- // Author: Thomas M. Schlageter
- // Silvio Robel
- //
- // Methods: Wid110Lib - constructor
- // ~Wid110Lib - destructor
- //
- // FCreateDll - create WID110Lib instance
- // FDestroyDll - destroy WID110Lib instance
- // FExit - terminate connection and exit
- // FGetCodeQualityBCR - get code qualitiy for BCR codes
- // FGetCodeQualityDMR - get code qualitiy for DMR codes
- // FGetCodeQualityLast - get code qualitiy for LAST code
- // FGetCodeQualityOCR - get code qualitiy for OCR codes
- // FGetErrorDescription - get error description
- // FGetLastError - get the last error number
- // FGetVersionParam - return sensor/interface version
- // FGetVersion - return library version
- // FGetWaferId - get the last BCR/OCR decode result
- // FInit - initialize library and connect
- // FIsInitialized - check for initialized state
- // FLiveGetImage - take single image with parameters
- // FLiveRead - perform a live read
- // FLoadRecipes - load parameters by sending a file
- // FLoadRecipesToSlot - load parameters by sending a file
- // FProcessGetImage - get image from process trigger
- // FProcessRead - perform a process read
- // FSwitchOverlay - switch overlay on/off
- // FGetCodeTime - get certain time parameter
- //
- //
- // Auxiliary methods:
- //
- // getErrno - get error number
- // getLastExcp - get the last exception message
- // getReadOK - get result read state
- // getTmpImage - return temporary image name
- // isException - return exception state
- //
- // ============================================================================
- using System;
- using System.IO;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Drawing;
- using System.Reflection;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Net.NetworkInformation;
- using System.Drawing.Imaging;
- namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.OcrReaders.IOSS
- {
- public class Wid110Lib : IDisposable
- {
- #region [ Private Fields ]
- // ====================================================================
- /// <summary>
- /// Private member variables.
- /// </summary>
- // ====================================================================
- private string version;
- private string versParam;
- private string tmpImage = Wid110LibConst.tmpImage;
- private string lastExcp;
- private int errno = Wid110LibConst.ecNone;
- private int readOK = Wid110LibConst.rcError;
- private IntPtr dll;
- #endregion
- #region [ DLL Method Prototypes ]
- // ====================================================================
- /// <summary>
- /// Imported method prototypes.
- /// </summary>
- // ====================================================================
- [DllImport("wid110Lib.dll")]
- private static extern IntPtr FuncCreateDll();
- [DllImport("wid110Lib.dll")]
- private static extern int FuncDestroyDll(IntPtr objptr);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncInit(IntPtr objptr,
- string cpIPAddress);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncIsInitialized(IntPtr objptr);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncCheckConnection(IntPtr objptr,
- string cpIPAddress,
- int timeout);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetVersionParam(IntPtr objptr,
- StringBuilder cVersion,
- int nMaxLen);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetVersion(IntPtr objptr,
- StringBuilder cVersion,
- int nMaxLen);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncSwitchOverlay(IntPtr objptr,
- int bOnOff);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncExit(IntPtr objptr);
- // exchange parameter functions
- [DllImport("wid110Lib.dll")]
- private static extern int FuncSetImageCapture(IntPtr objptr, IntPtr pCapture);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetImageCapture(IntPtr objptr, IntPtr pCapture);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncSetParamOCR(IntPtr objptr, IntPtr pOcr, IntPtr capture);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetParamOCR(IntPtr objptr, IntPtr pOcr, IntPtr capture);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncSetParamBCR(IntPtr objptr, IntPtr pBcr, IntPtr capture);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetParamBCR(IntPtr objptr, IntPtr pBcr, IntPtr capture);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncSetParamDMR(IntPtr objptr, IntPtr pDmr, IntPtr capture);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetParamDMR(IntPtr objptr, IntPtr pDmr, IntPtr capture);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncLiveGetImage(IntPtr objptr,
- string cpFileName,
- int nChannel,
- int nIntensity,
- int nColor);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncLiveRead(IntPtr objptr);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncLiveGetImageRead(IntPtr objptr,
- string cpFileName,
- int nChannel,
- int nIntensity,
- int nColor);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncProcessRead(IntPtr objptr);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncProcessGetImage(IntPtr objptr,
- string cpFileName,
- int nTypeImage);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetWaferId(IntPtr objptr,
- StringBuilder cReadId,
- int nMaxLen,
- IntPtr bReadSuccessful);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetCodeQualityOCR(IntPtr objptr,
- IntPtr pnQuality);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetCodeQualityBCR(IntPtr objptr,
- IntPtr pnQuality);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetCodeQualityDMR(IntPtr objptr,
- IntPtr pnQuality);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetCodeQualityLast(IntPtr objptr,
- IntPtr pnQuality);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetCodeTime(IntPtr objptr,
- IntPtr pnTime);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncLoadRecipes(IntPtr objptr,
- string cpFilePath,
- string cpFilename);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncLoadRecipesToSlot(IntPtr objptr,
- string cpFilePath,
- string cpFilename,
- int nSlot);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncSaveJob(IntPtr objptr,
- string cpFilePath,
- string cpFilename);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetLastError(IntPtr objptr);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetErrorDescription(IntPtr objptr,
- int nError,
- StringBuilder strText,
- int nTextLength);
- // teachin functions
- [DllImport("wid110Lib.dll")]
- private static extern int FuncTeachingBCR(IntPtr objptr);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncTeachingOCR(IntPtr objptr);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncTeachingDMR(IntPtr objptr);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncConfigureBCR(IntPtr objptr, string cName);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncConfigureOCR(IntPtr objptr, string cName);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncConfigureDMR(IntPtr objptr, string cName);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncConfigureDelete(IntPtr objptr, string cName);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncConfigurePreset(IntPtr objptr, string cName);
- // image handling functions
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetImageRawData(IntPtr objptr, IntPtr pImageData);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetImageXSize(IntPtr objptr, IntPtr pXSize);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetImageYSize(IntPtr objptr, IntPtr pYSize);
- // further function for change configuration
- [DllImport("wid110Lib.dll")]
- private static extern int FuncSetFineScan(IntPtr objptr, IntPtr pMode);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetFineScan(IntPtr objptr, IntPtr pMode);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetFontName(IntPtr objptr, StringBuilder fName, int index);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetConfigurationName(IntPtr objptr, StringBuilder cName, int index);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncSaveJobToReader(IntPtr objptr);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncGetROI(IntPtr objptr, string cName, IntPtr pWnd, IntPtr pCapture);
- [DllImport("wid110Lib.dll")]
- private static extern int FuncSetROI(IntPtr objptr, string cName, IntPtr pWnd, IntPtr pCapture);
- #endregion
- // ====================================================================
- /// <summary>
- /// Constructor: Initialize WID C# Library.
- /// </summary>
- // ====================================================================
- public Wid110Lib()
- {
- dll = FCreateDll();
- }
- #region IDisposable Support
- private bool disposedValue = false; // To detect redundant calls
- // ====================================================================
- /// <summary>
- /// Dispose: Free WID C# Library.
- /// </summary>
- // ====================================================================
- protected virtual void Dispose(bool disposing)
- {
- if (!disposedValue)
- {
- if (disposing)
- {
- // Dispose managed state (managed objects).
- }
- FDestroyDll(dll);
- dll = IntPtr.Zero;
- disposedValue = true;
- }
- }
- // ====================================================================
- /// <summary>
- /// Finalizer: Free WID C# Library (Internal Implementation)
- /// </summary>
- // ====================================================================
- ~Wid110Lib()
- {
- // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
- Dispose(false);
- }
- // This code added to correctly implement the disposable pattern.
- public void Dispose()
- {
- // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- #endregion
- // ====================================================================
- /// <summary>
- /// Create WID C# Library instance.
- /// </summary>
- /// <return> DLL if created, NULL upon error.</return>
- // ====================================================================
- private IntPtr FCreateDll()
- {
- IntPtr d = IntPtr.Zero;
- lastExcp = "";
- errno = Wid110LibConst.ecNone;
- try
- {
- d = FuncCreateDll();
- }
- catch (Exception e)
- {
- lastExcp = e.ToString();
- }
- return d;
- }
- // ====================================================================
- /// <summary>
- /// Destroy WID C# Library instance.
- /// </summary>
- /// <param name="dll"> DLL to destroy.</param>
- /// <return> true if destroyed.</return>
- // ====================================================================
- private bool FDestroyDll(IntPtr dll)
- {
- try
- {
- if (HandleDLLStatus(FuncDestroyDll(dll))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Initialize library and connect to IP.
- /// </summary>
- /// <param name="ip"> IP to connect.</param>
- /// <return> true if connected.</return>
- // ====================================================================
- public bool FInit(string ip)
- {
- try
- {
- Ping PingHandler = new Ping();
- PingReply reply = PingHandler.Send(ip, 1000);
- if (reply.Status == IPStatus.Success)
- {
- if (HandleDLLStatus(FuncInit(dll, ip))) return true;
- }
- else throw new IOException("IP Address " + ip + " not reachable!");
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Check library state.
- /// </summary>
- /// <return> true if initialized.</return>
- // ====================================================================
- public bool FIsInitialized()
- {
- try
- {
- if (HandleDLLStatus(FuncIsInitialized(dll)))
- return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Check reader connection
- /// </summary>
- /// <param name="ip">Reader IP Address</param>
- /// <param name="timeout_s">Timeout in seconds</param>
- /// <return> true if connected.</return>
- // ====================================================================
- public bool FCheckConnection(string ip, int timeout_s)
- {
- try
- {
- if (HandleDLLStatus(FuncCheckConnection(dll, ip, timeout_s))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Terminate connection.
- /// </summary>
- /// <return> true if disconnected.</return>
- // ====================================================================
- public bool FExit()
- {
- try
- {
- if (HandleDLLStatus(FuncExit(dll)))
- return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Set the image acquisition parameter to the reader.
- /// </summary>
- /// <param name="capture">Image capturing parameters. <see cref="WID_CAPTURE"/></param>
- /// <returns>true if no error.</returns>
- // ====================================================================
- public bool FSetImageCapture(WID_CAPTURE capture)
- {
- IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
- try
- {
- Marshal.StructureToPtr(capture, ptr, false);
- if (HandleDLLStatus(FuncSetImageCapture(dll, ptr))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally
- {
- Marshal.FreeHGlobal(ptr);
- }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Get the image acquisition parameter from reader.
- /// </summary>
- /// <returns>Image capturing parameters. <see cref="WID_CAPTURE"/></returns>
- // ====================================================================
- public WID_CAPTURE FGetImageCapture()
- {
- WID_CAPTURE capture = new WID_CAPTURE();
- IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
- try
- {
- Marshal.StructureToPtr(capture, ptr, false);
- if (HandleDLLStatus(FuncGetImageCapture(dll, ptr)))
- {
- capture = (WID_CAPTURE)Marshal.PtrToStructure(ptr, typeof(WID_CAPTURE));
- return capture;
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally
- {
- Marshal.FreeHGlobal(ptr);
- }
- return capture;
- }
- // ====================================================================
- /// <summary>
- /// Get library version.
- /// </summary>
- /// <param name="v"> WID110 library version.</param>
- // ====================================================================
- public string FGetVersion()
- {
- try
- {
- version = "";
- StringBuilder sb = new StringBuilder("", Wid110LibConst.versLenCS);
- if (HandleDLLStatus(FuncGetVersion(dll, sb, Wid110LibConst.versLenC)))
- {
- version = sb.ToString();
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return version;
- }
- // ====================================================================
- /// <summary>
- /// Get sensor interface version.
- /// </summary>
- /// <return> WID110 interface version.</return>
- // ====================================================================
- public string FGetVersionParam()
- {
- try
- {
- versParam = "";
- StringBuilder sb = new StringBuilder("", Wid110LibConst.versLenCS);
- if (HandleDLLStatus(FuncGetVersionParam(dll, sb, Wid110LibConst.versLenC)))
- {
- versParam = sb.ToString();
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return versParam;
- }
- // ====================================================================
- /// <summary>
- /// Change overlay flag.
- /// </summary>
- /// <param name="o"> overlay flag.</param>
- /// <return> true if no error.</return>
- // ====================================================================
- public bool FSwitchOverlay(bool o)
- {
- try
- {
- if (HandleDLLStatus(FuncSwitchOverlay(dll, Convert.ToInt32(o)))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Perform a live read using the temporary settings from an
- /// earlier 'FLiveGetImage()' call.
- /// </summary>
- /// <return> true if done.</return>
- // ====================================================================
- public bool FLiveRead()
- {
- try
- {
- if (HandleDLLStatus(FuncLiveRead(dll))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Perform a live read using the given parameters, and decode.
- /// </summary>
- /// <param name="name"> name to save image to.</param>
- /// <param name="channel"> illumination channel.</param>
- /// <param name="intensity"> illumination intensity.</param>
- /// <param name="color"> illumination color.</param>
- /// <return> true if done.</return>
- // ====================================================================
- public bool FLiveGetImageRead(string name,
- int channel,
- int intensity,
- int color)
- {
- try
- {
- if (HandleDLLStatus(FuncLiveGetImageRead(dll, name, channel, intensity, color))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Perform a process read..
- /// </summary>
- /// <return> true if done.</return>
- // ====================================================================
- public bool FProcessRead()
- {
- try
- {
- if (HandleDLLStatus(FuncProcessRead(dll))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Get code quality for OCR codes.
- /// </summary>
- /// <return> code quality, or rsltNoCodeQuality upon failure.</return>
- // ====================================================================
- public int FGetCodeQualityOCR()
- {
- IntPtr q = Marshal.AllocHGlobal(sizeof(int));
- int rc = Wid110LibConst.rsltNoCodeQuality;
- try
- {
- if (HandleDLLStatus(FuncGetCodeQualityOCR(dll, q)))
- {
- rc = Marshal.ReadInt32(q);
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally { Marshal.FreeHGlobal(q); }
- return rc;
- }
- // ====================================================================
- /// <summary>
- /// Get code quality for BCR codes.
- /// </summary>
- /// <return> code quality, or rsltNoCodeQuality upon failure.</return>
- // ====================================================================
- public int FGetCodeQualityBCR()
- {
- IntPtr q = Marshal.AllocHGlobal(sizeof(int));
- int rc = Wid110LibConst.rsltNoCodeQuality;
- try
- {
- if (HandleDLLStatus(FuncGetCodeQualityBCR(dll, q)))
- {
- rc = Marshal.ReadInt32(q);
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally { Marshal.FreeHGlobal(q); }
- return rc;
- }
- // ====================================================================
- /// <summary>
- /// Get code quality for DMR codes.
- /// </summary>
- /// <return> code quality, or rsltNoCodeQuality upon failure.</return>
- // ====================================================================
- public int FGetCodeQualityDMR()
- {
- IntPtr q = Marshal.AllocHGlobal(sizeof(int));
- int rc = Wid110LibConst.rsltNoCodeQuality;
- try
- {
- if (HandleDLLStatus(FuncGetCodeQualityDMR(dll, q)))
- {
- rc = Marshal.ReadInt32(q);
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally { Marshal.FreeHGlobal(q); }
- return rc;
- }
- // ====================================================================
- /// <summary>
- /// Get code quality for LAST code.
- /// </summary>
- /// <return> code quality, or rsltNoCodeQuality upon failure.</return>
- // ====================================================================
- public int FGetCodeQualityLast()
- {
- IntPtr q = Marshal.AllocHGlobal(sizeof(int));
- int rc = Wid110LibConst.rsltNoCodeQuality;
- try
- {
- if (HandleDLLStatus(FuncGetCodeQualityLast(dll, q)))
- {
- rc = Marshal.ReadInt32(q);
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally { Marshal.FreeHGlobal(q); }
- return rc;
- }
- // ====================================================================
- /// <summary>
- /// Retrieve image from last process trigger.
- /// </summary>
- /// <param name="name"> file name to save image.</param>
- /// <param name="type"> 'pvImgBest' or 'pvImgAll'.</param>
- /// <return> true if OK.</return>
- // ====================================================================
- public bool FProcessGetImage(string name,
- int type)
- {
- try
- {
- if (HandleDLLStatus(FuncProcessGetImage(dll, name, type))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Get the last BCR/OCR/DMR decode result.
- /// </summary>
- /// <return> the latest read result.</return>
- // ====================================================================
- public string FGetWaferId()
- {
- StringBuilder sb = new StringBuilder(Wid110LibConst.rsltERROR, Wid110LibConst.rsltLenCS);
- string res = Wid110LibConst.rsltERROR;
- IntPtr pok = Marshal.AllocHGlobal(1);
- try
- {
- if (HandleDLLStatus(FuncGetWaferId(dll, sb, Wid110LibConst.rsltLenC, pok)))
- {
- readOK = Marshal.ReadByte(pok, 0);
- if (1 == readOK)
- res = Wid110LibConst.rsltREAD + sb.ToString();
- else
- res = Wid110LibConst.rsltNOREAD + sb.ToString();
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally
- {
- Marshal.FreeHGlobal(pok);
- }
- return res;
- }
- // ====================================================================
- /// <summary>
- /// Take a single image using the given parameters.
- /// </summary>
- /// <param name="name"> name to save image to.</param>
- /// <param name="channel"> illumination channel.</param>
- /// <param name="intensity"> illumination intensity.</param>
- /// <param name="color"> illumination color.</param>
- /// <return> true if OK.</return>
- // ====================================================================
- public bool FLiveGetImage(string name,
- int channel,
- int intensity,
- int color)
- {
- try
- {
- if (HandleDLLStatus(FuncLiveGetImage(dll, name, channel, intensity, color))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Saves reader configuration into a *.wid file on the pc
- /// </summary>
- /// <param name="jobFilePath"> Full path to file.</param>
- /// <return> true if OK.</return>
- // ====================================================================
- public bool FSaveJob(string jobFilePath)
- {
- string path = Path.GetDirectoryName(jobFilePath);
- string file = Path.GetFileName(jobFilePath);
- try
- {
- if (HandleDLLStatus(FuncSaveJob(dll, path, file))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Load process parameter file
- /// </summary>
- /// <param name="jobFilePath"> Full path of job file</param>
- /// <return> true if OK.</return>
- // ====================================================================
- public bool FLoadRecipes(string jobFilePath)
- {
- try
- {
- if (!File.Exists(jobFilePath)) throw new FileNotFoundException(jobFilePath);
- string path = Path.GetDirectoryName(jobFilePath);
- string file = Path.GetFileName(jobFilePath);
- if (HandleDLLStatus(FuncLoadRecipes(dll, path, file))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Load parameters to a certain parameter slot (if applicable)
- /// </summary>
- /// <param name="jobFilePathpath"> Full path to job file.</param>
- /// <param name="slot"> slot (only for ocf and led files).</param>
- /// <return> true if valid.</return>
- // ====================================================================
- public bool FLoadRecipesToSlot(string jobFilePath,
- int slot)
- {
- try
- {
- if (!File.Exists(jobFilePath)) throw new FileNotFoundException(jobFilePath);
- string path = Path.GetDirectoryName(jobFilePath);
- string file = Path.GetFileName(jobFilePath);
- if (HandleDLLStatus(FuncLoadRecipesToSlot(dll, path, file, slot))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Save Job to Reader
- /// </summary>
- /// <return>true if valid.</return>
- // ====================================================================
- public bool FSaveJobToReader()
- {
- try
- {
- if (HandleDLLStatus(FuncSaveJobToReader(dll))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Get code time parameter
- /// </summary>
- /// <return> overall process time or rsltNoCodeTime upon failure.</return>
- // ====================================================================
- public int FGetCodeTime()
- {
- IntPtr q = Marshal.AllocHGlobal(sizeof(int));
- int rc = Wid110LibConst.rsltNoCodeTime;
- try
- {
- if (HandleDLLStatus(FuncGetCodeTime(dll, q)))
- {
- rc = Marshal.ReadInt32(q);
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally { Marshal.FreeHGlobal(q); }
- return rc;
- }
- // ====================================================================
- /// <summary>
- /// Get error description.
- /// </summary>
- /// <param name="eno"> error number to translate.</param>
- /// <return> error description.</return>
- // ====================================================================
- public string FGetErrorDescription(int eno)
- {
- StringBuilder sb = new StringBuilder(Wid110LibConst.errDESC, Wid110LibConst.errLen);
- int len = Wid110LibConst.errLen;
- try // if lib handle is invalid, error text is generated as well
- {
- if (FuncGetErrorDescription(dll, eno, sb, len) == Wid110LibConst.rcNoError)
- return sb.ToString();
- }
- catch (Exception e)
- {
- lastExcp = e.ToString();
- }
- return Wid110LibConst.errDESC;
- }
- // ====================================================================
- /// <summary>
- /// Get last error number.
- /// </summary>
- /// <return> last error number.</return>
- // ====================================================================
- public int FGetLastError()
- {
- try
- {
- return FuncGetLastError(dll); // get last internal lib error
- }
- catch (Exception e)
- {
- lastExcp = e.ToString();
- }
- return errno;
- }
- // ====================================================================
- /// <summary>
- /// Handle error from Wid110.dll (Internal use only)
- /// </summary>
- /// <param name="status">return status from DLL Function</param>
- /// <returns></returns>
- // ====================================================================
- private bool HandleDLLStatus(int status)
- {
- lastExcp = "";
- switch (status)
- {
- case Wid110LibConst.rcError:
- errno = FuncGetLastError(dll);
- return false;
- case Wid110LibConst.rcInvObj:
- errno = Wid110LibConst.ecInvObj;
- return false;
- case Wid110LibConst.rcNoError:
- errno = Wid110LibConst.ecNone;
- return true;
- }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Get the last error number.
- /// </summary>
- /// <return> last error number.</return>
- // ====================================================================
- public int getErrno()
- {
- return errno;
- }
- // ====================================================================
- /// <summary>
- /// Get the result read state.
- /// </summary>
- /// <return> result read state.</return>
- // ====================================================================
- public int getReadOK()
- {
- return readOK;
- }
- // ====================================================================
- /// <summary>
- /// Get the last exception message.
- /// </summary>
- /// <return> last exception message.</return>
- // ====================================================================
- public string getLastExcp()
- {
- return lastExcp;
- }
- // ====================================================================
- /// <summary>
- /// Get temporary image name.
- /// </summary>
- /// <return> temporary image name.</return>
- // ====================================================================
- public string getTmpImage()
- {
- return tmpImage;
- }
- // ====================================================================
- /// <summary>
- /// Get exception state.
- /// </summary>
- /// <return> true if exception.</return>
- // ====================================================================
- public bool isException()
- {
- return lastExcp.Length != 0;
- }
- // ====================================================================
- /// <summary>
- /// Check error for last function call
- /// </summary>
- /// <param name="errorMesage">Return error message from Wid110Lib.dll</param>
- /// <returns>True if error raised.</returns>
- // ====================================================================
- public bool CheckError(out string errorMesage)
- {
- errorMesage = string.Empty;
- if (isException())
- {
- errorMesage = getLastExcp();
- return true;
- }
- if (errno == Wid110LibConst.ecNone) return false;
- errorMesage = FGetErrorDescription(errno);
- return true;
- }
- // ====================================================================
- /// <summary>
- /// Return font name on selected index
- /// </summary>
- /// <param name="index">Font index. [0 - 7]</param>
- /// <returns>Font name</returns>
- // ====================================================================
- public string FGetFontName(int index)
- {
- try
- {
- StringBuilder sb = new StringBuilder("", 255);
- if (HandleDLLStatus(FuncGetFontName(dll, sb, index)))
- {
- return sb.ToString();
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return string.Empty;
- }
- // ====================================================================
- /// <summary>
- /// Set fine scanning mode flag
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- // ====================================================================
- public bool FSetFineScan(bool value)
- {
- IntPtr q = Marshal.AllocHGlobal(sizeof(Int32));
- try
- {
- Marshal.WriteInt32(q, Convert.ToInt32(value));
- if (HandleDLLStatus(FuncSetFineScan(dll, q))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally { Marshal.FreeHGlobal(q); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Get fine scanning mode flag
- /// </summary>
- /// <returns>True if fine scanning enabled, return false if error</returns>
- // ====================================================================
- public bool FGetFineScan()
- {
- IntPtr q = Marshal.AllocHGlobal(sizeof(Int32));
- try
- {
- if (HandleDLLStatus(FuncGetFineScan(dll, q)))
- {
- return Convert.ToBoolean(Marshal.ReadInt32(q));
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally { Marshal.FreeHGlobal(q); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Retrieve name of saved configuration
- /// </summary>
- /// <param name="index">Slot index [1 -99]</param>
- /// <returns>Configuration Name</returns>
- // ====================================================================
- public string FGetConfigurationName(int index)
- {
- try
- {
- StringBuilder sb = new StringBuilder("", 255);
- if (HandleDLLStatus(FuncGetConfigurationName(dll, sb, index)))
- {
- return sb.ToString();
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return string.Empty;
- }
- // ====================================================================
- /// <summary>
- /// Set OCR Parameter
- /// </summary>
- /// <param name="ocr">OCR Parameters</param>
- /// <param name="capture">Image Capture Parameters</param>
- /// <returns>true if no error</returns>
- // ====================================================================
- public bool FSetParamOCR(WID_OCR ocr, WID_CAPTURE capture)
- {
- WID_OCR_Unsafe unsafeOcr = ocr.ToUnsafeStruct();
- IntPtr ptrOcr = Marshal.AllocHGlobal(Marshal.SizeOf(unsafeOcr) + 1);
- IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
- try
- {
- Marshal.StructureToPtr(capture, ptrCapture, false);
- Marshal.StructureToPtr(unsafeOcr, ptrOcr, false);
- if (HandleDLLStatus(FuncSetParamOCR(dll, ptrOcr, ptrCapture))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally
- {
- Marshal.FreeHGlobal(ptrCapture);
- Marshal.FreeHGlobal(ptrOcr);
- }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Get OCR Parameter
- /// </summary>
- /// <param name="ocr">OCR Parameters</param>
- /// <param name="capture">Image Capture Parameters</param>
- /// <returns>true if no error</returns>
- // ====================================================================
- public bool FGetParamOCR(out WID_OCR ocr, out WID_CAPTURE capture)
- {
- WID_OCR_Unsafe unsafeOcr = new WID_OCR_Unsafe();
- IntPtr ptrOcr = Marshal.AllocHGlobal(Marshal.SizeOf(unsafeOcr) + 1);
- capture = new WID_CAPTURE();
- IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
- try
- {
- Marshal.StructureToPtr(unsafeOcr, ptrOcr, false);
- Marshal.StructureToPtr(capture, ptrCapture, false);
- if (HandleDLLStatus(FuncGetParamOCR(dll, ptrOcr, ptrCapture)))
- {
- unsafeOcr = (WID_OCR_Unsafe)Marshal.PtrToStructure(ptrOcr, typeof(WID_OCR_Unsafe));
- ocr = new WID_OCR(unsafeOcr);
- capture = (WID_CAPTURE)Marshal.PtrToStructure(ptrCapture, typeof(WID_CAPTURE));
- return true;
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally
- {
- Marshal.FreeHGlobal(ptrOcr);
- Marshal.FreeHGlobal(ptrCapture);
- }
- ocr = new WID_OCR();
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Set BCR Parameter
- /// </summary>
- /// <param name="bcr">BCR Parameters</param>
- /// <param name="capture">Image Capture Parameters</param>
- /// <returns>true if no error</returns>
- // ====================================================================
- public bool FSetParamBCR(WID_BCR bcr, WID_CAPTURE capture)
- {
- WID_BCR_Unsafe unsafeBcr = bcr.ToUnsafeStruct();
- IntPtr ptrBcr = Marshal.AllocHGlobal(Marshal.SizeOf(unsafeBcr) + 1);
- IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
- try
- {
- Marshal.StructureToPtr(capture, ptrCapture, false);
- Marshal.StructureToPtr(unsafeBcr, ptrBcr, false);
- if (HandleDLLStatus(FuncSetParamBCR(dll, ptrBcr, ptrCapture))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally
- {
- Marshal.FreeHGlobal(ptrCapture);
- Marshal.FreeHGlobal(ptrBcr);
- }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Get BCR Parameter
- /// </summary>
- /// <param name="bcr">BCR Parameters</param>
- /// <param name="capture">Image Capture Parameters</param>
- /// <returns>true if no error</returns>
- // ====================================================================
- public bool FGetParamBCR(out WID_BCR bcr, out WID_CAPTURE capture)
- {
- WID_BCR_Unsafe unsafebcr = new WID_BCR_Unsafe();
- IntPtr ptrBcr = Marshal.AllocHGlobal(Marshal.SizeOf(unsafebcr) + 1);
- capture = new WID_CAPTURE();
- IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
- try
- {
- Marshal.StructureToPtr(unsafebcr, ptrBcr, false);
- Marshal.StructureToPtr(capture, ptrCapture, false);
- if (HandleDLLStatus(FuncGetParamBCR(dll, ptrBcr, ptrCapture)))
- {
- unsafebcr = (WID_BCR_Unsafe)Marshal.PtrToStructure(ptrBcr, typeof(WID_BCR_Unsafe));
- bcr = new WID_BCR(unsafebcr);
- capture = (WID_CAPTURE)Marshal.PtrToStructure(ptrCapture, typeof(WID_CAPTURE));
- return true;
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally
- {
- Marshal.FreeHGlobal(ptrBcr);
- Marshal.FreeHGlobal(ptrCapture);
- }
- bcr = new WID_BCR();
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Set DMR Parameter
- /// </summary>
- /// <param name="dmr">DMR Parameters</param>
- /// <param name="capture">Image Capture Parameters</param>
- /// <returns>true if no error</returns>
- // ====================================================================
- public bool FSetParamDMR(WID_DMR dmr, WID_CAPTURE capture)
- {
- WID_DMR_Unsafe usafeDmr = dmr.ToUnsafeStruct();
- IntPtr ptrDmr = Marshal.AllocHGlobal(Marshal.SizeOf(usafeDmr) + 1);
- IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
- try
- {
- Marshal.StructureToPtr(capture, ptrCapture, false);
- Marshal.StructureToPtr(usafeDmr, ptrDmr, false);
- if (HandleDLLStatus(FuncSetParamDMR(dll, ptrDmr, ptrCapture))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally
- {
- Marshal.FreeHGlobal(ptrCapture);
- Marshal.FreeHGlobal(ptrDmr);
- }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Get DMR Parameter
- /// </summary>
- /// <param name="dmr">DMR Parameters</param>
- /// <param name="capture">Image Capture Parameters</param>
- /// <returns>true if no error</returns>
- // ====================================================================
- public bool FGetParamDMR(out WID_DMR dmr, out WID_CAPTURE capture)
- {
- WID_DMR_Unsafe unsafedmr = new WID_DMR_Unsafe();
- IntPtr ptrDmr = Marshal.AllocHGlobal(Marshal.SizeOf(unsafedmr) + 1);
- capture = new WID_CAPTURE();
- IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
- try
- {
- Marshal.StructureToPtr(unsafedmr, ptrDmr, false);
- Marshal.StructureToPtr(capture, ptrCapture, false);
- if (HandleDLLStatus(FuncGetParamDMR(dll, ptrDmr, ptrCapture)))
- {
- unsafedmr = (WID_DMR_Unsafe)Marshal.PtrToStructure(ptrDmr, typeof(WID_DMR_Unsafe));
- dmr = new WID_DMR(unsafedmr);
- capture = (WID_CAPTURE)Marshal.PtrToStructure(ptrCapture, typeof(WID_CAPTURE));
- return true;
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally
- {
- Marshal.FreeHGlobal(ptrDmr);
- Marshal.FreeHGlobal(ptrCapture);
- }
- dmr = new WID_DMR();
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Set ROI of named configuration
- /// </summary>
- /// <param name="configName">Configuration name</param>
- /// <param name="roi">ROI Parameters</param>
- /// <param name="capture">Image Capture Parameters</param>
- /// <returns>true if no error.</returns>
- // ====================================================================
- public bool FSetROI(string configName, WID_ROI roi, WID_CAPTURE capture)
- {
- IntPtr ptrRoi = Marshal.AllocHGlobal(Marshal.SizeOf(roi) + 1);
- IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
- try
- {
- Marshal.StructureToPtr(roi, ptrRoi, false);
- Marshal.StructureToPtr(capture, ptrCapture, false);
- if (HandleDLLStatus(FuncSetROI(dll, configName, ptrRoi, ptrCapture))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally
- {
- Marshal.FreeHGlobal(ptrCapture);
- Marshal.FreeHGlobal(ptrRoi);
- }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Get ROI of named configuration
- /// </summary>
- /// <param name="configName">Configuration name</param>
- /// <param name="roi">ROI Parameters</param>
- /// <param name="capture">Image Capture Parameters</param>
- /// <returns>true if no error.</returns>
- // ====================================================================
- public bool FGetROI(string configName, out WID_ROI roi, out WID_CAPTURE capture)
- {
- roi = new WID_ROI();
- IntPtr ptrRoi = Marshal.AllocHGlobal(Marshal.SizeOf(roi) + 1);
- capture = new WID_CAPTURE();
- IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
- try
- {
- Marshal.StructureToPtr(roi, ptrRoi, false);
- Marshal.StructureToPtr(capture, ptrCapture, false);
- if (HandleDLLStatus(FuncGetROI(dll, configName, ptrRoi, ptrCapture)))
- {
- roi = (WID_ROI)Marshal.PtrToStructure(ptrRoi, typeof(WID_ROI));
- capture = (WID_CAPTURE)Marshal.PtrToStructure(ptrCapture, typeof(WID_CAPTURE));
- return true;
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally
- {
- Marshal.FreeHGlobal(ptrRoi);
- Marshal.FreeHGlobal(ptrCapture);
- }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Get Image Width
- /// </summary>
- /// <returns>Image Width in pixels</returns>
- // ====================================================================
- public int FGetImageXSize()
- {
- IntPtr q = Marshal.AllocHGlobal(sizeof(Int32));
- try
- {
- if (HandleDLLStatus(FuncGetImageXSize(dll, q)))
- {
- return Convert.ToInt32(Marshal.ReadInt32(q));
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally { Marshal.FreeHGlobal(q); }
- return -1;
- }
- // ====================================================================
- /// <summary>
- /// Get Image Height
- /// </summary>
- /// <returns>Image height in pixels</returns>
- // ====================================================================
- public int FGetImageYSize()
- {
- IntPtr q = Marshal.AllocHGlobal(sizeof(Int32));
- try
- {
- if (HandleDLLStatus(FuncGetImageYSize(dll, q)))
- {
- return Convert.ToInt32(Marshal.ReadInt32(q));
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally { Marshal.FreeHGlobal(q); }
- return -1;
- }
- // ====================================================================
- /// <summary>
- /// Delete all configuration with the name.
- /// </summary>
- /// <param name="configName">Configuration Name</param>
- /// <returns>true if no error</returns>
- // ====================================================================
- public bool FConfigureDelete(string configName)
- {
- try
- {
- if (HandleDLLStatus(FuncConfigureDelete(dll, configName))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Preset Configuration with matching name.
- /// </summary>
- /// <param name="configName">Configuration name, leave empty to reset cycle</param>
- /// <returns>true if no error</returns>
- // ====================================================================
- public bool FConfigurePreset(string configName = "")
- {
- try
- {
- if (HandleDLLStatus(FuncConfigurePreset(dll, configName))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Tune OCR
- /// </summary>
- /// <returns>true if no error</returns>
- // ====================================================================
- public bool FTeachingOCR()
- {
- try
- {
- if (HandleDLLStatus(FuncTeachingOCR(dll))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Tune Barcode (NOT IMPLEMENTED YET)
- /// </summary>
- /// <returns>true if no error</returns>
- // ====================================================================
- public bool FTeachingBCR()
- {
- try
- {
- if (HandleDLLStatus(FuncTeachingBCR(dll))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Tune 2D Barcode (NOT IMPLEMENTED YET)
- /// </summary>
- /// <returns>true if no error</returns>
- // ====================================================================
- public bool FTeachingDMR()
- {
- try
- {
- if (HandleDLLStatus(FuncTeachingDMR(dll))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// OCR Configuration steps
- /// </summary>
- /// <param name="configName">null or configuration name</param>
- /// <returns>true if no error</returns>
- // ====================================================================
- public bool FConfigureOCR(string configName = "\0")
- {
- try
- {
- if (HandleDLLStatus(FuncConfigureOCR(dll, configName))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// Barcode Configuration steps. (NOT IMPLEMENTED YET)
- /// </summary>
- /// <param name="configName">null or configuration name</param>
- /// <returns>true if no error</returns>
- // ====================================================================
- public bool FConfigureBCR(string configName = "")
- {
- try
- {
- if (HandleDLLStatus(FuncConfigureBCR(dll, configName))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- // ====================================================================
- /// <summary>
- /// 2D Barcode Configuration steps. (NOT IMPLEMENTED YET)
- /// </summary>
- /// <param name="configName">null or configuration name</param>
- /// <returns>true if no error</returns>
- // ====================================================================
- public bool FConfigureDMR(string configName = "")
- {
- try
- {
- if (HandleDLLStatus(FuncConfigureDMR(dll, configName))) return true;
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- return false;
- }
- public bool FGetImageRawData(out Image image)
- {
- image = null;
- int width = FGetImageXSize();
- int height = FGetImageYSize();
- int imageSize = width * height;
- byte[] imageData = new byte[imageSize];
- IntPtr ptrImage = Marshal.AllocHGlobal(imageData.Length);
- try
- {
- if (HandleDLLStatus(FuncGetImageRawData(dll, ptrImage)))
- {
- Marshal.Copy(ptrImage, imageData, 0, imageSize);
- byte[] flippedData = ImageDataFlipHorizontal(imageData, width, height);
- image = CreateBMP(width, height, flippedData);
- image.Save("BMPDump.bmp");
- image.Dispose();
- return true;
- }
- }
- catch (Exception e) { lastExcp = e.ToString(); }
- finally { Marshal.FreeHGlobal(ptrImage); }
- return false;
- }
- private byte[] ImageDataFlipHorizontal(byte[] i, int w, int h)
- {
- byte[] d = new byte[i.Length];
- for (int r = 0; r < h; r++)
- {
- Array.Copy(i, r * w, d, (h - 1 - r) * w, w);
- }
- return d;
- }
- private Bitmap CreateBMP(int width, int height, byte[] values)
- {
- Bitmap b = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
- ColorPalette ncp = b.Palette;
- for (int i = 0; i < 256; i++)
- ncp.Entries[i] = Color.FromArgb(255, i, i, i);
- b.Palette = ncp;
- var BoundsRect = new Rectangle(0, 0, width, height);
- BitmapData bmpData = b.LockBits(BoundsRect,
- ImageLockMode.WriteOnly,
- b.PixelFormat);
- IntPtr ptr = bmpData.Scan0;
- int bytes = bmpData.Stride * b.Height;
- Marshal.Copy(values, 0, ptr, bytes);
- b.UnlockBits(bmpData);
- return b;
- }
- }
- }
|