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 using System.Collections.Generic;
00027
00028 namespace ZBar
00029 {
00033 public class Image : IDisposable
00034 {
00035 private IntPtr handle;
00036
00047 internal Image(IntPtr handle, bool incRef){
00048 this.handle = handle;
00049 if(this.handle == IntPtr.Zero)
00050 throw new Exception("Can't create an image from a null pointer!");
00051
00052 if(incRef)
00053 zbar_image_ref(this.handle, 1);
00054 }
00055
00063 public Image(){
00064 this.handle = zbar_image_create();
00065 if(this.handle == IntPtr.Zero)
00066 throw new Exception("Failed to create new image!");
00067 }
00068
00075 public Image(string filename){
00076 this.handle = zbar_image_read(filename);
00077 if(this.handle == IntPtr.Zero)
00078 throw new Exception("Failed to load image from: " + filename);
00079 }
00080
00084 internal IntPtr Handle{
00085 get{
00086
00087 return this.handle;
00088 }
00089 }
00090
00091 #region Wrapper methods
00092
00107 public void Dump(string filebase){
00108 if(zbar_image_write(this.handle, filebase) != 0)
00109 throw new ZBarException(this.handle);
00110 }
00111
00115 public uint Width{
00116 get{
00117 return zbar_image_get_width(this.handle);
00118 }
00119 set{
00120 zbar_image_set_size(this.handle, value, this.Height);
00121 }
00122 }
00123
00127 public uint Height{
00128 get{
00129 return zbar_image_get_height(this.handle);
00130 }
00131 set{
00132 zbar_image_set_size(this.handle, this.Width, value);
00133 }
00134 }
00135
00140 public uint Format{
00141 get{
00142 return zbar_image_get_format(this.handle);
00143 }
00144 set{
00145 zbar_image_set_format(this.handle, value);
00146 }
00147 }
00148
00152 public uint SequenceNumber{
00153 get{
00154 return zbar_image_get_sequence(this.handle);
00155 }
00156 set{
00157 zbar_image_set_sequence(this.handle, value);
00158 }
00159 }
00160
00165 public byte[] Data{
00166 get{
00167 IntPtr pData = zbar_image_get_data(this.handle);
00168 if(pData == IntPtr.Zero)
00169 throw new Exception("Image data pointer is null!");
00170 uint length = zbar_image_get_data_length(this.handle);
00171 byte[] data = new byte[length];
00172 Marshal.Copy(pData, data, 0, (int)length);
00173 return data;
00174 }
00175 set{
00176 IntPtr data = Marshal.AllocHGlobal(value.Length);
00177 Marshal.Copy(value, 0, data, value.Length);
00178 zbar_image_set_data(this.handle, data, (uint)value.Length, new zbar_image_cleanup_handler(Marshal.FreeHGlobal));
00179 }
00180 }
00181
00185 public IEnumerable<Symbol> Symbols{
00186 get{
00187 IntPtr pSym = zbar_image_first_symbol(this.handle);
00188 while(pSym != IntPtr.Zero){
00189 yield return new Symbol(pSym);
00190 pSym = Symbol.zbar_symbol_next(pSym);
00191 }
00192 }
00193 }
00194
00206 public Image Convert(uint format){
00207 IntPtr img = zbar_image_convert(this.handle, format);
00208 if(img == IntPtr.Zero)
00209 throw new Exception("Conversation failed!");
00210 return new Image(img, false);
00211 }
00212
00213 #endregion
00214
00215 #region IDisposable Implementation
00216
00217
00218
00230 protected virtual void Dispose(bool disposing){
00231 if (this.handle != IntPtr.Zero) {
00232 zbar_image_destroy(this.handle);
00233 this.handle = IntPtr.Zero;
00234 }
00235 if(disposing){
00236
00237 }
00238 }
00239
00243 public void Dispose(){
00244
00245 this.Dispose(true);
00246 GC.SuppressFinalize(this);
00247 }
00248
00252 ~Image(){
00253
00254
00255 this.Dispose(false);
00256 }
00257 #endregion
00258
00259 #region Extern C functions
00267 [DllImport("libzbar")]
00268 private static extern IntPtr zbar_image_create();
00269
00280 [DllImport("libzbar")]
00281 private static extern void zbar_image_destroy(IntPtr image);
00282
00289 [DllImport("libzbar")]
00290 private static extern void zbar_image_ref(IntPtr image, int refs);
00291
00302 [DllImport("libzbar")]
00303 private static extern IntPtr zbar_image_convert(IntPtr image, uint format);
00304
00315 [DllImport("libzbar")]
00316 private static extern IntPtr zbar_image_convert_resize(IntPtr image, uint format, uint width, uint height);
00317
00321 [DllImport("libzbar")]
00322 private static extern uint zbar_image_get_format(IntPtr image);
00323
00326 [DllImport("libzbar")]
00327 private static extern uint zbar_image_get_sequence(IntPtr image);
00328
00332 [DllImport("libzbar")]
00333 private static extern uint zbar_image_get_width(IntPtr image);
00334
00338 [DllImport("libzbar")]
00339 private static extern uint zbar_image_get_height(IntPtr image);
00340
00344 [DllImport("libzbar")]
00345 private static extern IntPtr zbar_image_get_data(IntPtr image);
00346
00349 [DllImport("libzbar")]
00350 private static extern uint zbar_image_get_data_length(IntPtr img);
00351
00357 [DllImport("libzbar")]
00358 private static extern IntPtr zbar_image_first_symbol(IntPtr image);
00359
00366 [DllImport("libzbar")]
00367 private static extern void zbar_image_set_format(IntPtr image, uint format);
00368
00371 [DllImport("libzbar")]
00372 private static extern void zbar_image_set_sequence(IntPtr image, uint sequence_num);
00373
00377 [DllImport("libzbar")]
00378 private static extern void zbar_image_set_size(IntPtr image, uint width, uint height);
00379
00383 private delegate void zbar_image_cleanup_handler(IntPtr image);
00384
00390 [DllImport("libzbar")]
00391 private static extern void zbar_image_set_data(IntPtr image, IntPtr data, uint data_byte_length, zbar_image_cleanup_handler cleanup_handler);
00392
00396 [DllImport("libzbar")]
00397 private static extern void zbar_image_free_data(IntPtr image);
00398
00401 [DllImport("libzbar")]
00402 private static extern void zbar_image_set_userdata(IntPtr image, IntPtr userdata);
00403
00406 [DllImport("libzbar")]
00407 private static extern IntPtr zbar_image_get_userdata(IntPtr image);
00408
00434 [DllImport("libzbar")]
00435 private static extern int zbar_image_write(IntPtr image, string filebase);
00436
00439 [DllImport("libzbar")]
00440 private static extern IntPtr zbar_image_read(string filename);
00441 #endregion
00442 }
00443 }