C# 佳能(Canon)相机EDSDK应用开发(二)--Live view与拍照

来源:互联网 发布:sai mac下载 编辑:程序博客网 时间:2024/04/29 14:05
上一节看过了基本的应用开发流程,本节来看一下如何控制相机进行实时预览及拍照

一、拍照

   太EASY了,就这么一句:

   EDSDK.EdsSendCommand(camera, EDSDK.CameraCommand_TakePicture, 0);

   *注意:相机应确保在MF上。

二、实时预览(Live view)

   就是将evf(Electronic Viewfinder--电子取景器)输出到PC上显示。

1、设置输出设备为PC:

            uint _data;
            EDSDK.EdsDataType _dataType;
            int _dataSize;

            //先获取原来的EVF输出设备属性的信息

            _hr = EDSDK.EdsGetPropertySize(camera, EDSDK.PropID_Evf_OutputDevice, 0, out _dataType, out _dataSize);
            _hr = EDSDK.EdsGetPropertyData(this._camera, EDSDK.PropID_Evf_OutputDevice, 0, out _data);
            IntPtr _device = new IntPtr();
            if (_hr == EDSDK.EDS_ERR_OK)
            {

              //设置输出到PC
                _device = new IntPtr(_data | EDSDK.EvfOutputDevice_PC);
                _hr = EDSDK.EdsSetPropertyData(camera, EDSDK.PropID_Evf_OutputDevice, 0, Marshal.SizeOf(_device), _device);

            }

2、在UI中显示

         *Live view到PC,需要不停的下载图象到PC,所以需要有个线程来做,用ThreadStart类。

       private unsafe void DownloadEvfData()
        {
            //开始实时取景
            while (true)
            {
                IntPtr _stream = IntPtr.Zero;
                IntPtr _image = new IntPtr();
                IntPtr _imageData = new IntPtr();
                uint _imageLen;
                if (_hr == EDSDK.EDS_ERR_OK)
                {
                    _hr = EDSDK.EdsCreateMemoryStream(0, out _stream);
                }
                if (_hr == EDSDK.EDS_ERR_OK)
                {
                    _hr = EDSDK.EdsCreateEvfImageRef(_stream, out _image);
                }
                //下载
                if (_hr == EDSDK.EDS_ERR_OK)
                {

                //此处需要图象从内存流已经准备好

                 EDSDK.EdsDownloadEvfImage(this._camera, _image);
                }
                //显示图象 
                EDSDK.EdsGetPointer(_stream, out _imageData);
                EDSDK.EdsGetLength(_stream, out _imageLen);

                UnmanagedMemoryStream ums = new UnmanagedMemoryStream((byte*)_imageData.ToPointer(), _imageLen, _imageLen, FileAccess.Read);
                _bmp = new Bitmap(ums, true);

                //_picBox是定义的一个pictureBox 控件

                _picBox.Image = _bmp;

                if (_stream != IntPtr.Zero)
                {
                    EDSDK.EdsRelease(_stream);
                    _stream = IntPtr.Zero;
                }
                if (_image != IntPtr.Zero)
                {
                    EDSDK.EdsRelease(_image);
                    _image = IntPtr.Zero;
                }
            }
        }

原创粉丝点击