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
00027 namespace ZBar
00028 {
00032 public class ImageScanner : IDisposable
00033 {
00034 private IntPtr handle = IntPtr.Zero;
00035 private bool cache = false;
00036
00040 public ImageScanner(){
00041 this.handle = zbar_image_scanner_create();
00042 if(this.handle == IntPtr.Zero)
00043 throw new Exception("Failed to create an underlying image_scanner!");
00044 }
00045
00046 #region Wrapper methods
00047
00058 public int Scan(Image image){
00059 int count = zbar_scan_image(this.handle, image.Handle);
00060 if(count < 0)
00061 throw new Exception("Image scanning failed!");
00062 return count;
00063 }
00064
00073 public bool Cache{
00074 get{
00075 return this.cache;
00076 }
00077 set{
00078 zbar_image_scanner_enable_cache(this.handle, value ? 1 : 0);
00079 this.cache = value;
00080 }
00081 }
00082
00086 public void SetConfiguration(SymbolType symbology, Config config, int value){
00087 if(zbar_image_scanner_set_config(this.handle, (int)symbology, (int)config, value) != 0)
00088 throw new Exception("Failed to set configuration");
00089 }
00090
00091 #endregion
00092
00093 #region IDisposable Implementation
00094
00095
00096
00108 protected virtual void Dispose(bool disposing){
00109 if(this.handle != IntPtr.Zero){
00110 zbar_image_scanner_destroy(this.handle);
00111 this.handle = IntPtr.Zero;
00112 }
00113 if(disposing){
00114
00115 }
00116 }
00117
00121 public void Dispose(){
00122
00123 this.Dispose(true);
00124 GC.SuppressFinalize(this);
00125 }
00126
00130 ~ImageScanner(){
00131
00132
00133 this.Dispose(false);
00134 }
00135 #endregion
00136
00137 #region Extern C functions
00141 [DllImport("libzbar")]
00142 private static extern IntPtr zbar_image_scanner_create();
00143
00147 [DllImport("libzbar")]
00148 private static extern void zbar_image_scanner_destroy(IntPtr scanner);
00149
00153 private delegate void zbar_image_data_handler(IntPtr image, IntPtr userdata);
00154
00162 [DllImport("libzbar")]
00163 private static extern zbar_image_data_handler zbar_image_scanner_set_data_handler(IntPtr scanner, zbar_image_data_handler handler, IntPtr userdata);
00164
00165
00172 [DllImport("libzbar")]
00173 private static extern int zbar_image_scanner_set_config(IntPtr scanner, int symbology, int config, int val);
00174
00182 [DllImport("libzbar")]
00183 private static extern void zbar_image_scanner_enable_cache(IntPtr scanner, int enable);
00184
00193 [DllImport("libzbar")]
00194 private static extern int zbar_scan_image(IntPtr scanner, IntPtr image);
00195 #endregion
00196 }
00197
00201 public enum Config{
00205 Enable = 0,
00206
00210 AddCheck,
00211
00215 EmitCheck,
00216
00220 ASCII,
00221
00225 Num,
00226
00230 MinimumLength = 0x20,
00231
00235 MaximumLength,
00236
00240 Position = 0x80,
00241
00245 XDensity = 0x100,
00246
00250 YDensity
00251 }
00252
00253 }