Click here to Skip to main content
Click here to Skip to main content

DataMatrixNet ported to Compact Framework

By , 18 Mar 2010
 

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 much 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:

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

Visual Studio 2005 / Compact Framework 2:

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

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

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

Visual Studio 2005 / Compact Framework 2:

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:

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

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

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

to this:

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

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

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

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:

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.

<!-- Social Bookmarks BEGIN -->

License

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

About the Author

hjgode
Germany Germany
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questiondata matrix librarymemberZacbr2 Sep '12 - 17:57 
Data Matrix barcode library used for encoding barcode in .NET framework application.
Generalgs1/ean? fnc1memberxxserxx1123 Apr '10 - 4:47 
Datamatrix is how ecc200/ean) barcode?
D'Oh! | :doh:
datamatrix text=
010869959157033121000812099100007#17110811100008120991
#=(char)29
 
error:
Warning: GS1 (EAN) signature ']d2' is not encoded as a control character FNC1 as specified, it is encoded as plain text.
GeneralRe: gs1/ean? fnc1memberhjgode27 Apr '10 - 2:13 
Hello
 
I dont know what you did. I tested with
 
textBox1.Text = "010869959157033121000812099100007\x1D17110811100008120991";
and
textBox1.Text = "010869959157033121000812099100007\x2917110811100008120991";
 
and dont get such an warning message. There is even no such warning defined anywhere in the source of DataMatrixNetCF.
 
Did you post at the right place.
 
regards
 
Josef
GeneralRe: gs1/ean? fnc1memberxxserxx1127 Apr '10 - 2:43 
i tested bctester program this datamatrix code
 
datamatrix barcode type :
 
ecc200!
Missing EAN signature!
 
How can I make this data matrix type (ecc200/ean) datamatrix barcode?
 
Regards
GeneralRe: gs1/ean? fnc1memberhjgode27 Apr '10 - 7:29 
Sorry
 
but I cannot help with this issue you have with bctester.
 
regards
 
Josef
GeneralRe: gs1/ean? fnc1memberxxserxx1127 Apr '10 - 10:13 
last question
 
Is this DataMatrixNet compatible ecc200/ean type?
 
thanks
AnswerRe: gs1/ean? fnc1memberhjgode28 Apr '10 - 2:05 
Yes, DatamatrixNet and DatamatrixNetCF are ECC200 compatible.
 
You have to adjust the DatamatrixNet option settings and then you will get another Datamatrix image. In my code you have to change the following:
 
...
options.ModuleSize = 4;
options.MarginSize = 4;
options.BackColor = Color.White;
options.ForeColor = Color.Black;
options.SizeIdx = DmtxSymbolSize.DmtxSymbol32x32;// DmtxSymbol96x96;
options.Scheme = DmtxScheme.DmtxSchemeAscii;// DmtxSchemeAutoBest;
...
Bitmap encodedBitmap = encoder.EncodeImage(textBox1.Text,options);
...
 
Then you will get an bcTester accepted Datamatrix code for your text.
 
I will update my blog and include some screen shots and the new code with the ability to change settings.
 
regards
 
Josef
GeneralRe: gs1/ean? fnc1memberxxserxx1128 Apr '10 - 3:30 
but;
I was selected in the bctester program value GS1/EAN. not select hex.
and Type must be Datamatrix(ECC200/EAN).
 
Datamatrix content=
]
d20108691234567890211323424679#1707011910X2512061322
 
01 - Global Trade Item Number (GTIN)
08691234567890
21 - Serial Number
1323424679
17 - Expiration Date (YYMMDD)
070119
10 - Batch or Lot number
X2512061322
GeneralRe: gs1/ean? fnc1memberhjgode28 Apr '10 - 5:50 
Sorry
 
but then I cannot help anymore. Please contact the author at the original post Barcode Image Generation Library[^]
 
or see the c++ based library libdmtx at sourceforge.
 
I am sorry, but I am not familar with all these different options, I only ported the code to CF and it works for my use.
 
with regards
 
Josef
GeneralRe: gs1/ean? fnc1membermichivo12 Apr '11 - 10:15 
Hi,
I am the author Smile | :)
I just fixed the Encoding of GS1 DataMatrix codes. Simply use DmtxScheme.DmtxSchemeAsciiGS1 as the encoding scheme. DataMatrix.Net also supports .net CF 3.5 out of the box now.
br, Michael

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 18 Mar 2010
Article Copyright 2010 by hjgode
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid