Skip to content

Commit

Permalink
update BarCodeRes
Browse files Browse the repository at this point in the history
  • Loading branch information
wqh17101 committed Aug 12, 2021
1 parent 848fcbf commit 2eb3968
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def has_libc_iconv():
zbar = Extension('zbar', sources=SRCS, include_dirs=INCLUDE, libraries=LIBS)

setup(name='zbar-lite',
version='0.23.91',
version='0.23.92',
description='zbar lite package only support scan image and recognize barcode',
long_description=open(os.path.join(CUR_DIR, "README.md"), encoding="utf-8").read(),
long_description_content_type="text/markdown",
Expand Down
53 changes: 47 additions & 6 deletions zbar_helper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
@Email : 597935261@qq.com
"""

import math
from collections import namedtuple
from typing import List

import zbar
Expand All @@ -15,22 +17,61 @@
except ModuleNotFoundError:
print("Warning,func show_info can not be used when cv2 is not available")

Position = namedtuple("Position", ["left_top", "left_bottom", "right_bottom", "right_top"])


class BarcodeRes:
"""
BarcodeRes
text : text of utf-8
type : barcode type
location : barcode point list
rect : bounding box of location
ori_orientation : zbar inner orientation (Only used for get points)
orientation : orientation degree
position : namedtuple with fields ["left_top", "left_bottom", "right_bottom", "right_top"]
"""

def __init__(self, text, barcode_type, location):
self.text = text
self.barcode_type = barcode_type
self.location = location
self.rect = get_bbox(location)
def __init__(self, x: zbar.Symbol):
self.text = x.data
self.type = str(x.type)
self.location = x.location
self.rect = get_bbox(x.location)
self.ori_orientation = str(x.orientation)
if self.ori_orientation == "LEFT": # for LEFT
self.position = Position._make([self.location[0], self.location[3], self.location[2], self.location[1]])
else:
self.position = Position._make(self.location)

self.orientation = get_clockwise_orientation(self.position.left_bottom, self.position.left_top, "degree")

def __repr__(self):
return str(self.__dict__)


def get_clockwise_orientation(start_p, end_p, return_format="degree"):
"""
calc clockwise orientation
:param start_p: start point
:param end_p: end point
:param return_format: degree or radian
:return:
"""
d_x = end_p[0] - end_p[0]
d_y = end_p[1] - start_p[1]
if d_y == 0:
if d_x >= 0:
res = math.pi / 2
else:
res = -math.pi / 2
else:
res = math.atan(d_x / d_y)
if return_format == "degree":
res = res / math.pi * 180
return round(res)


def get_bbox(p_list):
"""
Expand Down Expand Up @@ -59,7 +100,7 @@ def decode(img):
raw = img.tobytes()
image = zbar.Image(width, height, 'Y800', raw)
scanner.scan(image)
res = [BarcodeRes(x.data, str(x.type), x.location) for x in image]
res = [BarcodeRes(x) for x in image]
return res


Expand Down

0 comments on commit 2eb3968

Please sign in to comment.