Search code examples
c#twainmicr

How can I get MICR string from scanner by using TWAIN driver


I am writting some c# code for extract MICR string from checks.

My scanner is Cannon DR-850M which support MICR reader.

It can read MICR string on it's own scanning program. But I need to make my own by using TWAIN.

I can scan images in my program with TWAIN driver. However, I couldn't find how to get MICR string.

Is there a function to access scanner's MICR reader in TWAIN? or anything else?


I found that MICR data were located in "Extended Image Info". Thanks for answering.

and I got a little more problem.

Here's my code.

// Twain DLL Wrapper
[StructLayout(LayoutKind.Sequential, Pack = 2)]
internal class TwInfo
{                                   // TW_INFO
    public short InfoID;
    public short ItemType;
    public short NumItems;
    public short CondCode;
    public int Item;
}

[StructLayout(LayoutKind.Sequential, Pack = 2)]
internal class TwExtImageInfo
{                                   // TW_EXTIMAGEINFO
    public int NumInfos;
    [MarshalAs(UnmanagedType.Struct, SizeConst = 1)]
    public TwInfo Info;
}

//Application
public void GetExtendedImageInfo()
    {
        TwRC rc;
        TwExtImageInfo exinf = new TwExtImageInfo();

        exinf.NumInfos = 1;
        exinf.Info = new TwInfo();
        exinf.Info.InfoID = (short)TwEI.BARCODETEXT;

        rc = DSexfer(appid, srcds, TwDG.Image, TwDAT.ExtImageInfo, TwMSG.Get, exinf);

        // Here's What I want to know.
        IntPtr itemPtr = new IntPtr(exinf.Info.Item);  
        string str = Marshal.PtrToStringAnsi(itemPtr); // It returns weird value.
    }

// Here's result
exinf.Info.CondCode : 0 (short)
exinf.Info.InfoID   : 4610 (short)
exinf.Info.Item : 36962876 (int)  // what's that?
exinf.Info.ItemType : 12 (short)
exinf.Info.NumItems : 4 (short)
exinf.NumInfos      : 1 (int)

I got these values from TWAIN.

But I don't know what kind of value exinf.Info.Item is

In sample App, It show right MICR characters. But my own returns strange value.

Can I get some help?


Solution


  • SOLVED

    I just missed using GlobalLock for pointer.

    Here's code.

    public string GetExtendedImageInfo()
        {
            TwRC rc;
            TwExtImageInfo exinf = new TwExtImageInfo();
    
            exinf.NumInfos = 1;
            exinf.Info = new TwInfo();
            exinf.Info.InfoID = (short)TwEI.BARCODETEXT;
    
            rc = DSexfer(appid, srcds, TwDG.Image, TwDAT.ExtImageInfo, TwMSG.Get, exinf);
    
            StringBuilder strItem = new StringBuilder(255);
            IntPtr itemPtr = new IntPtr(exinf.Info.Item);
            IntPtr dataPtr = GlobalLock(itemPtr);
            string str = Marshal.PtrToStringAnsi(dataPtr);
            GlobalUnlock(itemPtr);
            GlobalFree(itemPtr);
    
            return str;
        }