11

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;
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
SleepNot
  • 2,982
  • 10
  • 43
  • 72

3 Answers3

33

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;
Mafii
  • 7,227
  • 1
  • 35
  • 55
Michael
  • 2,361
  • 1
  • 15
  • 15
  • I added encoding functionality to the WindowsCE demo which you can find in the source code repository of ZXing.Net. I posted a more complete list of necessary steps in the forum http://zxingnet.codeplex.com/discussions/396017 – Michael Nov 08 '12 at 22:07
  • Im not getting the .Format property on mine. But the one you posted on CodePlex did it. Thanks. – SleepNot Nov 09 '12 at 05:05
2

@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[]));
    }
lixonn
  • 1,033
  • 1
  • 12
  • 28
0

could it possibly be the size of the codes your are scanning?

take a look here

best way to generate and encode QR codes would be...

QR code encoder and Zbar

dizzytri99er
  • 930
  • 9
  • 25
  • Nope. I dont think so. I am done with the scanning part. Im now trying to produce a QR Code from a string. – SleepNot Nov 08 '12 at 13:54
  • after some reading, im pretty sure that Zxing does not support Aztec encoding.... only decoding im afraid. [QR code encoder](https://github.com/myang-git/QR-Code-Encoder-for-Objective-C) is the best QR code generator and [ZBar](http://zbar.sourceforge.net/iphone/) is a great framework for encoding and decoding, may be a hassle switching over but worth it in the long run if you can get the QR code – dizzytri99er Nov 08 '12 at 14:00
  • are you sure about that? but I am not trying to encode in Aztec. – SleepNot Nov 08 '12 at 14:18
  • if the error message mentions aztec then i can only assume that you are. [This](http://stackoverflow.com/questions/2489048/qr-code-encoding-and-decoding-using-zxing) discussion talks about getting Zxing to encode QR codes but i honestly think you are better off with QR code encoder and Zbar – dizzytri99er Nov 08 '12 at 14:21
  • But I dont think Zbar has an encoder/decoder for the .NET Compact Framework. – SleepNot Nov 08 '12 at 14:34
  • then im at a loss im afraid dude. I dont see any reason why it wouldn't be compatible but from personal experience Zbar and QR code have served me well – dizzytri99er Nov 08 '12 at 15:35