Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZXing.Net Encode string to QR Code in CF

How could I encode my string into a QR Code using ZXing.Net?

I can already decode, but having problems in encoding. It has an error that says: no encoder available for format AZTEC.

Here is my code:

IBarcodeWriter writer = new BarcodeWriter();
Bitmap barcodeBitmap;
var result = writer.Encode("Hello").ToBitmap();
barcodeBitmap = new Bitmap(result);
pictureBox1.Image = barcodeBitmap;
like image 300
SleepNot Avatar asked Nov 08 '12 13:11

SleepNot


2 Answers

You don't fully initialize the BarcodeWriter. You have to set the barcode format.

Try the following code snippet:

IBarcodeWriter writer = new BarcodeWriter { Format = BarcodeFormat.QR_CODE };
var result = writer.Write("Hello");
var barcodeBitmap = new Bitmap(result);
pictureBox1.Image = barcodeBitmap;
like image 197
Michael Avatar answered Oct 24 '22 11:10

Michael


@dizzytri99er

Seems that I have sucessfully encoded a message with ZXing.net therefore I think it does support Aztec encoding

This is the code I have used;

    static void Main(string[] args)
    {
        IBarcodeWriter writer = new BarcodeWriter
            {
                Format = BarcodeFormat.AZTEC
            };
        Bitmap aztecBitmap;
        var result = writer.Write("I love you ;)");
        aztecBitmap = new Bitmap(result);

        using (var stream = new FileStream("test.bmp", FileMode.OpenOrCreate, FileAccess.ReadWrite))
        {
            var aztecAsBytes = ImageToByte(aztecBitmap);
            stream.Write(aztecAsBytes, 0, aztecAsBytes.Length);
        }
    }


    public static byte[] ImageToByte(Image img)
    {
        ImageConverter converter = new ImageConverter();
        return (byte[])converter.ConvertTo(img, typeof(byte[]));
    }
like image 34
lixonn Avatar answered Oct 24 '22 12:10

lixonn