Tuesday, August 5, 2014

Generate QR barcode in Dynamics AX 2012

I just realize that Dynamics AX 2012 already provided the QR code generation under "C Sharp Projects" nodes


Below is the sample code how to use it

static void QRBarcode(Args _args)
{
    Microsoft.Dynamics.QRCode.Encoder   qrCode;
    System.String                       netString;
    str                                 tempFileName;
    System.Drawing.Bitmap               netBitmap;
    Bitmap                              imageQR;
    FileIOPermission    perm;  
    BinData             binData;
    container           imageContainer;  
    ;
   
    netString = "TEST";  
    qrCode = new Microsoft.Dynamics.QRCode.Encoder();  
    netBitmap = qrCode.Encode(netString); //encode the string as Bitmap can be used already
   
    tempFileName = qrCode.GetTempFile(netString); //to get the QR temporary file
   
    perm = new FileIOPermission(tempFileName,'r');
    perm.assert();  
    binData = new binData();
    binData.loadFile(tempFileName);
    imageContainer = binData.getData(); //get the QR code image put inside container so can be stored inside database for storing or reporting purpose

    System.IO.File::Delete(tempFileName);

    CodeAccessPermission::revertAssert();      
}

By using the code above we don't need to put another 3rd party software to generate the QR bar code.

In standard AX process this QRCode function has been used to generate the E-Invoice QR bar code.