Generating QR code on Windows Phone 7.1 using ZXin

2019-05-31 22:46发布

I'm having trouble generating a QR code on mango 7.1 with ZXing 2.0. It should be pretty straight forward, but it's not working.

The code:

QRCodeWriter writer = new QRCodeWriter();
var bMatrix = writer.encode("Hey dude, QR FTW!", BarcodeFormat.QR_CODE, 25, 25);
var asBitmap = bMatrix.ToBitmap();            
image1.Source = asBitmap;

image1 comes from the xaml.

bMatrix seems to contain the data that I need, but image1 never shows a thing.

4条回答
相关推荐>>
2楼-- · 2019-05-31 23:22

Try setting the image source like this :

image1 = new ImageBrush { ImageSource = asBitmap ;}
查看更多
做自己的国王
3楼-- · 2019-05-31 23:27

The easiest solution:

Uri uri = new Uri("http://www.esponce.com/api/v3/generate?content=" + "your content here" + "&format=png");

image1.Source = new BitmapImage(uri);
查看更多
Summer. ? 凉城
4楼-- · 2019-05-31 23:35

So I managed to do a workaround. I'm not sure if my original code didnt work due to a bug in the ZXing C# port or if I did something wrong. Anyhow, here is what I did to show the QR code.

image1 comes from xaml.

QRCodeWriter writer = new QRCodeWriter();
var bMatrix = writer.encode("Hey dude! QR FTW!", BarcodeFormat.QR_CODE, width, height);

WriteableBitmap wbmi = new System.Windows.Media.Imaging.WriteableBitmap(width, height);

for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x++)
    {
        int grayValue = bMatrix.Array[y][x] & 0xff;
        if (grayValue == 0)                        
            wbmi.SetPixel(x, y, Color.FromArgb(255, 0, 0,0));                                                    
         else
             wbmi.SetPixel(x, y, Color.FromArgb(255, 255, 255, 255));
     }
 }
 image1.Source = wbmi;
查看更多
我想做一个坏孩纸
5楼-- · 2019-05-31 23:46

I run into the same problem. Assigning the WriteableBitmap directly to Image.Source didn't work. After some search I found a strengh Workaround which writes the WritableBitap into a MemoryStream using SaveJpeg method:

    using (MemoryStream ms = new MemoryStream())
    {
        asBitmap.SaveJpeg(ms, (int)asBitmap.PixelWidth, (int)asBitmap.PixelHeight, 0, 100);
        BitmapImage bmp = new BitmapImage();
        bmp.SetSource(ms);
        Image.Source = bmp;
    }

This worked unless the QR code was displayed in dark/light blue, not black/white. Telling this a friend he remebered that in Windows phone pixel Color is not a Byte, but an integer. With this knowledge and the sources of zxing I changed the ByteMatrix.ToBitmap method as follows:

    public WriteableBitmap ToBitmap()
    {
        const int BLACK = 0;
        const int WHITE = -1;
        sbyte[][] array = Array;
        int width = Width;
        int height = Height;
        var pixels = new byte[width*height];

        var bmp = new WriteableBitmap(width, height);

        for (int y = 0; y < height; y++)
        {
            int offset = y*width;
            for (int x = 0; x < width; x++)
            {
                int c = array[y][x] == 0 ? BLACK : WHITE;
                bmp.SetPixel(x, y, c);
            }
        }

        //Return the bitmap
        return bmp;
    }

And this solved the problem at all, even assigning the WritableBitmap directly to Image.Source. It seemed, the Image was correctly assigned, but the alpha value was transparent, which was removed when creating a jpeg.

查看更多
登录 后发表回答