Generating QR code on Windows Phone 7.1 using ZXing 2.0 C# Port

╄→гoц情女王★ 提交于 2019-12-08 07:42:57

问题


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.


回答1:


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;



回答2:


Try setting the image source like this :

image1 = new ImageBrush { ImageSource = asBitmap ;}



回答3:


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.




回答4:


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);


来源:https://stackoverflow.com/questions/10293726/generating-qr-code-on-windows-phone-7-1-using-zxing-2-0-c-sharp-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!