1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/****************************************************************************************
 *
 *  Rust Wrapper for the ZBar Barcode Reader
 *  Copyright 2018 - Justin Ferguson <jferg@newcontext.com>
 *
 *  This is a rudimentary rust wrapper around the ZBar Bar Code reader.  It is not complete,
 *  and, to be honest, its use is likely to lead to much wailing and gnashing of teeth; it's
 *  the first thing I've written in rust and is probably broken in numerous ways.
 *
 *  I am releasing it simply so I can include it as a crate in another project.  If it's
 *  useful to you, enjoy.
 *
 *  The Rust Wrapper for the ZBar Bar Code Reader 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 Rust Wrapper for the ZBar Bar Code Reader; if not, write
 *  to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 *  Boston, MA  02110-1301  USA
 */

#![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)]
#![allow(non_snake_case)]

extern crate libc;
extern crate png;

use self::libc::{c_void, c_int, c_char};
use std::ffi::CStr;

#[repr(C)] pub struct zbar_image_scanner_t { _private: [u8; 0] }
#[repr(C)] pub struct zbar_image_data_handler_t { _private: [u8; 0]}
#[repr(C)] pub struct zbar_symbol_t { _private: [u8; 0]}
#[repr(C)] pub struct zbar_symbol_set_t { _private: [u8; 0]}
#[repr(C)] pub struct zbar_image_t { _private: [u8; 0]}
#[repr(C)] pub struct zbar_cleanup_handler_t { _private: [u8; 0]}

#[repr(C)]
pub enum zbar_color_e {
    ZBAR_SPACE = 0,
    ZBAR_BAR = 1
}

/** decoded symbol type. */
#[repr(C)]
pub enum zbar_symbol_type_e {
    ZBAR_NONE        =      0,  // no symbol decoded
    ZBAR_PARTIAL     =      1,  // intermediate status
    ZBAR_EAN8        =      8,  // EAN-8
    ZBAR_UPCE        =      9,  // UPC-E
    ZBAR_ISBN10      =     10,  // ISBN-10 (from EAN-13). @since 0.4
    ZBAR_UPCA        =     12,  // UPC-A
    ZBAR_EAN13       =     13,  // EAN-13
    ZBAR_ISBN13      =     14,  // ISBN-13 (from EAN-13). @since 0.4
    ZBAR_I25         =     25,  // Interleaved 2 of 5. @since 0.4
    ZBAR_CODE39      =     39,  // Code 39. @since 0.4
    ZBAR_PDF417      =     57,  // PDF417. @since 0.6
    ZBAR_QRCODE      =     64,  // QR Code. @since 0.10
    ZBAR_CODE128     =    128,  // Code 128
    ZBAR_SYMBOL      = 0x00ff,  // mask for base symbol type
    ZBAR_ADDON2      = 0x0200,  // 2-digit add-on flag
    ZBAR_ADDON5      = 0x0500,  // 5-digit add-on flag
    ZBAR_ADDON       = 0x0700,  // add-on flag mask
}

/** error codes. */
#[repr(C)]
pub enum zbar_error_e {
    ZBAR_OK = 0,                // no error
    ZBAR_ERR_NOMEM,             // out of memory
    ZBAR_ERR_INTERNAL,          // internal library error
    ZBAR_ERR_UNSUPPORTED,       // unsupported request
    ZBAR_ERR_INVALID,           // invalid request
    ZBAR_ERR_SYSTEM,            // system error
    ZBAR_ERR_LOCKING,           // locking error
    ZBAR_ERR_BUSY,              // all resources busy
    ZBAR_ERR_XDISPLAY,          // X11 display error
    ZBAR_ERR_XPROTO,            // X11 protocol error
    ZBAR_ERR_CLOSED,            // output window is closed
    ZBAR_ERR_WINAPI,            // windows system error
    ZBAR_ERR_NUM                // number of error codes
}

/** decoder configuration options.
 */
