zoukankan      html  css  js  c++  java
  • 引用ZXing生成二维码

    1、生成二维码

    ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。

    Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。本文引用zxing.dll,生成二维码。

    using com.google.zxing.qrcode;
    using com.google.zxing;
    using com.google.zxing.common;
    using ByteMatrix = com.google.zxing.common.ByteMatrix;
    using EAN13Writer = com.google.zxing.oned.EAN13Writer;
    using EAN8Writer = com.google.zxing.oned.EAN8Writer;
    using MultiFormatWriter = com.google.zxing.MultiFormatWriter;
    using System.IO;
    using System.Collections;
    
    
    private void btnGenerate_Click(object sender, EventArgs e)
            {
                ByteMatrix byteMatrix;
                string content = this.textBox1.Text;
    
                if (!string.IsNullOrEmpty(content))
                {
                    byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300);
                    bitmap = ToBitmap(byteMatrix);
                }
    
                this.pictureBox1.Image = bitmap;
                mapCreate = bitmap;
            }
    
            public static Bitmap ToBitmap(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;
            }
    View Code

    2、加图片水印

    //图片水印处理方法
            private Bitmap ImageWatermark(Bitmap map, string waterpath)
            {
                Image waterimg = Image.FromFile(waterpath);
    
                //添加水印
                Graphics g = Graphics.FromImage(map);
    
                //获取水印位置设置
                ArrayList loca = new ArrayList();
                int x = 0;
                int y = 0;
                x = map.Width / 2 - waterimg.Width / 2;
                y = map.Height / 2 - waterimg.Height / 2;
                loca.Add(x);
                loca.Add(y);
    
                //g.DrawImage(waterimg, new Rectangle(int.Parse(loca[0].ToString()), int.Parse(loca[1].ToString()), waterimg.Width, waterimg.Height));
                g.DrawImage(waterimg, new Rectangle(int.Parse(loca[0].ToString()), int.Parse(loca[1].ToString()), 75, 75));
    
                return map;
            }
    View Code

    3、导出

    //先添加 saveFileDialog控件 
    
    private void btnOut_Click(object sender, EventArgs e)
            {
                try
                {
                    saveFileDialog1.ShowDialog();
                    string fileName = saveFileDialog1.FileName;
    
                    if (fileName != null)
                    {
                        mapCreate.Save(fileName);//mapCreate是bitmap格式的图片
                    }
                }
                catch (Exception ex)
                {                
                    throw;
                }
            }
    View Code
  • 相关阅读:
    使用TFS CI 又想保留服务运行状态的简单方法
    【知乎】二战中日本陆军的建制
    缓存你的BITMAP对象
    Android 多种方式正确的加载图像,有效避免oom
    GitHub进一步了解
    响应式编程,是明智的选择
    Android 主题动态切换框架:Prism
    Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误
    ClassLoader工作机制
    Java——泛型(最易懂的方式讲解泛型)
  • 原文地址:https://www.cnblogs.com/yuan-jun/p/6628621.html
Copyright © 2011-2022 走看看