1

我在使用 ZXing 2.0 的 mango 7.1 上生成二维码时遇到问题。它应该很简单,但它不起作用。

编码:

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 来自 xaml。

bMatrix 似乎包含我需要的数据,但 image1 从不显示任何东西。

4

4 回答 4

3

所以我设法做了一个解决方法。我不确定我的原始代码是否由于 ZXing C# 端口中的错误或我做错了什么而无法工作。无论如何,这是我为显示 QR 码所做的。

image1 来自 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;
于 2012-04-25T09:33:56.343 回答
1

尝试像这样设置图像源:

image1 = new ImageBrush { ImageSource = asBitmap ;}
于 2012-04-25T09:39:57.093 回答
0

我遇到了同样的问题。将 WriteableBitmap 直接分配给 Image.Source 不起作用。经过一番搜索,我发现了一个强大的解决方法,它使用 SaveJpeg 方法将 WritableBitap 写入 MemoryStream:

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

除非 QR 码显示为深/浅蓝色,而不是黑色/白色,否则此方法有效。告诉一位朋友,他记得在 Windows 手机中像素颜色不是字节,而是整数。有了这些知识和 zxing 的来源,我将 ByteMatrix.ToBitmap 方法更改如下:

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

这完全解决了问题,甚至将 WritableBitmap 直接分配给 Image.Source。看起来,图像已正确分配,但 alpha 值是透明的,在创建 jpeg 时已将其删除。

于 2013-02-02T22:29:52.523 回答
-1

最简单的解决方案:

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

image1.Source = new BitmapImage(uri);
于 2012-07-12T06:24:06.530 回答