Monday, 7 February 2011

AX - Scan a document

There are a few ways to scan a document in AX.
Best way is to use Windows Image Acquisition (WIA). This comes standard from Windows Vista and up. Previous version you had to download "Windows Image Acquisition Automation Library v2.0" from Microsoft.

Without getting into too much detail. Here is the code:
static void scanDocument(Args _args)
{
    COM               commonDialog;
    COM               imageFile;
    str               wiaFormatJPEG     = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
    str               wiaFormatBMP      = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";
    str               wiaFormatPNG      = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}";
    str               wiaFormatGIF      = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}";
    str               wiaFormatTIFF     = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}";
    InteropPermission perm;
    ;

    // Set code access permission to help protect the use of the
    // COM object.
    perm = new InteropPermission(InteropKind::ComInterop);
    if (perm == null)
    {
        return;
    }
    // Permission scope starts here.
    perm.assert();

    commonDialog = new COM("WIA.CommonDialog");

    if (commonDialog != null)
    {
        imageFile = commonDialog.ShowAcquireImage(
                1,
                1,
                65536,
                wiaFormatJPEG,
                false,
                true,
                false);

        if (WinAPI::fileExists("C:\\Temp\\c\\AXtestb.jpg"))
        {
            info('file exist');
        }
        else
        {
            imageFile.SaveFile("C:\\Temp\\c\\AXtestb.jpg");
        }
    }

    // Close code access permission scope.
    CodeAccessPermission::revertAssert();
}

Ideas:

  • What you could do is integrate this into the document handling classes in AX and make this available as a feature on the document handling form. ie. Scan and attach a document
  • You could use the same for Web Cams and a few other devices

3 comments:

Muhammad Yasir said...

Hi,

You post is quiet nice, one thing i would like to ask is that if i want to search for scanners that are connected over network so how can i search for that device.

Please reply as soon as possible.

Muhammad Yasir said...
This comment has been removed by a blog administrator.
Munib said...

Hi Muhammad,
I haven't tried it for over the network. I don't know if it will work.