我一直在网上搜索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码图像
猜你喜欢
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- c# 模拟线性回归的示例 2023-03-14
- Unity Shader实现模糊效果 2023-04-27
- Oracle中for循环的使用方法 2023-07-04
- Unity3D实现渐变颜色效果 2023-01-16
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- 如何使用C# 捕获进程输出 2023-03-10
- user32.dll 函数说明小结 2022-12-26
- .NET CORE DI 依赖注入 2023-09-27