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
/*
* Copyright 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//package com.google.zxing.common.detector;
use crate::{
common::{BitMatrix, Result},
point_f, Exceptions, Point,
};
/**
* <p>
* Detects a candidate barcode-like rectangular region within an image. It
* starts around the center of the image, increases the size of the candidate
* region until it finds a white rectangular region. By keeping track of the
* last black points it encountered, it determines the corners of the barcode.
* </p>
*
* @author David Olivier
*/
const INIT_SIZE: i32 = 10;
const CORR: i32 = 1;
pub struct WhiteRectangleDetector<'a> {
image: &'a BitMatrix,
height: i32,
width: i32,
leftInit: i32,
rightInit: i32,
downInit: i32,
upInit: i32,
}
impl<'a> WhiteRectangleDetector<'_> {
pub fn new_from_image(image: &'a BitMatrix) -> Result<WhiteRectangleDetector<'a>> {
WhiteRectangleDetector::new(
image,
INIT_SIZE,
image.getWidth() as i32 / 2,
image.getHeight() as i32 / 2,
)
}
/**
* @param image barcode image to find a rectangle in
* @param initSize initial size of search area around center
* @param x x position of search center
* @param y y position of search center
* @throws NotFoundException if image is too small to accommodate {@code initSize}
*/
pub fn new(
image: &'a BitMatrix,
initSize: i32,
x: i32,
y: i32,
) -> Result<WhiteRectangleDetector<'a>> {
let halfsize = initSize / 2;
let leftInit = x - halfsize;
let rightInit = x + halfsize;
let upInit = y - halfsize;
let downInit = y + halfsize;
if upInit < 0
|| leftInit < 0
|| downInit >= image.getHeight() as i32
|| rightInit >= image.getWidth() as i32
{
return Err(Exceptions::NOT_FOUND);
}
Ok(WhiteRectangleDetector {
image,
height: image.getHeight() as i32,
width: image.getWidth() as i32,
leftInit,
rightInit,
downInit,
upInit,
})
}
/**
* <p>
* Detects a candidate barcode-like rectangular region within an image. It
* starts around the center of the image, increases the size of the candidate
* region until it finds a white rectangular region.
* </p>
*
* @return {@link Point}[] describing the corners of the rectangular
* region. The first and last points are opposed on the diagonal, as
* are the second and third. The first point will be the topmost
* point and the last, the bottommost. The second point will be
* leftmost and the third, the rightmost
* @throws NotFoundException if no Data Matrix Code can be found
*/
pub fn detect(&self) -> Result<[Point; 4]> {
let mut left: i32 = self.leftInit;
let mut right: i32 = self.rightInit;
let mut up: i32 = self.upInit;
let mut down: i32 = self.downInit;
let mut size_exceeded = false;
let mut a_black_point_found_on_border = true;
let mut at_least_one_black_point_found_on_right = false;
let mut at_least_one_black_point_found_on_bottom = false;
let mut at_least_one_black_point_found_on_left = false;
let mut at_least_one_black_point_found_on_top = false;
while a_black_point_found_on_border {
a_black_point_found_on_border = false;
// .....
// . |
// .....
let mut right_border_not_white = true;
while (right_border_not_white || !at_least_one_black_point_found_on_right)
&& right < self.width
{
right_border_not_white = self.contains_black_point(up, down, right, false);
if right_border_not_white {
right += 1;
a_black_point_found_on_border = true;
at_least_one_black_point_found_on_right = true;
} else if !at_least_one_black_point_found_on_right {
right += 1;
}
}
if right >= self.width {
size_exceeded = true;
break;
}
// .....
// . .
// .___.
let mut bottom_border_not_white = true;
while (bottom_border_not_white || !at_least_one_black_point_found_on_bottom)
&& down < self.height
{
bottom_border_not_white = self.contains_black_point(left, right, down, true);
if bottom_border_not_white {
down += 1;
a_black_point_found_on_border = true;
at_least_one_black_point_found_on_bottom = true;
} else if !at_least_one_black_point_found_on_bottom {
down += 1;
}
}
if down >= self.height {
size_exceeded = true;
break;
}
// .....
// | .
// .....
let mut left_border_not_white = true;
while (left_border_not_white || !at_least_one_black_point_found_on_left) && left >= 0 {
left_border_not_white = self.contains_black_point(up, down, left, false);
if left_border_not_white {
left -= 1;
a_black_point_found_on_border = true;
at_least_one_black_point_found_on_left = true;
} else if !at_least_one_black_point_found_on_left {
left -= 1;
}
}
if left < 0 {
size_exceeded = true;
break;
}
// .___.
// . .
// .....
let mut top_border_not_white = true;
while (top_border_not_white || !at_least_one_black_point_found_on_top) && up >= 0 {
top_border_not_white = self.contains_black_point(left, right, up, true);
if top_border_not_white {
up -= 1;
a_black_point_found_on_border = true;
at_least_one_black_point_found_on_top = true;
} else if !at_least_one_black_point_found_on_top {
up -= 1;
}
}
if up < 0 {
size_exceeded = true;
break;
}
}
if !size_exceeded {
let max_size = right - left;
let mut z: Option<Point> = None;
let mut i = 1;
while z.is_none() && i < max_size {
//for (int i = 1; z == null && i < maxSize; i++) {
z = self.get_black_point_on_segment(
left as f32,
(down - i) as f32,
(left + i) as f32,
down as f32,
);
i += 1;
}
if z.is_none() {
return Err(Exceptions::NOT_FOUND);
}
let mut t: Option<Point> = None;
//go down right
let mut i = 1;
while t.is_none() && i < max_size {
//for (int i = 1; t == null && i < maxSize; i++) {
t = self.get_black_point_on_segment(
left as f32,
(up + i) as f32,
(left + i) as f32,
up as f32,
);
i += 1;
}
if t.is_none() {
return Err(Exceptions::NOT_FOUND);
}
let mut x: Option<Point> = None;
//go down left
let mut i = 1;
while x.is_none() && i < max_size {
//for (int i = 1; x == null && i < maxSize; i++) {
x = self.get_black_point_on_segment(
right as f32,
(up + i) as f32,
(right - i) as f32,
up as f32,
);
i += 1;
}
if x.is_none() {
return Err(Exceptions::NOT_FOUND);
}
let mut y: Option<Point> = None;
//go up left
let mut i = 1;
while y.is_none() && i < max_size {
//for (int i = 1; y == null && i < maxSize; i++) {
y = self.get_black_point_on_segment(
right as f32,
(down - i) as f32,
(right - i) as f32,
down as f32,
);
i += 1;
}
if y.is_none() {
return Err(Exceptions::NOT_FOUND);
}
Ok(self.center_edges(y.unwrap(), z.unwrap(), x.unwrap(), t.unwrap()))
} else {
Err(Exceptions::NOT_FOUND)
}
}
fn get_black_point_on_segment(&self, a_x: f32, a_y: f32, b_x: f32, b_y: f32) -> Option<Point> {
let a = point_f(a_x, a_y);
let b = point_f(b_x, b_y);
let dist = a.distance(b).round() as i32;
let x_step: f32 = (b_x - a_x) / dist as f32;
let y_step: f32 = (b_y - a_y) / dist as f32;
for i in 0..dist {
let x = (a_x + i as f32 * x_step).round() as i32;
let y = (a_y + i as f32 * y_step).round() as i32;
if self.image.get(x as u32, y as u32) {
return Some(point_f(x as f32, y as f32));
}
}
None
}
/**
* recenters the points of a constant distance towards the center
*
* @param y bottom most point
* @param z left most point
* @param x right most point
* @param t top most point
* @return {@link Point}[] describing the corners of the rectangular
* region. The first and last points are opposed on the diagonal, as
* are the second and third. The first point will be the topmost
* point and the last, the bottommost. The second point will be
* leftmost and the third, the rightmost
*/
fn center_edges(&self, y: Point, z: Point, x: Point, t: Point) -> [Point; 4] {
//
// t t
// z x
// x OR z
// y y
//
let yi = y.x;
let yj = y.y;
let zi = z.x;
let zj = z.y;
let xi = x.x;
let xj = x.y;
let ti = t.x;
let tj = t.y;
if yi < self.width as f32 / 2.0 {
[
point_f(ti - CORR as f32, tj + CORR as f32),
point_f(zi + CORR as f32, zj + CORR as f32),
point_f(xi - CORR as f32, xj - CORR as f32),
point_f(yi + CORR as f32, yj - CORR as f32),
]
} else {
[
point_f(ti + CORR as f32, tj + CORR as f32),
point_f(zi + CORR as f32, zj - CORR as f32),
point_f(xi - CORR as f32, xj + CORR as f32),
point_f(yi - CORR as f32, yj - CORR as f32),
]
}
}
/**
* Determines whether a segment contains a black point
*
* @param a min value of the scanned coordinate
* @param b max value of the scanned coordinate
* @param fixed value of fixed coordinate
* @param horizontal set to true if scan must be horizontal, false if vertical
* @return true if a black point has been found, else false.
*/
fn contains_black_point(&self, a: i32, b: i32, fixed: i32, horizontal: bool) -> bool {
if horizontal {
for x in a..=b {
if self.image.get(x as u32, fixed as u32) {
return true;
}
}
} else {
for y in a..=b {
if self.image.get(fixed as u32, y as u32) {
return true;
}
}
}
false
}
}