4

我一直在网络上搜索在 Windows Phone 8.1 中使用 Zxing 的代码示例,但没有找到。我正在用 C# 编写,下面是我的代码,到目前为止我已经想出了:

BarcodeWriter _writer = new BarcodeWriter();

var hello =  _writer.Encoder.encode("HelloWhoIsThere", BarcodeFormat.QR_CODE, 350, 350);

ZXing.Common.BitMatrix matrix = new ZXing.Common.BitMatrix(359,350);

ZXing.Rendering.PixelData rendered = _writer.Renderer.Render(hello, BarcodeFormat.CODE_128, "HelloWhoIsThere");

byte[] byte1 = rendered.Pixel;

Stream memStream = new MemoryStream(byte1);

memStream.Position = 0;

BitmapDecoder decoder = await BitmapDecoder.CreateAsync(memStream.AsRandomAccessStream());

// create a new stream and encoder for the new image
InMemoryRandomAccessStream mrAccessStream = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(mrAccessStream, decoder);

// convert the bitmap to a 400px by 400px bitmap
encoder.BitmapTransform.ScaledHeight = 350;
encoder.BitmapTransform.ScaledWidth = 350;

// write out to the stream
try
{
    await encoder.FlushAsync();
}
catch (Exception ex)
{
    string s = ex.ToString();
}

// render the stream to the screen
WB = new WriteableBitmap(350, 350);
WB.SetSource(mrAccessStream);
if (WB != null)
{
    SelectedImage.Source = WB;
}
if (WB == null)
{
    txtDecoderContent.Text = "WB = null";
}

我收到“System.NullReferenceException:对象引用未设置为对象实例”的错误。我认为当我尝试将呈现的 QR 码转换为字节 [] 时会发生这种情况。

我会很感激任何帮助,谢谢

4

1 回答 1

7

usings

using ZXing;
using Windows.UI.Xaml.Media.Imaging;

code

IBarcodeWriter writer = new BarcodeWriter
            {
                Format = BarcodeFormat.QR_CODE,
                Options = new ZXing.Common.EncodingOptions
                {
                    Height = 300,
                    Width = 300
                }
            };
var result = writer.Write("generator works");
var wb = result.ToBitmap() as WriteableBitmap;

//add to image component
image.Source = wb;

much simpler and working (tested in one of my apps)

于 2015-01-16T16:14:59.223 回答