如何从QRCodeWriter获取Image.Source?

时间:2012-06-26 20:44:37

标签: c# zxing

我对旧的libs有点兴奋,似乎无法从QRCodeWriter转到Image.Source我可以放入我的Image:D

我做如下:

QRCodeWriter writer = new QRCodeWriter();
var bMatrix = writer.encode("FOO FOO FOO", BarcodeFormat.QR_CODE, 300, 300);

但是从那儿起我迷失了。 bMatrix有一个属性Array,但这是zxings的ByteMatrix类型,我仍然需要访问Image.Source。

WriteableBitmap有SetSource和SetValue,但我无法绕过ByteMatrix。 :(

1 个答案:

答案 0 :(得分:1)

试试这个(从糊盒中取出:http://pastebin.com/612q0Qrb

    QRCodeWriter writer = new QRCodeWriter();
    com.google.zxing.common.ByteMatrix matrix;

    int size = 180;
    matrix = writer.encode("MECARD:N:Owen,Sean;ADR:76 9th Avenue, 4th Floor, New York, NY 10011;TEL:+12125551212;EMAIL:srowen@example.com;; ", BarcodeFormat.QR_CODE, size, size, null);


    Bitmap img = new Bitmap(size, size);
    Color Color = Color.FromArgb(0, 0, 0);

    for (int y = 0; y < matrix.Height; ++y)
    {
        for (int x = 0; x < matrix.Width; ++x)
        {
            Color pixelColor = img.GetPixel(x, y);

            //Find the colour of the dot
            if (matrix.get_Renamed(x, y) == -1)
            {
                img.SetPixel(x, y, Color.White );
            }
            else
            {
                img.SetPixel(x, y, Color.Black);
            }
        }
    }


    img.Save(@"c:test.bmp",ImageFormat.Bmp);