예제 #1
0
        public override void DisConnect()
        {
            if (_camera.handle != IntPtr.Zero)
            {
                SniCamResult sResult = DS_U3Wrapper.SniCamReleaseCamera(_camera.handle);
                sResult        = DS_U3Wrapper.SniCamTerminate();
                _camera.handle = IntPtr.Zero;
            }

            //unload the SniCam.dll
            //IntPtr nikonDll = LoadLibrary("SniCam.dll");
            //FreeLibrary(nikonDll);
            //FreeLibrary(nikonDll);
        }
예제 #2
0
        bool AutoManual(int featureId, bool auto)
        {
            bool result = false;

            DS_U3Wrapper.SniFeatureState state = new DS_U3Wrapper.SniFeatureState();
            DS_U3Wrapper.SniCamGetState(_camera.handle, featureId, out state);

            if (auto)
            {
                if ((state.bits & 0x02000000U) != 0)
                {
                    state.bits |= (uint)NikonFeatureState.sniAutoState;
                    SniCamResult res = DS_U3Wrapper.SniCamSetState(_camera.handle, featureId, state);
                    if (res != SniCamResult.SNI_OK)
                    {
                        MessageBox.Show("Failed to set state, Error Code: " + res, "Error");
                    }
                    else
                    {
                        result = true;
                    }
                }
            }
            else
            {
                if ((state.bits & 0x01000000U) != 0)
                {
                    state.bits &= ~((uint)NikonFeatureState.sniAutoState);
                    SniCamResult res = DS_U3Wrapper.SniCamSetState(_camera.handle, featureId, state);
                    if (res != SniCamResult.SNI_OK)
                    {
                        MessageBox.Show("Failed to set state, Error Code: " + res, "Error");
                    }
                    else
                    {
                        result = true;
                    }
                }
            }

            return(result);
        }
예제 #3
0
        bool OnePush(int featureId)
        {
            bool result = false;

            DS_U3Wrapper.SniFeatureState state = new DS_U3Wrapper.SniFeatureState();
            DS_U3Wrapper.SniCamGetState(_camera.handle, featureId, out state);
            if ((state.bits & 0x10000000U) != 0)
            {
                state.bits |= (uint)NikonFeatureState.sniOnePushState;
                SniCamResult res = DS_U3Wrapper.SniCamSetState(_camera.handle, featureId, state);
                if (res != SniCamResult.SNI_OK)
                {
                    MessageBox.Show("Failed to set OnePush, Error Code: " + res, "Error");
                }
                else
                {
                    result = true;
                }
            }

            return(result);
        }
예제 #4
0
        public override BitmapSource GetImage(DoWorkEventArgs e)
        {
            NikonCameraImage helper = (NikonCameraImage)e.Argument;
            BitmapSource     source = null, orginalSource = null;

            try
            {
                SniCamResult sResult = DS_U3Wrapper.SniCamGetNextImage(helper.camera.handle,
                                                                       helper.stop, out helper.format, out helper.index, ref helper.image);

                if (sResult < SniCamResult.SNI_OK)
                {
                    throw new Exception("SniCamGetNextImage returned " + sResult);
                }

                DS_U3Wrapper.SniImageFormat format = (DS_U3Wrapper.SniImageFormat)Marshal.PtrToStructure(
                    helper.format, typeof(DS_U3Wrapper.SniImageFormat));

                //Allocate space for image buffer
                IntPtr buffer = IntPtr.Zero;
                byte[] managedBuffer;
                try
                {
                    buffer  = Marshal.AllocHGlobal(2 * format.buf_size.ToInt32());
                    sResult = DS_U3Wrapper.SniCamGetImageData(helper.camera.handle,
                                                              helper.image, (uint)(2 * format.buf_size.ToInt32()), ref buffer);
                    if (sResult != SniCamResult.SNI_OK)
                    {
                        DS_U3Wrapper.SniCamReleaseImage(helper.camera.handle, ref helper.image);
                        throw new Exception("SniCamGetImageData returned " + sResult);
                    }

                    if (format.mode == ESniColorMode.sniRgb24)
                    {
                        managedBuffer = new byte[format.buf_size.ToInt32()];
                        Marshal.Copy(buffer, managedBuffer, 0, format.buf_size.ToInt32());

                        System.Drawing.Bitmap             bmp     = new System.Drawing.Bitmap(format.width, format.height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                        System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(
                            new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
                            System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat);
                        Marshal.Copy(managedBuffer, 0, bmpData.Scan0, managedBuffer.Length);
                        bmp.UnlockBits(bmpData);

                        IntPtr            hBitmap     = bmp.GetHbitmap();
                        BitmapSizeOptions sizeOptions = BitmapSizeOptions.FromEmptyOptions();
                        try
                        {
                            orginalSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero,
                                                                                                         System.Windows.Int32Rect.Empty, sizeOptions);
                        }
                        finally
                        {
                            DeleteObject(hBitmap);
                        }

                        if ((Properties.Settings.Default.CropHeight == 0 && Properties.Settings.Default.CropWidth == 0) ||
                            (Properties.Settings.Default.CropHeight + Properties.Settings.Default.CropTop > orginalSource.Height) ||
                            (Properties.Settings.Default.CropWidth + Properties.Settings.Default.CropLeft > orginalSource.Width)
                            )
                        {
                            source          = orginalSource;
                            CropImageWidth  = 0;
                            CropImageHeight = 0;
                        }
                        else
                        {
                            source = new CroppedBitmap(orginalSource,
                                                       new System.Windows.Int32Rect((int)Properties.Settings.Default.CropLeft,
                                                                                    (int)Properties.Settings.Default.CropTop,
                                                                                    (int)(Properties.Settings.Default.CropWidth == 0 ?
                                                                                          orginalSource.Width : Properties.Settings.Default.CropWidth),
                                                                                    (int)(Properties.Settings.Default.CropHeight == 0 ?
                                                                                          orginalSource.Height : Properties.Settings.Default.CropHeight)));
                            CropImageWidth  = source.Width;
                            CropImageHeight = source.Height;
                        }
                        source.Freeze();
                        ImageWidth  = orginalSource.Width;
                        ImageHeight = orginalSource.Height;

                        sResult = DS_U3Wrapper.SniCamReleaseImage(helper.camera.handle, ref helper.image);
                        if (sResult != SniCamResult.SNI_OK)
                        {
                            throw new Exception("SniCamReleaseImage returned " + sResult);
                        }
                    }
                    else
                    {
                        App.LogEntry.AddEntry("Nikon Camera is not in ESniColorMode.sniRgb24 - please reset it.");
                    }
                }
                catch (Exception /*ex*/)
                {
                    throw;
                }
                finally
                {
                    if (buffer != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(buffer);
                        buffer = IntPtr.Zero;
                    }
                }
            }
            catch (Exception ex)
            {
                App.LogEntry.AddEntry("Failed to get image: " + ex.Message);
            }
            finally
            {
            }

            return(source);
        }
예제 #5
0
        public override bool Connect()
        {
            bool result = false;

            try
            {
                //discover camera
                _camera = new DS_U3Wrapper.SniCameraInfo();
                Int32        cameraCount             = 0;
                IntPtr       unmanagedCameraInfoAddr = IntPtr.Zero;
                SniCamResult sResult = DS_U3Wrapper.SniCamDiscoverCameras(out unmanagedCameraInfoAddr, out cameraCount);

                if (sResult != SniCamResult.SNI_OK)
                {
                    throw new Exception("Could not connect to the DS-U3 successfully");
                }
                if (cameraCount == 0)
                {
                    throw new Exception("DS-U3 not available or no cameras connected.");
                }

                // Marshal the data at the returned pointer into a managed object
                _camera = (DS_U3Wrapper.SniCameraInfo)Marshal.PtrToStructure(unmanagedCameraInfoAddr, typeof(DS_U3Wrapper.SniCameraInfo));

                unmanagedCameraInfoAddr = IntPtr.Zero;

                //open camera
                IntPtr cameraHandle = IntPtr.Zero;
                sResult = DS_U3Wrapper.SniCamOpenCamera(ref _camera, out cameraHandle);

                if (sResult != SniCamResult.SNI_OK)
                {
                    throw new Exception("Could not open Camera Handle");
                }

                _camera.handle = cameraHandle;

                // Need to check status of camera. According to camera logs,
                // if the Nikon software finds the camera in a status other
                // than available, it requests the driver version, then closes
                // and opens the camera. Not sure why, but prevents the controller
                // from simply preventing communication with the camera.
                if (_camera.status != 0)
                {
                    StringBuilder version = new StringBuilder(34);
                    DS_U3Wrapper.SniCamGetDriverVersion(_camera.handle, version);
                    DS_U3Wrapper.SniCamCloseCamera(_camera.handle);
                    _camera.handle = IntPtr.Zero;
                    sResult        = DS_U3Wrapper.SniCamOpenCamera(ref _camera, out cameraHandle);

                    if (sResult != SniCamResult.SNI_OK)
                    {
                        throw new Exception("Could not open Camera Handle");
                    }

                    _camera.handle = cameraHandle;
                }


                _features = new Dictionary <Features, DS_U3Wrapper.SniFeature>();

                IntPtr p;

                sResult = DS_U3Wrapper.SniCamGetFeatures(_camera.handle, out p);
                if (sResult != SniCamResult.SNI_OK)
                {
                    throw new Exception("Could not get Camera features");
                }

                int numFeatures = Marshal.ReadInt32(p);
                //move to first feature element
                p = (IntPtr)(p.ToInt32() + Marshal.SizeOf(typeof(DS_U3Wrapper.SniFeatures)) -
                             Marshal.SizeOf(typeof(IntPtr)));

                // Setup a mapping from feature name to feature ID number, in order to more easily
                // access features by name.
                for (int i = 0; i < numFeatures; i++)
                {
                    DS_U3Wrapper.SniFeature feature = (DS_U3Wrapper.SniFeature)Marshal.PtrToStructure(p,
                                                                                                      typeof(DS_U3Wrapper.SniFeature));

                    foreach (Features featureName in Enum.GetValues(typeof(Features)))
                    {
                        if (featureName.ToString().Equals(feature.name))
                        {
                            _features.Add(featureName, feature);
                        }
                    }

                    //move to next feature element
                    p = (IntPtr)(p.ToInt32() + Marshal.SizeOf(typeof(DS_U3Wrapper.SniFeature)));
                }


                //initialise settings
                InitializeSettings();

                result = true;
            }
            catch (Exception /*ex*/)
            {
                if (_camera.handle != IntPtr.Zero)
                {
                    DS_U3Wrapper.SniCamCloseCamera(_camera.handle);
                    _camera.handle = IntPtr.Zero;
                }
                result = false;
            }


            return(result);
        }