Click here to Skip to main content
15,867,686 members
Articles / All Topics

DataMatrixNet Ported to Compact Framework

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
18 Mar 2010CPOL1 min read 24.8K   5   11
DataMatrixNet for Compact Framework 2.0

If you need to print, show, generate or analyse DataMatrix Barcodes on a Windows Mobile device, you can now use this class library. I have ported the code to be compatible with Compact Framework 2.0 and Visual Studio 2005.

The original full .NET Framework source code is located at SourceForge.

Here is first a screenshot of the test application running on a Windows Mobile device using the DataMatrixNetCF class:

Test application screenshot

The full .NET framework has many more functions or the classes provide more optional arguments than Compact Framework (CF) does. So there is some code to be changed to work with CF. BTW: you can run the code on a desktop PC to, as the Full Framework is upwards compatible to CF.

I had to change some constructs which are used to initialize a structure directly with some values. Here is one example for DmtxDecode.cs:

Original:

C#
DmtxPixelLoc pEmpty = new DmtxPixelLoc() { X = 0, Y = 0 };

Visual Studio 2005 / Compact Framework 2:

C#
DmtxPixelLoc pEmpty = new DmtxPixelLoc();
pEmpty.X = 0; pEmpty.Y = 0;

Similar changes have to be applied to a lot of code lines:

Original:

C#
follow.Loc = new DmtxPixelLoc() { X = followBeg.Loc.X + 
	DmtxConstants.DmtxPatternX[patternIdx], Y = followBeg.Loc.Y + 
	DmtxConstants.DmtxPatternY[patternIdx] };

Visual Studio 2005 / Compact Framework 2:

C#
follow.Loc = new DmtxPixelLoc(followBeg.Loc.X + 
	DmtxConstants.DmtxPatternX[patternIdx], followBeg.Loc.Y + 
	DmtxConstants.DmtxPatternY[patternIdx]);

To get this working, the structure defined in DmtxPixelLoc.cs had to get a constructor code:

C#
public DmtxPixelLoc(int x, int y)
{
    _x = x;
    _y = y;
}

As the compiler complained about this ugly construct, I rewrote this:

C#
prevPrevValue = (byte)((prevIndex > channel.FirstCodeWord / 12) ? 
	channel.EncodedWords[prevIndex - 1] : 0);

to this:

C#
if (prevIndex > channel.FirstCodeWord / 12)
   prevPrevValue = channel.EncodedWords[prevIndex - 1];
else
   prevPrevValue = 0;

I don't like this short form of If/else.

Finally, it took some time to convert this code of the file DmtxImageEncoder.cs to CF:

C#
internal static Bitmap CopyDataToBitmap(byte[] data, int width, int height)
{
    data = InsertPaddingBytes(data, width, height, 24);
    int stride = 4 * ((width * 24 + 31) / 32);
    GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
    //Here create the Bitmap to the know height, width and format
    Bitmap bmp = new Bitmap(width, height, stride, 
		PixelFormat.Format24bppRgb, dataHandle.AddrOfPinnedObject());
    return bmp;
}

In compact framework, this works:

C#
internal static Bitmap CopyDataToBitmap(byte[] data, int width, int height)
{
    data = InsertPaddingBytes(data, width, height, 24);
    int stride = 4 * ((width * 24 + 31) / 32);
    GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);

    // Create a new bitmap.
    Bitmap msBMP = new Bitmap(width,height,PixelFormat.Format24bppRgb);

    // Lock the bitmap's bits.
    Rectangle rect = new Rectangle(0, 0, msBMP.Width, msBMP.Height);
    System.Drawing.Imaging.BitmapData bmpData =
    msBMP.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
    PixelFormat.Format24bppRgb);

    // Get the address of the first line.
    IntPtr ptr = bmpData.Scan0;

    int bytes  = bmpData.Stride * msBMP.Height;
    // Copy the RGB values back to the bitmap
    System.Runtime.InteropServices.Marshal.Copy(data, 0, ptr, bytes);

    // Unlock the bits.
    msBMP.UnlockBits(bmpData);
    return msBMP; // return bmp;
}

Here, you can download the source code of the class library and a test application. The code is written in Visual Studio 2005 with Windows Mobile 5 SDK as target.

If you need some PDF library for Windows Mobile, see my port of iTextSharp. The sourceforge DataMatrixNet code uses iTextSharp too.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalgs1/ean? fnc1 Pin
xxserxx1123-Apr-10 4:47
xxserxx1123-Apr-10 4:47 
GeneralRe: gs1/ean? fnc1 Pin
hjgode27-Apr-10 2:13
hjgode27-Apr-10 2:13 
GeneralRe: gs1/ean? fnc1 Pin
xxserxx1127-Apr-10 2:43
xxserxx1127-Apr-10 2:43 
GeneralRe: gs1/ean? fnc1 Pin
hjgode27-Apr-10 7:29
hjgode27-Apr-10 7:29 
GeneralRe: gs1/ean? fnc1 Pin
xxserxx1127-Apr-10 10:13
xxserxx1127-Apr-10 10:13 
AnswerRe: gs1/ean? fnc1 Pin
hjgode28-Apr-10 2:05
hjgode28-Apr-10 2:05 
GeneralRe: gs1/ean? fnc1 Pin
xxserxx1128-Apr-10 3:30
xxserxx1128-Apr-10 3:30 
GeneralRe: gs1/ean? fnc1 Pin
hjgode28-Apr-10 5:50
hjgode28-Apr-10 5:50 
GeneralRe: gs1/ean? fnc1 Pin
michivo12-Apr-11 10:15
michivo12-Apr-11 10:15 
GeneralRe: gs1/ean? fnc1 Pin
hjgode29-Apr-10 4:30
hjgode29-Apr-10 4:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.