#[repr(C)]
pub enum zbar_config_e {
    ZBAR_CFG_ENABLE = 0,        // enable symbology/feature
    ZBAR_CFG_ADD_CHECK,         // enable check digit when optional
    ZBAR_CFG_EMIT_CHECK,        // return check digit when present
    ZBAR_CFG_ASCII,             // enable full ASCII character set
    ZBAR_CFG_NUM,               // number of boolean decoder configs
    ZBAR_CFG_MIN_LEN = 0x20,    // minimum data length for valid decode
    ZBAR_CFG_MAX_LEN,           // maximum data length for valid decode
    ZBAR_CFG_POSITION = 0x80,   // enable scanner to collect position data
    ZBAR_CFG_X_DENSITY = 0x100, // image scanner vertical scan density
    ZBAR_CFG_Y_DENSITY          // image scanner horizontal scan density
}

#[link(name = "zbar")]
extern "C" {

    pub fn zbar_set_verbosity(verbosity: c_int);

    /******* Image Interface *******/
    /** constructor
     * @returns a new image object with uninitialized data and format.  This image should be
     * destroyed using zbar_image_destroy as soon as the application is finished with it.
     */
    pub fn zbar_image_create() -> *mut zbar_image_t;

    /** destructor
     *  all images create by or returned to the application should be destroyed using this function.
     *  when an image is destroyed, the associated data cleanup handler will be involed if
     *  available.
     */
    pub fn zbar_image_destroy(image: *mut zbar_image_t);

    /** image reference count manipulation
     *  increment the reference count when you store a new reference to the image.  decrement when
     *  the reference is no longer used.  do not refer to the image any longer once the count is
     *  decremented.
     */
    pub fn zbar_image_ref(image: *mut zbar_image_t, refs: c_int);

    pub fn zbar_image_get_data(image: *const zbar_image_t) -> *mut u8;
    pub fn zbar_image_get_format(image: *const zbar_image_t) -> u32;

    pub fn zbar_image_get_height(image: *const zbar_image_t) -> u32;
    pub fn zbar_image_get_width(image: *const zbar_image_t) -> u32;

    pub fn zbar_image_set_size(image: *const zbar_image_t, width: u32, height: u32);
    pub fn zbar_image_set_format(image: *const zbar_image_t, format: u32);

    pub fn zbar_image_set_data(image: *const zbar_image_t,
                               data: *const u8,
                               byte_length: u32,
                               cleanup_handler: unsafe extern fn(*mut zbar_image_t));

    pub fn zbar_image_free_data(image: *mut zbar_image_t);

    pub fn zbar_image_first_symbol(image: *const zbar_image_t) -> *const zbar_symbol_t;

    /**
     * Symbol Interface
     */
    pub fn zbar_symbol_next(symbol: *const zbar_symbol_t) -> *const zbar_symbol_t;

    pub fn zbar_symbol_get_type(symbol: *const zbar_symbol_t) -> zbar_symbol_type_e;
    pub fn zbar_symbol_get_data(symbol: *const zbar_symbol_t) -> *const c_char;
    pub fn zbar_get_symbol_name(sym: zbar_symbol_type_e) -> *const c_char;

    /******* Image Scanner Interface *******/
	/** constructor **/
	pub fn zbar_image_scanner_create() -> *mut zbar_image_scanner_t;
	/** destructor **/
	pub fn zbar_image_scanner_destroy(scanner: *mut zbar_image_scanner_t);

    /** setup result handler callback
     * the specified function will be called by the scanner whenever new results are available from
     * a decoded image.  pass a NULL value to disable callbacks.
     * @returns the previously-registered handler
     */
	pub fn zbar_image_scanner_set_data_handler(scanner: *mut zbar_image_scanner_t,
											   handler: *mut zbar_image_data_handler_t,
											   userdata: c_void) -> zbar_image_data_handler_t;

    /** parse a configuration string of the form "[symbology.]config[=value]".
     *  the config must match one of the recognized names.
     *  the symbology, if present, must match one of the recognized names.
     *  if symbology is unspecified, it will be set to 0.
     *  if value is unspecified, it will be set to 1.
     *  @returns 0 if the config is parsed successfully, 1 otherwise
     */
    pub fn zbar_parse_config(config_string: *const c_char,
                             symbology: *const zbar_symbol_type_e,
                             config: *const zbar_config_e,
                             value: c_int) -> c_int;

    /** set config for indicated symbology (0 for all) to specified value
     * @returns 0 for success, non-0 for failure (config does not apply to specified
     * symbology, or value out of range
     */
	pub fn zbar_image_scanner_set_config(scanner: *mut zbar_image_scanner_t,
										 symbology: zbar_symbol_type_e,
										 config: zbar_config_e,
										 value: c_int) -> c_int;

    /** parse configuration string using zbar_parse_config() and apply to image scanner using
     *  zbar image_scanner_set_config()
     *  @returns 0 for suhttps://www.google.com/search?tbm=isch&source=hp&biw=1468&bih=666&ei=EqlJWtD-MKm4jwSM65WADA&q=qr+code&oq=qr+code&gs_l=img.3..0l10.760.1792.0.1926.8.5.0.3.3.0.118.376.3j1.4.0....0...1ac.1.64.img..1.7.411.0...0.6p2UJmijeU0#imgrc=xc-lmcIMuckakM:ccess, non-0 for failure
     */
	pub fn zbar_image_scanner_parse_config(scanner: *mut zbar_image_scanner_t,
										   config: *const c_char) -> c_int;

	/** retrieve decode results for the last scanned image.
	 *  @returns the symbol set result container or NULL if no results are available
	 *  @note the symbol set does not have its reference count adjusted; ensure that the count is
	 *  incremented if the results may be kept after the next image is scanned.
	 */
	pub fn zbar_image_scanner_get_results(scanner: *mut zbar_image_scanner_t) -> zbar_symbol_set_t;

	/*
	* scan for symbols in provided image.  The image format must be Y800 or GRAY.
	*  @returns >0 if symbols were successfully decoded from the image,
	*  0 if no symbols were found or -1 if an error occurs
	*/
	pub fn zbar_scan_image(scanner: *const zbar_image_scanner_t, image: *mut zbar_image_t) -> c_int;
}

