grocer 0.1.2

A simple Rust library for reading UPC barcodes from images.
Documentation
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405

//! grocer
//! A simple library for reading barcodes from an image
//! 


use std::env;
use std::error::Error;
use std::path::Path;
use image::GenericImageView;



#[derive(Debug)]
struct WhiteZone {
	end: u32,
	size: u32,
}

#[derive(Clone, Debug)]
struct Bar {
	color: u32,
	modules: u32,
	width: u32,
}


pub struct ScannerSettings {
	pub high_speed: bool,
}

pub struct Barcode {
	pub found: bool,
	pub code: String,
	pub orientation: u32, // not yet used
	pub reversed: u32, // not yet used
}


fn pixel_bw(pixel: image::Rgba<u8>) -> u32 {
	let thresh = 110; // Default: 110
	let grayscale = {
		let mut sum: u32 = 0;
		for i in 0..3 {
			sum += pixel[i] as u32;
		}
		sum / 3
	};
	if grayscale < thresh {
		0
	} else {
		1
	}
}


fn get_white_zones_list(file: &str, y: u32) -> Vec<WhiteZone> {
	let mut zones: Vec<WhiteZone> = Vec::new();

	let img = image::open(file).unwrap();
	let width = img.dimensions().0;
	
	let mut zone_size: u32 = 0;
	let mut last_bw: u32 = 1;
	for x in 0..width {
		let pix = img.get_pixel(x, y);
		let bw = pixel_bw(pix);
		
		// End of zone
		if bw == 0 && last_bw == 1{
			let new_zone = WhiteZone {
				end: x,
				size: zone_size,
			};
			zones.push(new_zone);
			zone_size = 0;
		}

		if bw == 1 {
			zone_size += 1;
		}
		
		last_bw = bw;
	}
	// Add the last zone if it was white
	if last_bw == 1 {
		let new_zone = WhiteZone {
			end: width,
			size: zone_size,
		};
		zones.push(new_zone);
	}

	zones.sort_by_key(|el| el.size);
	zones.reverse();
	zones
}


fn collect_bars(file: &str, y: u32, quiet_zone: &WhiteZone) -> Result<Vec<Bar>, Box<dyn Error>> {
	let mut bars: Vec<Bar> = Vec::new();
	let img = image::open(file).unwrap();
	let width = img.dimensions().0;

	let mut last_bw: u32 = 0;
	let mut zone_size: u32 = 0;

	// Get each bar's width and color
	for x in quiet_zone.end..width {
		let pix = img.get_pixel(x, y);
		let bw = pixel_bw(pix);
		
		if last_bw != bw {
			let bar = Bar {
				color: last_bw,
				width: zone_size,
				modules: 0,
			};
			bars.push(bar);
			zone_size = 0;
		}
		zone_size += 1;
		last_bw = bw;
	}
	
	// Add the last zone if it was white
	if last_bw == 1 {
		let bar = Bar {
			color: last_bw,
			width: zone_size,
			modules: 0,
		};
		bars.push(bar);
	}
	
	
	if bars.len() < 2 || bars[1].color != 1 {
		// Error. We should have a start zone 
		// with a white bar as the second bar
		return Err("Invalid start region.".into());	
	}

	// First white bar size
	let white_bar_size = bars[1].width;

	// Find the ending quiet zone index
	let quiet_zone_bar_size: u32 = 8;
	let quiet_zone_min_width = white_bar_size * quiet_zone_bar_size;
	
	let mut quiet_zone_end = 0;
	for i in 0..bars.len() {
		if bars[i].width >= quiet_zone_min_width {
			quiet_zone_end = i;
			break;
		}
	}
	
	if quiet_zone_end == 0 {
		return Err("Error. No acceptable start or end zone.".into());
	}


	Ok((&bars[..quiet_zone_end-1]).to_vec())
}


fn barlist_to_upc(barlist: Vec<Bar>) -> String {
	let mut upc_code = String::from("");
	let upc_lookup = [
	(3, 2, 1, 1),
	(2, 2, 2, 1),
	(2, 1, 2, 2),
	(1, 4, 1, 1),
	(1, 1, 3, 2),
	(1, 2, 3, 1),
	(1, 1, 1, 4),
	(1, 3, 1, 2),
	(1, 2, 1, 3),
	(3, 1, 1, 2),
	];		

	// Needs to skip Start, End, and Middle
	let new_barlist = barlist.clone();
	
	let barlist_start = &new_barlist[..27];
	let barlist_end   = &new_barlist[32..];

	let full_barlist = [barlist_start, barlist_end].concat().to_vec();

	// Skip the start & end patterns
	for i in (3..full_barlist.len()-3).step_by(4){
		let digit = (full_barlist[i+0].modules,
			full_barlist[i+1].modules,
			full_barlist[i+2].modules,
			full_barlist[i+3].modules);
		for k in 0..upc_lookup.len() {
			if upc_lookup[k] == digit {
				let _ = &upc_code.push_str(&k.to_string());
				break;
			}
		}
	}
	upc_code
}

