• 微信二维码名片生成示例


    二维码的对于现在已经很流行了,主要是因为其大数据量和容错能力。出于爱好,学了下google的zxing对二维码的处理。

    首先生成一张二维码的话,只要输入文本就OK了。

    下面是加密的代码

    protected void btnEncode_Click(object sender, EventArgs e)  
           {  
             
               try
               {  
                   //要生成二维码的文本。  
                   string content =  "我是逆世狂神\r\n我的百度Hi:http://hi.baidu.com/yunanwu\r\n我的微博:http://weibo.com/wuyunnan";  
                   //二维码图片保存路径  
                   string file = AppDomain.CurrentDomain.BaseDirectory + "test.png";  
                   //防止中文乱码,如果此处不进行设置,可以修改源文件(zxing库是开源项目)
                   /*源代码中有两处UTF-8的问题,会导致乱码,
                      其一:com.google.zxing.qrcode.encoder.encoder类中的
                      internal const System.String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";
                      此处,将ISO-8859-1改为UTF-8
                      其二:com.google.zxing.qrcode.decoder.DecodedBitStreamParser类的成员
                      private const System.String UTF8 = "UTF8";
                      应将UTF8改为UTF-8
                   */  
                   Hashtable hst = new Hashtable();  
                   hst.Add(EncodeHintType.CHARACTER_SET, "UTF-8");  
                   //encode为重载方法,如果不设置Hashtable,则可能会导致中文乱码  
                   zxing.ByteMatrix byteMatrix = new MultiFormatWriter().encode(content,   
                       BarcodeFormat.QR_CODE, 350, 350,hst);  
                   //写入到文件  
                   writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, file);   
                   //添加头像  
                    addImg(file); 
                   //显示到前台  
                   img.InnerHtml += "<img width='256px' src='test.png' id='imgTest' />";  
             
               }  
               catch (Exception ex)  
               {  
                   Response.Write(ex.Message);  
               }  
           }  
             
             
    public void writeToFile(zxing.ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)  
           {  
               //生成图片  
               Bitmap bmap = toBitmap(matrix);  
               //保存图片  
               bmap.Save(file, format);  
           }  
             
           public Bitmap toBitmap(zxing.ByteMatrix matrix)  
           {  
               //图片宽度和高度  
               int width = matrix.Width;  
               int height = matrix.Height;  
               //实例化一张图片(最后的参数是指定图片的像素格式)  
               Bitmap bmap = new Bitmap(width, height,System.Drawing.Imaging.PixelFormat.Format32bppArgb);  
               //循环渲染每个像素  
               for (int x = 0; x < width; x++)  
               {  
                   for (int y = 0; y < height; y++)  
                   {  
                       bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));  
                   }  
               }  
               return bmap;  
           }
    //   添加头像  
           /*添加头像的方法主要原理也是添加水印的原理*/
           public string addImg(string file)  
           {  
               System.Drawing.Image img = System.Drawing.Image.FromFile(file);  
               System.Drawing.Image img2 = System.Drawing.Image.FromFile(  
                   AppDomain.CurrentDomain.BaseDirectory + "1.jpg");  
               Graphics g = Graphics.FromImage(img);  
               g.DrawImage(img2, 140, 140, 64, 64);  
               string name = Server.MapPath("\\new\\" + Path.GetFileName(file));  
               img.Save(name);  //保存带头像的二维码图片
               g.Dispose();  
               img.Dispose();  
               img2.Dispose();  
               File.Delete(file);//删除旧二维码  
               return name; //返回新的二维码图片路径 
           }

    下面是解析的代码:

    protected void btnDecode_Click(object sender, EventArgs e)  
           {  
               //保存上传的文件  
               string name = AppDomain.CurrentDomain.BaseDirectory + "\\upload\\" + FileUpload1.FileName + ".png";  
               FileUpload1.SaveAs(name);  
               //读取图片  
               System.Drawing.Image img = System.Drawing.Image.FromFile(name);  
               Bitmap bmap;  
               try
               {  
                   bmap = new Bitmap(img);  
               }  
               catch (System.IO.IOException ioe)  
               {  
            
                   Response.Write(ioe.ToString());  
                   return;  
               }  
               if (bmap == null)  
               {  
            
                   Response.Write("无法进行解码");  
                   return;  
               }  
               //解码二维码  
               LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);  
               com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new zxing.HybridBinarizer(source));  
               Result result;//保存解码结果  
               try
               {  
                   result = new MultiFormatReader().decode(bitmap);  
               }  
               catch (ReaderException re)  
               {  
                   this.txtContext.Text = re.ToString(); 
                   return;  
               }  
               txtContext.Text =result.Text;  
            
            
            
           }

    扫扫我的二维码名片吧:

    此处添加头像的代码是直接重画图片,其实也就是添加水印的原理,如果有更好的方法请赐教...

    本文从百度空间搬家到博客园。。

    邮箱:yunanwu@foxmail.com 微博:@提灯寻影(http://weibo.com/wuyunnan) 技术主页:http://www.cnblogs.com/yuanawu/ 可以白手起家不可手无寸铁!我是我命运的主宰者,我是我灵魂的掌舵人! 每一次的选择都将是一个挑战!
  • 相关阅读:
    例行性工作排程 (crontab)
    数组
    继续我们的学习。这次鸟哥讲的是LVM。。。磁盘管理 最后链接文章没有看
    htop资源管理器
    转:SSL协议详解
    转:SSL 握手协议详解
    转:Connection reset原因分析和解决方案
    使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
    转:logback的使用和logback.xml详解
    转:Java logger组件:slf4j, jcl, jul, log4j, logback, log4j2
  • 原文地址:https://www.cnblogs.com/yunanwu/p/4168573.html
Copyright © 2020-2023  润新知