windows phone 生产二维码和解码本地二维码图片

时间:2023-03-09 05:40:37
windows phone 生产二维码和解码本地二维码图片

前面模仿着写了一个手机扫描二维码和条形码的例子,zxing(下载)的Silverlight库实现的,当时还纳闷有windows phone的库为什么不用,其实都是一样的,,,要改的就是获取摄像头获取的数据要修改一下。为了节省时间,就在网上找了一个C# zxing库的例子,写了一下手机生成二维码和解码本地图片库的图片的小例子。

生成二维码

EncodingOptions options;//包含一些编码、大小等的设置
BarcodeWriter write = null;//用来生成二维码,对应的BarcodeReader用来解码
options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = ,
Height=,
Margin=
};
write = new BarcodeWriter();
write.Format = BarcodeFormat.QR_CODE;
write.Options = options;

准备工作做好,只需要write一下就可以了,参数为二维码的内容

WriteableBitmap bitmap = write.Write("ZXing");
imgCode.Source = bitmap;

保存到本地的代码,windows phone 屏幕截图那篇有提到。

解码图片库的图片:

  利用PhotoChooserTask获取图片库的文件,获得图片数据,用BarcodeReader解码

if (e.TaskResult==TaskResult.OK)
{
BarcodeReader reader = new BarcodeReader();
WriteableBitmap wb = new WriteableBitmap(,);
wb.SetSource(e.ChosenPhoto);
try
{
Result result = reader.Decode(wb);
MessageBox.Show(result.Text);
}
catch
{
MessageBox.Show("未能解码你选择的二维码图片,请确认选择正确");
} }

还有就是利用Google Chart服务生成二维码https://developers.google.com/chart/infographics/docs/qr_codes

请求的API地址及参数

API地址: https://chart.googleapis.com/chart
请求参数:

  • cht=qr 生成图片的格式为二维码格式
  • chs=widthxheight 生成图片的大小。例如要生成300x300的图片,参数值为chs=300x300
  • chl=data 二维码中包含的内容,内容必须使用UTF-8格式编码
  • choe=output_encoding 非必要参数。输出的内容编码格式,默认为UTF-8
  • chld=L|M|Q|H 非必要参数,生成的二维码的容错率

如果想生成一个图片大小为200x200像素,内容为Hello,world的二维码,那么请求的URL就是: https://chart.googleapis.com/chart?cht=qr&chld=H&chs=200x200&chl=Hello,world