fn is_code_upc(code: &str) -> bool{
	let mut pos: u32 = 3;
	let mut sum: u32 = 0;
	for c in code.chars() {
		let val: u32 = match c.to_string().trim().parse() {
			Ok(num) => num,
			Err(_) => return false,
		};
		sum += pos * val;
		pos = (pos + 2) % 4; // alternate 1 & 3 
	}
	if sum % 10 == 0 {
		return true ;
	}
	return false ;
}


fn scan_upc_line(file: &str, height_percent: f64) -> String {
	let upc_modules = 95;
	
	let mut code: String = String::from("");
	let img = image::open(file).unwrap();
	let y: f64 = img.dimensions().1.into();
	let mid: u32 = (y * height_percent) as u32;

	// Get the list of white zones
	let zones: Vec<WhiteZone> = get_white_zones_list(file, mid);

	for start_zone in zones {

		// Try to get a code for each zone
		let mut barlist = match collect_bars(file, mid, &start_zone) {
			Ok(list) => list,
			Err(_) => continue,
		};

		// Make sure there are enough bars
		if barlist.len() < 58 {
			continue;
		}

		// Get width of barcode in pixels
		let mut barcode_width: u32 = 0;
		for bar in &barlist {
			barcode_width += bar.width;
		}

		let module_size: f64 = (barcode_width as f64) / (upc_modules as f64);
		//println!("Module size: {}", module_size);
		
		// Get the module size of each bar
		for i in 0..barlist.len() {
			let mut module_count: u32 = 0;
			let mut sample_x: f64 = (module_size as f64) / 2.0;
			while sample_x < (barlist[i].width as f64) {
				module_count += 1;
				sample_x += module_size;
			}
			
			barlist[i].modules = module_count;
		}

		code = barlist_to_upc(barlist);
		//println!("CC: {}", code);	
	
		let check = is_code_upc(&code);	
		if check {
			//println!("The UPC code is valid");
		} else {
			//println!("The UPC code is invalid");
			code = String::from("");
		}
	
		break;
	}
	code
}




/// Scans an image file horizontally for any UPC-A codes and returns the code as a string.
///
/// # Parameters
/// 
/// file: A string of the image filename to read from.
/// settings: A ScannerSettings struct to select scanning options.
///
/// # Return
///
/// Returns a Barcode struct representing any barcode scanned. Returns an empty string in the code field if no barcode was found
///
/// # Examples
/// 
/// ```
///    	let scan_settings = grocer::ScannerSettings { high_speed: false, };
///	let barcode: grocer::Barcode = grocer::scan_upc("images/image.png", scan_settings);
///	println!("Code: {}", barcode.code);
/// ```
pub fn scan_upc(file: &str, settings: ScannerSettings) -> Barcode {
	let mut barcode;
	let mut code: String = String::from("");
	
	// Make a list of codes that are found
	let mut codes: Vec<String> = Vec::new();
	let mut code_counts: Vec<u32> = Vec::new();

	if !Path::new(file).exists() {
		return Barcode {
			found: false,
			code: code,
			orientation: 0,
			reversed: 0,
		};
	}


	for percent in (0..100).step_by(5) {
		code = scan_upc_line(file, (percent as f64) / 100.0);
		
		// Reject codes that are not the right size
		if code.len() != 12 {
			continue;
		}

		if code != "" {
			if settings.high_speed {
				break;
			}
			else {
				// Increment the code count or add it to the list
				let mut found: bool = false;
				for (i, c) in codes.iter().enumerate() {
					if &code == c {
						code_counts[i] += 1;
						found = true;
						break;
					}
				}
				if !found {
					codes.push(code.clone());
					code_counts.push(1);
				}
			}
		}
	}

	if settings.high_speed {
		barcode = Barcode {
			found: { 
				if code == "" {
					false
				} else {
					true
				}
			},
			code: code,
			orientation: 0,
			reversed: 0,		
		};
	}
	else {
		
		// Use the most frequently found code in the image
		let mut max_found: u32 = 0;
		let mut max_index: usize = 0;
		for (i, &cnt) in code_counts.iter().enumerate() {
			if cnt > max_found {
				max_found = cnt;
				max_index = i;
			}
		}

		//println!("Code list: {:?}", codes);	
		//println!("Code count: {:?}", code_counts);	
	
		code = codes[max_index].clone();
		
		barcode = Barcode {
			found: { 
				if code == "" {
					false
				} else {
					true
				}
			},
			code: code,
			orientation: 0,
			reversed: 0,		
		};
	}
	barcode
}