Esempio n. 1
0
        private void CaptureThread()
        {
            int    n_frames = 60;
            int    fps      = 25;
            string result   = "NG";

            dmcam.init(null);
            dmcam.log_cfg(log_level_e.LOG_LEVEL_WARN, log_level_e.LOG_LEVEL_DEBUG,
                          log_level_e.LOG_LEVEL_NONE);

            var devs = new dmcamDevArray(16);
            var cnt  = dmcam.dev_list(devs.cast(), 16);

            Log(string.Format("found {0} device", cnt));

            if (cnt == 0)
            {
                goto FINAL;
            }

            /* open device */
            Log(" Open dmcam device ..");
            var dev = dmcam.dev_open(null);

            if (dev == null)
            {
                Log(" Open device failed");
                goto FINAL;
            }

            Log(string.Format(" Set fps to {0} ..", fps));
            {
                param_item_t pFps = new param_item_t {
                    param_id = dev_param_e.PARAM_FRAME_RATE
                };
                pFps.param_val.frame_rate.fps = (uint)fps;

                dmcamParamArray param = new dmcamParamArray(1);
                param.setitem(0, pFps);

                if (!dmcam.param_batch_set(dev, param.cast(), 1))
                {
                    Log("set fps failed");
                    goto FINAL;
                }

                param.setitem(0, new param_item_t {
                    param_id = dev_param_e.PARAM_FRAME_RATE
                });

                if (!dmcam.param_batch_get(dev, param.cast(), 1))
                {
                    Log("get fps failed");
                    goto FINAL;
                }

                Log(string.Format(" Get fps : {0} ..", param.getitem(0).param_val.frame_rate.fps));
            }

            cap_cfg_t cfg = new cap_cfg_t
            {
                cache_frames_cnt = 10,
                on_error         = null,
                on_frame_ready   = null,
                en_save_replay   = 0,
                en_save_dist_u16 = 0,
                en_save_gray_u16 = 0,
                fname_replay     = null
            };

            dmcam.cap_config_set(dev, cfg);
            //dmcam.cap_set_frame_buffer(dev, null, 10 * 320 * 240 * 4);
            Log(" Start capture ...");
            dmcam.cap_start(dev);

            var f = new byte[640 * 480 * 4 * 3];

            Log(string.Format(" sampling {0} frames ...", n_frames));
            var count = 0;

            while (true)
            {
                // get one frame
                var finfo = new frame_t();
                var ret   = dmcam.cap_get_frames(dev, 1, f, (uint)f.Length, finfo);
                if (ret > 0)
                {
                    var imgW = (int)finfo.frame_info.width;
                    var imgH = (int)finfo.frame_info.height;
                    Console.Write(@" frame @ {0}x{1}, {2} [",
                                  finfo.frame_info.width,
                                  finfo.frame_info.height,
                                  finfo.frame_info.frame_idx);

                    for (var n = 0; n < 16; n++)
                    {
                        Console.Write(@"{0:X2}, ", f[n]);
                    }

                    Console.WriteLine(@"]");

                    var dist = new ushort[imgW * imgH];
                    var gray = new ushort[imgW * imgH];

                    /* calc distance */
                    dmcam.frame_get_dist_u16(dev, dist, dist.Length, f, f.Length, finfo.frame_info);
                    dmcam.frame_get_gray_u16(dev, gray, gray.Length, f, f.Length, finfo.frame_info);

                    for (var n = 0; n < 16; n++)
                    {
                        Console.Write(@"{0:F},", dist[n]);
                    }

                    /* convert depth to pseudo color image */
                    var distRgb = new byte[3 * imgW * imgH];
                    dmcam.cmap_dist_u16_to_RGB(distRgb, distRgb.Length, dist, dist.Length,
                                               cmap_outfmt_e.DMCAM_CMAP_OUTFMT_BGR, 0, 5000, null);
                    /* convert gray to IR image */
                    var grayU8 = new byte[imgW * imgH];
                    dmcam.cmap_gray_u16_to_IR(grayU8, grayU8.Length, gray, gray.Length, 0);

                    {
                        var imgDepth = new Bitmap(imgW, imgH, PixelFormat.Format24bppRgb);
                        var imgIR    = new Bitmap(imgW, imgH, PixelFormat.Format24bppRgb);

                        BitmapData bmData = imgDepth.LockBits(new Rectangle(0, 0, imgDepth.Width, imgDepth.Height),
                                                              ImageLockMode.ReadWrite, imgDepth.PixelFormat);
                        IntPtr pNative = bmData.Scan0;
                        Marshal.Copy(distRgb, 0, pNative, distRgb.Length);
                        imgDepth.UnlockBits(bmData);

//                        bmData = img_ir.LockBits(new Rectangle(0, 0, img_ir.Width, img_ir.Height),
//                            ImageLockMode.ReadWrite, img_ir.PixelFormat);
//                        pNative = bmData.Scan0;
//                        Marshal.Copy(gray_u8, 0, pNative, gray_u8.Length);
//                        img_depth.UnlockBits(bmData);
                        var w = imgW;
                        var h = imgH;
                        for (int y = 0; y < h; y++)
                        {
                            for (int x = 0; x < w; x++)
                            {
                                int r = grayU8[(y * w + x)] & 0xff;
                                int g = grayU8[(y * w + x)] & 0xff;
                                int b = grayU8[(y * w + x)] & 0xff;
                                imgIR.SetPixel(x, y, Color.FromArgb(0, r, g, b));
                            }
                        }

                        RefreshUi(imgDepth, imgIR);
                    }

                    Console.WriteLine(@"]");
                    count += 1;
                    if (count >= n_frames)
                    {
                        break;
                    }
                }
            }

            Log(" Stop capture ...");
            dmcam.cap_stop(dev);
            Log(" Close dmcam device ..");
            dmcam.dev_close(dev);

            dmcam.uninit();
            result = "OK";
FINAL:
            if (_testMode)
            {
                Log(" Loop test " + result);
                Application.Exit();
            }
        }
