C# 調用 qrencode的動態庫

 自己根據qrencode的源碼導了一個dll動態庫,見:

自己希望能用C#語言調用以下。
首先構建需要的對象:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct QrCodeLL
    {

        public int version;         ///< version of the symbol
        public int width;
        public int size;///< width of the symbol
        public IntPtr data; ///< symbol data

    }
接口:
[DllImport("LLQrencode.dll", EntryPoint = "OutPutQrCodeCSharp", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr OutPutQrCodeCSharp(byte[] bts);//接收參數對應 unsigned char* 也就是二維碼內容

使用方法:

     public Form1()
        {
            InitializeComponent();
        }
        string str = "https://www.cnblogs.com/HelloQLQ/p/16289395.html";
        private void button1_Click(object sender, EventArgs e)
        {
            var byts = Encoding.UTF8.GetBytes(str);

            IntPtr ptr = IntPtr.Zero;
            var i = TestLL.OutPutQrCodeCSharp(byts);
            var qCode = Marshal.PtrToStructure<QrCodeLL>(i);

            int size = qCode.width * qCode.width + qCode.width - 1;
            byte[] managedArray = new byte[size];
            Marshal.Copy(qCode.data, managedArray, 0, size);

            var arr = getPngArr(managedArray, qCode.width);
            var img = GetMap(arr, qCode.width);

            picBox.Image = Magnifier(img, 4);
            picBox.Refresh();
        }

        public byte[] getPngArr(byte[] data, int width)
        {
            byte[] arr = new byte[data.Length];
            
            int m = 0;
            for (int y = 0; y < width; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    if ((data[y * width + x] & 0x01) == 1)
                    {
                        arr[m++] = 0;
                    }
                    else
                    {
                        arr[m++] = 1;
                    }
                }
            }
            return arr;
        }

        public Bitmap GetMap(byte[] arr, int w)
        {
            Bitmap map = new Bitmap(w, w);

            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    if (arr[i * w + j] == 1)
                        map.SetPixel(i, j, Color.White);
                    else
                    {
                        map.SetPixel(i, j, Color.Black);
                    }
                }
            }
            return map;
        }

        public Bitmap Magnifier(Bitmap srcbitmap, int multiple)
        {
            if (multiple <= 0) { multiple = 0; return srcbitmap; }
            Bitmap bitmap = new Bitmap(srcbitmap.Size.Width * multiple, srcbitmap.Size.Height * multiple);
            BitmapData srcbitmapdata = srcbitmap.LockBits(new Rectangle(new Point(0, 0), srcbitmap.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            BitmapData bitmapdata = bitmap.LockBits(new Rectangle(new Point(0, 0), bitmap.Size), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            unsafe
            {
                byte* srcbyte = (byte*)(srcbitmapdata.Scan0.ToPointer());
                byte* sourcebyte = (byte*)(bitmapdata.Scan0.ToPointer());
                for (int y = 0; y < bitmapdata.Height; y++)
                {
                    for (int x = 0; x < bitmapdata.Width; x++)
                    {
                        long index = (x / multiple) * 4 + (y / multiple) * srcbitmapdata.Stride;
                        sourcebyte[0] = srcbyte[index];
                        sourcebyte[1] = srcbyte[index + 1];
                        sourcebyte[2] = srcbyte[index + 2];
                        sourcebyte[3] = srcbyte[index + 3];
                        sourcebyte += 4;
                    }
                }
            }
            srcbitmap.UnlockBits(srcbitmapdata);
            bitmap.UnlockBits(bitmapdata);
            return bitmap;
        }

 

運行效果:
0

總結:
c++使用的 undesign char* 最好是作爲接口的返回參數,C#端用IntPtr接收,
接收後,通過Marshal.PtrToStructure(i);轉爲結構體。
結構體裏的指針也是對應c++裏的 undesign char*,這樣接收:
int size = qCode.width * qCode.width + qCode.width - 1;
byte[] managedArray = new byte[size];
Marshal.Copy(qCode.data, managedArray, 0, size);
需要注意,需要知道接收數組的大小,這裏根據源碼得知大小是這樣算的,其他的話就需要在返回參數裏給明白了。
getPngArr,是把數據轉爲二維碼字節數組。
GetMap,原始二維碼圖像大小
Magnifier,方法二維碼圖片尺寸。網上抄的。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章