00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 using System;
00025 using System.Runtime.InteropServices;
00026
00030 namespace ZBar
00031 {
00035 public class Video : IDisposable
00036 {
00037 private IntPtr video = IntPtr.Zero;
00038 private bool enabled = false;
00039
00043 public Video(){
00044 this.video = zbar_video_create();
00045 if(this.video == IntPtr.Zero)
00046 throw new Exception("Didn't create an unmanaged Video instance, don't know what happened.");
00047 }
00048
00049 #region Wrapper methods
00050
00057 public void Open(string device){
00058 if(zbar_video_open(this.video, device) != 0){
00059 throw new ZBarException(this.video);
00060 }
00061 }
00062
00066 public void Close(){
00067 if(zbar_video_open(this.video, null) != 0){
00068 throw new ZBarException(this.video);
00069 }
00070 }
00071
00075 public bool Enabled{
00076 set{
00077 if(zbar_video_enable(this.video, value ? 1 : 0) != 0)
00078 throw new ZBarException(this.video);
00079 this.enabled = value;
00080 }
00081 get{
00082 return this.enabled;
00083 }
00084 }
00085
00089 public int Width{
00090 get{
00091 int width = zbar_video_get_width(this.video);
00092 if(width == 0)
00093 throw new Exception("Video device not opened!");
00094 return width;
00095 }
00096 }
00097
00101 public int Height{
00102 get{
00103 int height = zbar_video_get_height(this.video);
00104 if(height == 0)
00105 throw new Exception("Video device not opened!");
00106 return height;
00107 }
00108 }
00109
00122 public void RequestSize(uint width, uint height){
00123 if(zbar_video_request_size(this.video, width, height) != 0)
00124 throw new ZBarException(this.video);
00125 }
00126
00134 public Image NextFrame(){
00135 IntPtr image = zbar_video_next_image(this.video);
00136 if(image == IntPtr.Zero)
00137 throw new ZBarException(this.video);
00138 return new Image(image, false);
00139 }
00140
00141 #endregion
00142
00143 #region IDisposable Implementation
00144
00145
00146
00158 protected virtual void Dispose(bool disposing){
00159 if(this.video != IntPtr.Zero){
00160 zbar_video_destroy(this.video);
00161 this.video = IntPtr.Zero;
00162 }
00163 if(disposing){
00164
00165 }
00166 }
00167
00171 public void Dispose(){
00172
00173 this.Dispose(true);
00174 GC.SuppressFinalize(this);
00175 }
00176
00180 ~Video(){
00181
00182
00183 this.Dispose(false);
00184 }
00185 #endregion
00186
00187 #region Extern C functions
00188
00190 [DllImport("libzbar")]
00191 private static extern IntPtr zbar_video_create();
00192
00194 [DllImport("libzbar")]
00195 private static extern void zbar_video_destroy(IntPtr video);
00196
00204 [DllImport("libzbar")]
00205 private static extern int zbar_video_open(IntPtr video, string device);
00206
00214 [DllImport("libzbar")]
00215 private static extern int zbar_video_get_fd(IntPtr video);
00216
00223 [DllImport("libzbar")]
00224 private static extern int zbar_video_request_size(IntPtr video, uint width, uint height);
00225
00229 [DllImport("libzbar")]
00230 private static extern int zbar_video_request_interface(IntPtr video, int version);
00231
00245 [DllImport("libzbar")]
00246 private static extern int zbar_video_request_iomode(IntPtr video, int iomode);
00247
00252 [DllImport("libzbar")]
00253 private static extern int zbar_video_get_width(IntPtr video);
00254
00259 [DllImport("libzbar")]
00260 private static extern int zbar_video_get_height(IntPtr video);
00261
00267 [DllImport("libzbar")]
00268 private static extern int zbar_video_init(IntPtr video, uint format);
00269
00275 [DllImport("libzbar")]
00276 private static extern int zbar_video_enable(IntPtr video, int enable);
00277
00282 [DllImport("libzbar")]
00283 private static extern IntPtr zbar_video_next_image(IntPtr video);
00284
00285 #endregion
00286 }
00287 }