我一直在网上搜索Windows Phone 8.1中使用Zxing的代码示例,但是很简单.我在C#中编写,下面是我的代码,到目前为止我已经提出了:BarcodeWriter _writer = new BarcodeWriter();var hello = _writer.Encoder.encode(H...
我一直在网上搜索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码转换为byte []时会发生这种情况.
我很感激任何帮助,谢谢
解决方法:
usings
using ZXing;
using Windows.UI.Xaml.Media.Imaging;
码
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;
更简单和工作(在我的一个应用程序中测试)
沃梦达教程
本文标题为:c# – 如何在Windows Phone 8.1上使用Zxing创建QR码图像
基础教程推荐
猜你喜欢
- winform把Office转成PDF文件 2023-06-14
- unity实现动态排行榜 2023-04-27
- C# List实现行转列的通用方案 2022-11-02
- C#类和结构详解 2023-05-30
- C#控制台实现飞行棋小游戏 2023-04-22
- C# windows语音识别与朗读实例 2023-04-27
- 一个读写csv文件的C#类 2022-11-06
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- ZooKeeper的安装及部署教程 2023-01-22
- C# 调用WebService的方法 2023-03-09