CameraConfig.cs
上传用户:chengzheng
上传日期:2013-08-05
资源大小:38k
文件大小:6k
- using System;
- using System.Text;
- using OpenNETCF.Runtime.InteropServices;
- namespace Cowburn.Imaging
- {
- #region Enums
- /// <summary>
- /// Describes the capture of the camera.
- /// </summary>
- public enum CaptureMode : int
- {
- /// <summary>
- /// Capture still images.
- /// </summary>
- Image = 0,
- /// <summary>
- /// Record a short video clip.
- /// </summary>
- Video = 1,
- }
- /// <summary>
- /// Describes the format of the capture file.
- /// </summary>
- public enum FileType : int
- {
- /// <summary>
- /// Capture images to the JPEG format.
- /// </summary>
- JPEG = 2,
- /// <summary>
- /// Capture video to the AVI format.
- /// </summary>
- AVI = 8,
- }
- /// <summary>
- /// Describes what will be returned from the camera
- /// following successfult capture.
- /// </summary>
- public enum CaptureType : int
- {
- /// <summary>
- /// A path to a capture will be returned following
- /// successful capture.
- /// </summary>
- File = 0,
- /// <summary>
- /// A byte array containing the image data will be
- /// returned following successful capture.
- /// </summary>
- Data = 1,
- }
- /// <summary>
- /// Describes the orientation of the image.
- /// </summary>
- public enum Orientation
- {
- /// <summary>
- /// The image will be displayed in landscape mode.
- /// </summary>
- Landscape = 0,
- /// <summary>
- /// The image will be displayed in portrait mode.
- /// </summary>
- Portrait = 1,
- }
- /// <summary>
- /// Describes the image or video resolution.
- /// </summary>
- public enum Resolution : int
- {
- /// <summary>
- /// The image resolution will be QQCIF (88x72).
- /// </summary>
- QQCIF = 0x00000001,
- /// <summary>
- /// The image resolution will be 128x96.
- /// </summary>
- _128x96 = 0x00000002,
- /// <summary>
- /// The image resolution will be QQVGA (160x120). Supported by Smartphone.
- /// </summary>
- QQVGA = 0x00000004,
- /// <summary>
- /// The image resolution will be QCIF (176x144). Supported by Smartphone.
- /// </summary>
- QCIF = 0x00000008,
- /// <summary>
- /// The image resolution will be QVGA (320x240). Supported by Smartphone.
- /// </summary>
- QVGA = 0x00000010,
- /// <summary>
- /// The image resolution will be CIF (352x288). Supported by Smartphone.
- /// </summary>
- CIF = 0x00000020,
- /// <summary>
- /// The image resolution will be VGA (640x480). Supported by Smartphone.
- /// </summary>
- VGA = 0x00000040,
- }
- #endregion
- internal sealed class CameraConfiguration : IDisposable
- {
- // This is a pointer to a block of unmanaged memory
- // which we will use to create a struct containing the
- // configuration data for the camera to use during
- // image/video capture.
- internal IntPtr _ptr = IntPtr.Zero;
- /// <summary>
- /// Creates an instance of CameraConfiguration and
- /// registers an instance of a MessageWindow-derived
- /// class as the callback window.
- /// </summary>
- /// <param name="hCallbackWnd">A handle to a callback window derived from MessageWindow.</param>
- public CameraConfiguration(IntPtr hCallbackWnd) : this()
- {
- // Define the default configuration for the camera.
- // This is a landscape, JPEG still image at 640x480
- // and captured to a file.
- this.hWndCallback = hCallbackWnd;
- this.CaptureMode = CaptureMode.Image;
- this.FileType = FileType.JPEG;
- this.Orientation = Orientation.Landscape;
- this.Resolution = Resolution.VGA;
- this.VideoSize = 0;
- this.SaveFolder = string.Empty;
- this.CaptureType = CaptureType.File;
- }
- internal CameraConfiguration()
- {
- // Allocate 548 bytes of unmanaged memory for
- // our struct
- _ptr = MarshalEx.AllocHGlobal(548);
- }
- #region Properties
- /// <summary>
- /// Get/Set the handle to the callback window.
- /// </summary>
- public IntPtr hWndCallback
- {
- get { return MarshalEx.ReadIntPtr(_ptr, 0); }
- set { MarshalEx.WriteIntPtr(_ptr, 0, value); }
- }
- /// <summary>
- /// Get/Set the CaptureMode.
- /// </summary>
- public CaptureMode CaptureMode
- {
- get { return (CaptureMode)MarshalEx.ReadInt32(_ptr, 4); }
- set { MarshalEx.WriteInt32(_ptr, 4, Convert.ToInt32(value)); }
- }
- /// <summary>
- /// Get/Set the FileType
- /// </summary>
- public FileType FileType
- {
- get { return (FileType)MarshalEx.ReadInt32(_ptr, 8); }
- set { MarshalEx.WriteInt32(_ptr, 8, Convert.ToInt32(value)); }
- }
- /// <summary>
- /// Get/Set the capture orientation
- /// </summary>
- internal Orientation Orientation
- {
- get { return (Orientation)MarshalEx.ReadInt32(_ptr, 12); }
- set { MarshalEx.WriteInt32(_ptr, 12, Convert.ToInt32(value)); }
- }
- /// <summary>
- /// Get/Set the capture resolution
- /// </summary>
- public Resolution Resolution
- {
- get { return (Resolution)MarshalEx.ReadInt32(_ptr, 16); }
- set { MarshalEx.WriteInt32(_ptr, 16, Convert.ToInt32(value)); }
- }
- /// <summary>
- /// Get/Set maximum size of the file when capturing video
- /// </summary>
- public int VideoSize
- {
- get { return MarshalEx.ReadInt32(_ptr, 20); }
- set { MarshalEx.WriteInt32(_ptr, 20, value); }
- }
- /// <summary>
- /// Get/Set the type of capture to perform
- /// </summary>
- public CaptureType CaptureType
- {
- get { return (CaptureType)MarshalEx.ReadInt32(_ptr, 24); }
- set { MarshalEx.WriteInt32(_ptr, 24, Convert.ToInt32(value)); }
- }
- /// <summary>
- /// Get/Set default folder to store file-based captures in.
- /// </summary>
- public string SaveFolder
- {
- get
- {
- byte[] buffer = MarshalEx.ReadByteArray(_ptr, 0, 520);
- return Encoding.Unicode.GetString(buffer, 0, buffer.Length).Trim(' ');
- }
- set
- {
- byte[] buffer = Encoding.Unicode.GetBytes(value);
- MarshalEx.WriteByteArray(_ptr, 28, buffer);
- }
- }
- #endregion
- /// <summary>
- /// Returns the pointer to the unmanaged struct
- /// containing the configuration data.
- /// </summary>
- public IntPtr ToPointer()
- {
- return _ptr;
- }
- public void Dispose()
- {
- // Unallocate the unmanaged memory if we have
- // previously a block of memory
- if(_ptr != IntPtr.Zero)
- {
- MarshalEx.FreeHGlobal(_ptr);
- _ptr = IntPtr.Zero;
- }
- }
- }
- }