Esempio n. 2
0
        public static void Main(string[] argv)
        {
            dmcam.init(null);
            dmcam.log_cfg(log_level_e.LOG_LEVEL_WARN, log_level_e.LOG_LEVEL_DEBUG,
                          log_level_e.LOG_LEVEL_NONE);
            dmcamDevArray devs = new dmcamDevArray(16);
            int           cnt  = dmcam.dev_list(devs.cast(), 16);

            Console.Write("found {0} device\n", cnt);

            if (cnt == 0)
            {
                return;
            }
            Console.WriteLine(" Open dmcam device ..");
            dev_t dev = dmcam.dev_open(null);

            if (dev == null)
            {
                Console.WriteLine(" Open device failed");
                return;
            }

            /* get info */
            dev_info_t dev_info = new dev_info_t();

            dmcam.dev_get_info(dev, dev_info);
            dm_u32_p freq_list = dm_u32_p.frompointer(dev_info.calib.freq_list);

            Console.WriteLine("calibrated freq: n={0}, f0={1}", dev_info.calib.n_freq, freq_list.getitem(0));

            /* set param */
            param_item_t p_fps = new param_item_t();

            p_fps.param_id = dev_param_e.PARAM_FRAME_RATE;
            p_fps.param_val.frame_rate.fps = 10;

            param_item_t p_intg = new param_item_t();

            p_intg.param_id = dev_param_e.PARAM_INTG_TIME;
            p_intg.param_val.intg.intg_us = 1000;

            dmcamParamArray wparams = new dmcamParamArray(2);

            wparams.setitem(0, p_fps);
            wparams.setitem(1, p_intg);

            if (!dmcam.param_batch_set(dev, wparams.cast(), 2))
            {
                Console.WriteLine(" set param failed\n");
            }

            /* get param */
            param_item_t r_fps = new param_item_t();

            r_fps.param_id = dev_param_e.PARAM_FRAME_RATE;
            param_item_t r_intg = new param_item_t();

            r_intg.param_id = dev_param_e.PARAM_INTG_TIME;

            dmcamParamArray rparams = new dmcamParamArray(2);

            rparams.setitem(0, r_fps);
            rparams.setitem(1, r_intg);

            if (!dmcam.param_batch_get(dev, rparams.cast(), 2))
            {
                Console.WriteLine(" get param failed\n");
            }
            else
            {
                Console.WriteLine("fps = {0}, intg = {1}",
                                  (int)rparams.getitem(0).param_val.frame_rate.fps,
                                  (int)rparams.getitem(1).param_val.intg.intg_us);
            }

            cap_cfg_t cfg = new cap_cfg_t();

            cfg.cache_frames_cnt = 10;
            cfg.on_error         = null;
            cfg.on_frame_ready   = null;
            cfg.en_save_replay   = 0;
            cfg.en_save_dist_u16 = 0;
            cfg.en_save_gray_u16 = 0;
            cfg.fname_replay     = null;

            dmcam.cap_config_set(dev, cfg);
            //dmcam.cap_set_frame_buffer(dev, null, 10 * 320 * 240 * 4);
            // dmcam.cap_set_callback_on_error(dev, null);
            Console.WriteLine(" Start capture ...");
            dmcam.cap_start(dev);

            byte[] f = new byte[640 * 480 * 4 * 3];
            //ByteBuffer f = ByteBuffer.allocateDirect(1920 * 1080 * 3); [> should be large enough to have one frame <]
            Console.WriteLine(" sampling 100 frames ...");
            int  count = 0;
            bool run   = true;

            while (run)
            {
                // get one frame
                frame_t finfo = new frame_t();
                int     ret   = dmcam.cap_get_frames(dev, 1, f, (uint)f.Length, finfo);
                if (ret > 0)
                {
                    int img_w = (int)finfo.frame_info.width;
                    int img_h = (int)finfo.frame_info.height;
                    Console.Write(" frame @ {0}x{1}, {2} [",
                                  finfo.frame_info.width,
                                  finfo.frame_info.height,
                                  finfo.frame_info.frame_idx);

                    //byte[] first_seg = new byte[16];
                    //f.get(first_seg, 0, 16);
                    for (int n = 0; n < 16; n++)
                    {
                        Console.Write("{0:X2}, ", f[n]);
                    }
                    Console.Write("]\n");

                    ushort[] dist = new ushort[img_w * img_h];

                    //dmcam.raw2dist(dist, dist.length, f, f.capacity());
                    dmcam.frame_get_dist_u16(dev, dist, dist.Length, f, f.Length, finfo.frame_info);
                    for (int n = 0; n < 16; n++)
                    {
                        Console.Write("{0},", dist[n]);
                    }
                    Console.Write("]\n");
                    count += 1;
                    if (count >= 30)
                    {
                        break;
                    }
                }
                // print(" frame @ %d, %d, %d" %
                // (finfo.frame_fbpos, finfo.frame_count, finfo.frame_size))
                // # print the first 16bytes of the frame
                // # print([hex(n) for n in f][:16])
            }

            Console.WriteLine(" Stop capture ...");
            dmcam.cap_stop(dev);
            Console.WriteLine(" Close dmcam device ..");
            dmcam.dev_close(dev);

            // Console.WriteLine("hello world " + cnt);
            dmcam.uninit();
        }