pub fn image_set_data(image: *const zbar_image_t,
                      data: &[u8],
                      length: u32,
                      cleanup_handler: unsafe extern fn(*mut zbar_image_t)) -> bool {
    unsafe {
        zbar_image_set_data(image, data.as_ptr(), length, cleanup_handler);
    }

    return true;
}

pub fn image_free_data(image: *mut zbar_image_t) {
    unsafe {
        zbar_image_free_data(image);
    }
}

pub fn symbol_get_data(symbol: *const zbar_symbol_t) -> String {
    unsafe {
        let data = zbar_symbol_get_data(symbol);
        let res = CStr::from_ptr(data).to_string_lossy().into_owned();
        return res;
    }
}

pub fn get_symbol_name(symbol: zbar_symbol_type_e) -> String {
    unsafe {
        return CStr::from_ptr(zbar_get_symbol_name(symbol)).to_string_lossy().into_owned();
    }
}

#[test]
fn scan_image() {
    use std::fs::File;
    use std::mem;
    use std::ptr::null;

    eprintln!("Loading image.");
    let decoder = png::Decoder::new(File::open("QR_code.png").unwrap());
    let (info, mut reader) = decoder.read_info().unwrap();
    let mut buf:Vec<u8> = vec![0; info.buffer_size()];
    reader.next_frame(&mut buf).unwrap();
    eprintln!("Image loaded.");

    unsafe {
        zbar_set_verbosity(100);

        let scanner = zbar_image_scanner_create();

        assert!(!scanner.is_null(), "Scanner should be non-null!");

        zbar_image_scanner_set_config(scanner, zbar_symbol_type_e::ZBAR_NONE, zbar_config_e::ZBAR_CFG_ENABLE, 1);

        let width = info.width;
        let height = info.height;

        assert_eq!(height, 1200, "Image height is 1200.");
        assert_eq!(width, 1200, "Image width is 1200.");

        let img = zbar_image_create();

        let format_arr:&[u8] = b"Y800";
        let format = mem::transmute::<[u8; 4], u32>([format_arr[0], format_arr[1], format_arr[2], format_arr[3]]);

        zbar_image_set_format(img, format);
        zbar_image_set_size(img, width, height);
        image_set_data(img, &buf, width * height, zbar_image_free_data);

        eprintln!("Image data set.");

        let n = zbar_scan_image(scanner, img);

        assert_eq!(n, 1, "Image successfully scanned with 1 result.");

        let mut symbol = zbar_image_first_symbol(img);
        while !symbol.is_null() {
            let typ = zbar_symbol_get_type(symbol);
            let data = symbol_get_data(symbol);
            eprintln!("Symbol name: {}", get_symbol_name(typ));
            eprintln!("Symbol data: {}", data);
            symbol = zbar_symbol_next(symbol);
        }
    }
}