WP--条形码(一维码)生成


网上能找到的参考资料不多,其实很简单本人分享下吧:


1.引用ZXing库百度一下,不同版本的ZXing库架构略有不同。部分方法和属性的调用会有些少出入,本文使用的是最新的 zxing.wp8.0.dll。


2.引用

using ZXing;
using ZXing.OneD;
using ZXing.Common;

3.直接贴出我项目里写好的通用:

   /// <summary>
        /// 生成可直接使用的位图对象
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static WriteableBitmap CreateBarcode(string str)
        {
            WriteableBitmap wb = null;
            Code128Writer writer = new Code128Writer();
            BitMatrix bitMatrix;
            try
            {
                bitMatrix = writer.encode(str, BarcodeFormat.CODE_128, 440, 120);
                wb = ConvertByteMartixToWriteableBitmap(bitMatrix);
            }
            catch
            {

            }
            return wb;
        }

        /// <summary>
        /// 将点矩阵转化为位图
        /// </summary>
        /// <param name="bm"></param>
        /// <returns></returns>
        public static WriteableBitmap ConvertByteMartixToWriteableBitmap(BitMatrix bm)
        {
            WriteableBitmap wb = new WriteableBitmap(bm.Width, bm.Height);
            for (int x = 0; x <= wb.PixelWidth - 1; x++)
            {
                for (int y = 0; y <= wb.PixelHeight - 1; y++)
                {
                    if (!bm[x, y])
                    {
                        //白色            
                        wb.Pixels[wb.PixelWidth * y + x] = BitConverter.ToInt32(BitConverter.GetBytes(0xffffffff), 0);
                    }
                    else
                    {
                        //黑色       
                        wb.Pixels[wb.PixelWidth * y + x] = BitConverter.ToInt32(BitConverter.GetBytes(0xff000000), 0);
                    }
                }
            }
            return wb;
        }


基本直接调用就行了,懂的不用问,不懂的自己问自己


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值