Quantcast
Channel: magick Discussions Rss Feed
Viewing all articles
Browse latest Browse all 3693

New Post: create valid barcode image

$
0
0
Hello,

I'm trying to create an image with a type 128C barcode using Magick.NET-7.0.0.0021-Q8-x86. Whatever I try, I can not get the barcode recognized by a barcode scaner or zbarimg although, to the naked eye, the image looks similar to another image, for the same code, generated using System.Drawing.

I try to do this in Dynamics AX 2009 (ERP software) that has it's own language called x++ but it can use C# libraries (if added to references).

My code looks similar to what one normaly use in C# but with some differences. Maybe someone can take a look to see if I'm missing something to generate a valid barcode

first I declare the variables :
    str                              barcodetxt;
    InteropPermission       permission;

    ImageMagick.MagickImage image;
    ImageMagick.MagickColor bgColor, fontColor;
    ImageMagick.Gravity     gravity;

    System.Byte             r, g, b;
    System.Text             barcodeTxtNet;

    BarcodeCode128          MyBarcode = BarcodeCode128::construct();

    Image                   BarcodeImage;
    Int64                   barcodeBitmapPtr;
    System.Drawing.Bitmap   barcodeBitmap;
    Container               returnData;
;
 
now the code that generates the bitmap
    permission = new InteropPermission(InteropKind::ClrInterop); //this allows me to call C# code on the server tier
    permission .assert();

    r = System.Convert::ToByte(255);
    g = System.Convert::ToByte(255);
    b = System.Convert::ToByte(255);
    try
    {
        bgColor = new ImageMagick.MagickColor(r,g,b); //create a image with white background
        image = new ImageMagick.MagickImage(bgColor, 800, 240); //set the size

//next I try to set the bit depth to 1 but it dosn't seem to work 
//(if I save the image as a bmp it has bit depth of 32 and 8 if I save it as a jpg)
        image.set_ColorType(ImageMagick.ColorType::Bilevel);
        image.set_ClassType(ImageMagick.ClassType::Pseudo);
        image.ChangeColorSpace(ImageMagick.ColorSpace::RGB);

        barcodetxt = '6625020060338943108201500001061102';//this is the code

       //MyBarCode is a class that encodes the above code to code 128C
        MyBarCode.string(true,barcodetxt);
        MyBarCode.encode();
        barcodetxt=MyBarCode.barcodeStr();
        barcodetxt = strfmt('label:%1', barcodetxt);
// now barcodetext has this value: label:%"*"$%*!&&&%%&&)-!!#'%%-!')&#"""-&%&'!)%&&&%&"&#'""&&%*!&')!1
//if I copy this code in an word document and use the same font I get a valid barcode!

        r = System.Convert::ToByte(0);
        g = System.Convert::ToByte(0);
        b = System.Convert::ToByte(0);
        fontColor = new ImageMagick.MagickColor(r,g,b);

        image.set_Font(@'C:\WINDOWS\Fonts\C128N.ttf');
        image.set_FontPointsize(72);

        //image.set_BorderColor(bgColor);
        //image.set_BackgroundColor(bgColor);
        //image.set_FillColor(fontColor);
        //image.set_StrokeColor(ImageMagick.MagickColor::get_Transparent());
        //image.set_StrokeColor(bgColor);
        //image.set_StrokeWidth(0);
        //image.set_StrokeAntiAlias(false);

        gravity = ImageMagick.Gravity::Center;
        image.set_TextGravity(gravity);

        //image.set_TextKerning(1);
        //image.set_AntiAlias(false);
        //image.set_Depth(1);

        image.Read(barcodetxt);

        CodeAccessPermission::revertAssert();

        image.Write(@"c:\test.jpg"); 
      }
catch
{
...
}
thanks,
Alex

Viewing all articles
Browse latest Browse all 3693

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>