From f8289c69fbd108809f6bec97658957cf8d1f2483 Mon Sep 17 00:00:00 2001 From: Timur Sizov Date: Sun, 10 Feb 2019 08:18:42 +0200 Subject: [PATCH] Adding "Bounds" property to symbol for giving the user information about location of a barcode in the image --- libzbar-cil/Symbol.cs | 707 +++++++++++++++++++++++------------------- 1 file changed, 385 insertions(+), 322 deletions(-) diff --git a/libzbar-cil/Symbol.cs b/libzbar-cil/Symbol.cs index e6df406..ff8bbba 100755 --- a/libzbar-cil/Symbol.cs +++ b/libzbar-cil/Symbol.cs @@ -1,322 +1,385 @@ -/*------------------------------------------------------------------------ - * Copyright 2009 (c) Jonas Finnemann Jensen - * Copyright 2007-2009 (c) Jeff Brown - * - * This file is part of the ZBar CIL Wrapper. - * - * The ZBar CIL Wrapper is free software; you can redistribute it - * and/or modify it under the terms of the GNU Lesser Public License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * The ZBar CIL Wrapper is distributed in the hope that it will be - * useful, but WITHOUT ANY WARRANTY; without even the implied warranty - * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser Public License for more details. - * - * You should have received a copy of the GNU Lesser Public License - * along with the ZBar CIL Wrapper; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, - * Boston, MA 02110-1301 USA - * - *------------------------------------------------------------------------*/ - -using System; -using System.Runtime.InteropServices; - -namespace ZBar -{ - /// - /// Representation of a decoded symbol - /// - /// This symbol does not hold any references to unmanaged resources. - public class Symbol - { - /// - /// Initialize a symbol from pointer to a symbol - /// - /// - /// Pointer to a symbol - /// - internal Symbol(IntPtr symbol){ - if(symbol == IntPtr.Zero) - throw new Exception("Can't initialize symbol from null pointer."); - - //Get data from the symbol - IntPtr pData = zbar_symbol_get_data(symbol); - int length = (int)zbar_symbol_get_data_length(symbol); - this.data = Marshal.PtrToStringAnsi(pData, length); - - //Get the other fields - this.type = (SymbolType)zbar_symbol_get_type(symbol); - this.quality = zbar_symbol_get_quality(symbol); - this.count = zbar_symbol_get_count(symbol); - } - - private string data; - private int quality; - private int count; - private SymbolType type; - - public override string ToString(){ - return this.type.ToString() + " " + this.data; - } - - #region Public properties - - /// - /// Retrieve current cache count. - /// - /// - /// When the cache is enabled for the image_scanner this provides inter-frame reliability and redundancy information for video streams. - /// < 0 if symbol is still uncertain. - /// 0 if symbol is newly verified. - /// > 0 for duplicate symbols - /// - public int Count{ - get{ - return this.count; - } - } - - /// - /// Data decoded from symbol. - /// - public string Data{ - get{ - return this.data; - } - } - - /// - /// Get a symbol confidence metric. - /// - /// - /// An unscaled, relative quantity: larger values are better than smaller values, where "large" and "small" are application dependent. - /// - public int Quality{ - get{ - return this.quality; - } - } - - /// - /// Type of decoded symbol - /// - public SymbolType Type{ - get{ - return this.type; - } - } - - #endregion - - #region Extern C functions - /// - /// symbol reference count manipulation. - /// - /// - /// increment the reference count when you store a new reference to the - /// symbol. decrement when the reference is no longer used. do not - /// refer to the symbol once the count is decremented and the - /// containing image has been recycled or destroyed. - /// the containing image holds a reference to the symbol, so you - /// only need to use this if you keep a symbol after the image has been - /// destroyed or reused. - /// - [DllImport("libzbar-0")] - private static extern void zbar_symbol_ref(IntPtr symbol, int refs); - - /// - /// retrieve type of decoded symbol. - /// - /// the symbol type - [DllImport("libzbar-0")] - private static extern int zbar_symbol_get_type(IntPtr symbol); - - /// - /// retrieve data decoded from symbol. - /// - /// the data string - [DllImport("libzbar-0")] - private static extern IntPtr zbar_symbol_get_data(IntPtr symbol); - - /// - /// retrieve length of binary data. - /// - /// the length of the decoded data - [DllImport("libzbar-0")] - private static extern uint zbar_symbol_get_data_length(IntPtr symbol); - - /// - /// retrieve a symbol confidence metric. - /// - /// an unscaled, relative quantity: larger values are better - /// than smaller values, where "large" and "small" are application - /// dependent. - /// - /// expect the exact definition of this quantity to change as the - /// metric is refined. currently, only the ordered relationship - /// between two values is defined and will remain stable in the future - /// - [DllImport("libzbar-0")] - private static extern int zbar_symbol_get_quality(IntPtr symbol); - - /// - /// retrieve current cache count. - /// - /// when the cache is enabled for the - /// image_scanner this provides inter-frame reliability and redundancy - /// information for video streams. - /// - /// - /// < 0 if symbol is still uncertain. - /// 0 if symbol is newly verified. - /// > 0 for duplicate symbols - /// - [DllImport("libzbar-0")] - private static extern int zbar_symbol_get_count(IntPtr symbol); - - /// - /// retrieve the number of points in the location polygon. the - /// location polygon defines the image area that the symbol was - /// extracted from. - /// - /// the number of points in the location polygon - /// this is currently not a polygon, but the scan locations - /// where the symbol was decoded - [DllImport("libzbar-0")] - private static extern uint zbar_symbol_get_loc_size(IntPtr symbol); - - /// - /// retrieve location polygon x-coordinates. - /// points are specified by 0-based index. - /// - /// the x-coordinate for a point in the location polygon. - /// -1 if index is out of range - [DllImport("libzbar-0")] - private static extern int zbar_symbol_get_loc_x(IntPtr symbol, uint index); - - /// - /// retrieve location polygon y-coordinates. - /// points are specified by 0-based index. - /// - /// the y-coordinate for a point in the location polygon. - /// -1 if index is out of range - [DllImport("libzbar-0")] - private static extern int zbar_symbol_get_loc_y(IntPtr symbol, uint index); - - /// - /// iterate the result set. - /// - /// the next result symbol, or - /// NULL when no more results are available - /// Marked internal because it is used by the symbol iterators. - [DllImport("libzbar-0")] - internal static extern IntPtr zbar_symbol_next(IntPtr symbol); - - /// - /// print XML symbol element representation to user result buffer. - /// - /// see http://zbar.sourceforge.net/2008/barcode.xsd for the schema. - /// is the symbol to print - /// is the inout result pointer, it will be reallocated - /// with a larger size if necessary. - /// is inout length of the result buffer. - /// the buffer pointer - [DllImport("libzbar-0")] - private static extern IntPtr zbar_symbol_xml(IntPtr symbol, out IntPtr buffer, out uint buflen); - #endregion - } - - /// - /// Different symbol types - /// - [Flags] - public enum SymbolType{ - /// - /// No symbol decoded - /// - None = 0, - - /// - /// Intermediate status - /// - Partial = 1, - - /// - /// EAN-8 - /// - EAN8 = 8, - - /// - /// UPC-E - /// - UPCE = 9, - - /// - /// ISBN-10 (from EAN-13) - /// - ISBN10 = 10, - - /// - /// UPC-A - /// - UPCA = 12, - - /// - /// EAN-13 - /// - EAN13 = 13, - - /// - /// ISBN-13 (from EAN-13) - /// - ISBN13 = 14, - - /// - /// Interleaved 2 of 5. - /// - I25 = 25, - - /// - /// Code 39. - /// - CODE39 = 39, - - /// - /// PDF417 - /// - PDF417 = 57, - - /// - /// QR Code - /// - QRCODE = 64, - - /// - /// Code 128 - /// - CODE128 = 128, - - /// - /// mask for base symbol type - /// - Symbole = 0x00ff, - - /// - /// 2-digit add-on flag - /// - Addon2 = 0x0200, - - /// - /// 5-digit add-on flag - /// - Addon5 = 0x0500, - - /// - /// add-on flag mask - /// - Addon = 0x0700 - } -} +/*------------------------------------------------------------------------ + * Copyright 2009 (c) Jonas Finnemann Jensen + * Copyright 2007-2009 (c) Jeff Brown + * + * This file is part of the ZBar CIL Wrapper. + * + * The ZBar CIL Wrapper is free software; you can redistribute it + * and/or modify it under the terms of the GNU Lesser Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * The ZBar CIL Wrapper is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser Public License for more details. + * + * You should have received a copy of the GNU Lesser Public License + * along with the ZBar CIL Wrapper; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + * + *------------------------------------------------------------------------*/ + +using System; +using System.Drawing; +using System.Runtime.InteropServices; + +namespace ZBarSharp +{ + /// + /// Representation of a decoded symbol + /// + /// This symbol does not hold any references to unmanaged resources. + public class Symbol + { + /// + /// Initialize a symbol from pointer to a symbol + /// + /// + /// Pointer to a symbol + /// + internal Symbol(IntPtr symbol) + { + if (symbol == IntPtr.Zero) + throw new Exception("Can't initialize symbol from null pointer."); + + //Get data from the symbol + IntPtr pData = zbar_symbol_get_data(symbol); + int length = (int)zbar_symbol_get_data_length(symbol); + this.data = Marshal.PtrToStringAnsi(pData, length); + + //Get the other fields + this.type = (SymbolType)zbar_symbol_get_type(symbol); + this.quality = zbar_symbol_get_quality(symbol); + this.count = zbar_symbol_get_count(symbol); + + //Calculate the bounds of symbol location withim the image, in pixels + this.bounds = CalculateBounds(symbol); + + } + + private string data; + private int quality; + private int count; + private SymbolType type; + private Rectangle bounds; + + public override string ToString() + { + return this.type.ToString() + " " + this.data + " Bounds: " + this.Bounds.ToString(); + } + + #region Public properties + + /// + /// Retrieve current cache count. + /// + /// + /// When the cache is enabled for the image_scanner this provides inter-frame reliability and redundancy information for video streams. + /// < 0 if symbol is still uncertain. + /// 0 if symbol is newly verified. + /// > 0 for duplicate symbols + /// + public int Count + { + get + { + return this.count; + } + } + + /// + /// Data decoded from symbol. + /// + public string Data + { + get + { + return this.data; + } + } + + /// + /// Get a symbol confidence metric. + /// + /// + /// An unscaled, relative quantity: larger values are better than smaller values, where "large" and "small" are application dependent. + /// + public int Quality + { + get + { + return this.quality; + } + } + + /// + /// Type of decoded symbol + /// + public SymbolType Type + { + get + { + return this.type; + } + } + + /// + /// Bounding rectangle of the symbol within the containing image. The bounds are given in pixels. + /// + public Rectangle Bounds + { + get + { + return this.bounds; + } + } + + #endregion + + #region Extern C functions + /// + /// symbol reference count manipulation. + /// + /// + /// increment the reference count when you store a new reference to the + /// symbol. decrement when the reference is no longer used. do not + /// refer to the symbol once the count is decremented and the + /// containing image has been recycled or destroyed. + /// the containing image holds a reference to the symbol, so you + /// only need to use this if you keep a symbol after the image has been + /// destroyed or reused. + /// + [DllImport(LinkDefs.ZBarDLL)] + private static extern void zbar_symbol_ref(IntPtr symbol, int refs); + + /// + /// retrieve type of decoded symbol. + /// + /// the symbol type + [DllImport(LinkDefs.ZBarDLL)] + private static extern int zbar_symbol_get_type(IntPtr symbol); + + /// + /// retrieve data decoded from symbol. + /// + /// the data string + [DllImport(LinkDefs.ZBarDLL)] + private static extern IntPtr zbar_symbol_get_data(IntPtr symbol); + + /// + /// retrieve length of binary data. + /// + /// the length of the decoded data + [DllImport(LinkDefs.ZBarDLL)] + private static extern uint zbar_symbol_get_data_length(IntPtr symbol); + + /// + /// retrieve a symbol confidence metric. + /// + /// an unscaled, relative quantity: larger values are better + /// than smaller values, where "large" and "small" are application + /// dependent. + /// + /// expect the exact definition of this quantity to change as the + /// metric is refined. currently, only the ordered relationship + /// between two values is defined and will remain stable in the future + /// + [DllImport(LinkDefs.ZBarDLL)] + private static extern int zbar_symbol_get_quality(IntPtr symbol); + + /// + /// retrieve current cache count. + /// + /// when the cache is enabled for the + /// image_scanner this provides inter-frame reliability and redundancy + /// information for video streams. + /// + /// + /// < 0 if symbol is still uncertain. + /// 0 if symbol is newly verified. + /// > 0 for duplicate symbols + /// + [DllImport(LinkDefs.ZBarDLL)] + private static extern int zbar_symbol_get_count(IntPtr symbol); + + /// + /// retrieve the number of points in the location polygon. the + /// location polygon defines the image area that the symbol was + /// extracted from. + /// + /// the number of points in the location polygon + /// this is currently not a polygon, but the scan locations + /// where the symbol was decoded + [DllImport(LinkDefs.ZBarDLL)] + private static extern uint zbar_symbol_get_loc_size(IntPtr symbol); + + /// + /// retrieve location polygon x-coordinates. + /// points are specified by 0-based index. + /// + /// the x-coordinate for a point in the location polygon. + /// -1 if index is out of range + [DllImport(LinkDefs.ZBarDLL)] + private static extern int zbar_symbol_get_loc_x(IntPtr symbol, uint index); + + /// + /// retrieve location polygon y-coordinates. + /// points are specified by 0-based index. + /// + /// the y-coordinate for a point in the location polygon. + /// -1 if index is out of range + [DllImport(LinkDefs.ZBarDLL)] + private static extern int zbar_symbol_get_loc_y(IntPtr symbol, uint index); + + /// + /// iterate the result set. + /// + /// the next result symbol, or + /// NULL when no more results are available + /// Marked internal because it is used by the symbol iterators. + [DllImport(LinkDefs.ZBarDLL)] + internal static extern IntPtr zbar_symbol_next(IntPtr symbol); + + /// + /// print XML symbol element representation to user result buffer. + /// + /// see http://zbar.sourceforge.net/2008/barcode.xsd for the schema. + /// is the symbol to print + /// is the inout result pointer, it will be reallocated + /// with a larger size if necessary. + /// is inout length of the result buffer. + /// the buffer pointer + [DllImport(LinkDefs.ZBarDLL)] + private static extern IntPtr zbar_symbol_xml(IntPtr symbol, out IntPtr buffer, out uint buflen); + #endregion + + #region Helper functions + + /// + /// Calculates the bounding rect of the decoded symbol + /// + /// + /// + private static Rectangle CalculateBounds(IntPtr symbol) + { + int minX = int.MaxValue, maxX = int.MinValue, minY = int.MaxValue, maxY = int.MinValue; + uint locSize = zbar_symbol_get_loc_size(symbol); + if (locSize == 0) + return Rectangle.Empty; + for (uint i = 0; i < locSize; i++) + { + //Check whether the x value of the current point is on one of the horizontal bounds + int val = zbar_symbol_get_loc_x(symbol, i); + if (val < minX) + minX = val; + if (val > maxX) + maxX = val; + + //Check whether the y value of the current point is on one of the vertical bounds + val = zbar_symbol_get_loc_y(symbol, i); + if (val < minY) + minY = val; + if (val > maxY) + maxY = val; + } + + return new Rectangle(minX, minY, maxX - minX, maxY - minY); + } + + #endregion + } + + /// + /// Different symbol types + /// + [Flags] + public enum SymbolType + { + /// + /// No symbol decoded + /// + None = 0, + + /// + /// Intermediate status + /// + Partial = 1, + + /// + /// EAN-8 + /// + EAN8 = 8, + + /// + /// UPC-E + /// + UPCE = 9, + + /// + /// ISBN-10 (from EAN-13) + /// + ISBN10 = 10, + + /// + /// UPC-A + /// + UPCA = 12, + + /// + /// EAN-13 + /// + EAN13 = 13, + + /// + /// ISBN-13 (from EAN-13) + /// + ISBN13 = 14, + + /// + /// Interleaved 2 of 5. + /// + I25 = 25, + + /// + /// Code 39. + /// + CODE39 = 39, + + /// + /// PDF417 + /// + PDF417 = 57, + + /// + /// QR Code + /// + QRCODE = 64, + + /// + /// Code 128 + /// + CODE128 = 128, + + /// + /// mask for base symbol type + /// + Symbole = 0x00ff, + + /// + /// 2-digit add-on flag + /// + Addon2 = 0x0200, + + /// + /// 5-digit add-on flag + /// + Addon5 = 0x0500, + + /// + /// add-on flag mask + /// + Addon = 0x0700 + } +}