图漾双目相机 C#拍照 并保存入sqlite数据库

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using pcammls;
using SDK = pcammls.pcammls;
using System.IO;
using System.Data.OleDb;
using System.Data.SQLite;
using System.Data.Common;
 private void button启动相机_Click(object sender, EventArgs e)
        {
           
            SDK.TYInitLib();
            TY_VERSION_INFO info = new TY_VERSION_INFO();
            SDK.TYLibVersion(info);
            richTextBox1.Text = info.major.ToString() + info.minor.ToString() + info.patch.ToString();
            DeviceInfoVector devs = new DeviceInfoVector();
            SDK.selectDevice(SDK.TY_INTERFACE_ALL, "", "", 10, devs);
            int sz = devs.Count();
            richTextBox1.Text = richTextBox1.Text + "  " + sz.ToString();
            //选相机0
            var item = devs[0];
            richTextBox1.Text = richTextBox1.Text + "  " + item.id.ToString() + item.modelName.ToString();
            var dev_info = item;
            IntPtr dev_handle = new IntPtr();
            IntPtr iface_handle = new IntPtr();
            SDK.TYOpenInterface(dev_info.iface.id, ref iface_handle);
            SDK.TYOpenDevice(iface_handle, dev_info.id, ref dev_handle);

            //测量
            IntPtr handle = dev_handle;
            SDK.TYEnableComponents(handle, SDK.TY_COMPONENT_DEPTH_CAM);
            List<uint8_t_ARRAY> buffers = new List<uint8_t_ARRAY>();
            uint buff_sz;
            SDK.TYGetFrameBufferSize(handle, out buff_sz);
            buffers.Add(new uint8_t_ARRAY((int)buff_sz));
            buffers.Add(new uint8_t_ARRAY((int)buff_sz));
            SDK.TYEnqueueBuffer(handle, buffers[0].VoidPtr(), buff_sz);
            SDK.TYEnqueueBuffer(handle, buffers[1].VoidPtr(), buff_sz);
            SDK.TYStartCapture(handle);

            //深度图
            TY_FRAME_DATA frame = new TY_FRAME_DATA();
            SDK.TYFetchFrame(handle, frame, 5000);
            var images = frame.image;
            var img = images[0];
            var pixel_arr = uint16_t_ARRAY.FromVoidPtr(img.buffer);
            int offset = img.width * img.height / 2 + img.width / 2;
            ushort distance = pixel_arr[offset];
            TXTdistance1.Text = distance.ToString(); //图像中心点
            txt宽度.Text = img.width.ToString(); //640    
            txt高度.Text = img.height.ToString(); //480

            //坐标 299.282 值 871 程序为 int offset2 = img.width * 282 + 299;
            int offset2 = img.width * 282 + 299;
            ushort distance2 = pixel_arr[offset2];
            textBox距离.Text = distance2.ToString();

            //坐标117,212
            int offset3 = img.width * 212 + 117;
            ushort distance3 = pixel_arr[offset2];
            textBox距离.Text = distance2.ToString();
            TXT指定点距离1.Text = distance3.ToString();
            

            //FileStream fs = new FileStream("E:\\一维文本.txt", FileMode.Create);
            //StreamWriter sw = new StreamWriter(fs);

            SDK.TYDeinitLib();
            conn.Open();
            del数据();
            DbTransaction trans = conn.BeginTransaction(); // <-------------------
            //开始写入
            //sw.Write("Hello World!!!!" + "\r\n");
            //sw.Write("Hello World2!!!!"+ "\r\n");
            for (int= 0;<= 479;++)
            {
                for (int= 0;<= 639;++)
                {
                    int 深度地址 = img.width *+;
                    ushort 深度 = pixel_arr[深度地址];

                  // sw.Write(宽.ToString() + " " + 高.ToString() + " " + 深度.ToString() + "\r\n");
                   插入数据(.ToString(),.ToString(), 深度.ToString());
                }


            }

            //清空缓冲区
          // sw.Flush();
            //关闭流
           // sw.Close();
          // fs.Close();
            trans.Commit(); 
            conn.Close();
            textBox1.Text = "DONE";
           
        }
        static string path3 = System.IO.Directory.GetCurrentDirectory();
        static string databaseFileName = path3 + "\\DIANYUN.db";
        static string connectionString = "data source = " + databaseFileName;
        static SQLiteConnection conn = new SQLiteConnection(connectionString);
        static string query = "insert into ZHAOPIAN1 (x,y,z) values( ";
        static SQLiteCommand cmd = new SQLiteCommand(query, conn);
        private void 插入数据(string 数据1, string 数据2, string 数据3)
        {
           
           
          
            query = "insert into ZHAOPIAN1 (x,y,z) values( ";
            query = query + 数据1 + ", ";
            query = query + 数据2 + ", ";
            query = query + 数据3 + ") ";

            cmd.CommandText = query;
            cmd.ExecuteNonQuery();
            

        }

你可能感兴趣的:(图漾双目相机)