1+ import os
2+ import sys
3+ import time
4+ import io
5+ import numpy as np
6+ from picamera import PiCamera
7+ from picamera .array import PiRGBArray
8+ from dbr import *
9+ import cv2
10+
11+ # you can change the following variables' value to your own value.
12+ license_key = "Input your own license"
13+ json_file = r"Please input your own template path"
14+
15+ reader = BarcodeReader ()
16+
17+ def intermediate_results_callback_func (frame_id , i_results , user_data ):
18+ print (frame_id )
19+ for result in i_results :
20+ intermediate_result = IntermediateResult (result )
21+ print ('Intermediate Result data type : {0}' .format (intermediate_result .result_type ))
22+ print ('Intermediate Result data : {0}' .format (intermediate_result .results ))
23+ print ("-------------" )
24+ ## Or you can inherit the abstract class IntermediateResultCallBack to implement the abstract method intermediate_results_callback_func.
25+ # class SubIntermediateResultCallBack(IntermediateResultCallBack):
26+ # @staticmethod
27+ # def intermediate_results_callback_func(frame_id, i_results, user_data):
28+ # print(frame_id)
29+ # for result in i_results:
30+ # intermediate_result = IntermediateResult(result)
31+ # print('Intermediate Result data type : {0}'.format(intermediate_result.result_type))
32+ # print('Intermediate Result data : {0}'.format(intermediate_result.results))
33+ # print("-------------")
34+
35+
36+ def text_results_callback_func (frame_id , t_results , user_data ):
37+ print (frame_id )
38+ for result in t_results :
39+ text_result = TextResult (result )
40+ print ("Barcode Format : " )
41+ print (text_result .barcode_format_string )
42+ print ("Barcode Text : " )
43+ print (text_result .barcode_text )
44+ print ("Localization Points : " )
45+ print (text_result .localization_result .localization_points )
46+ print ("Exception : " )
47+ print (text_result .exception )
48+ print ("-------------" )
49+ ## Or you can inherit the abstract class TextResultResultCallBack to implement the abstract method text_results_callback_func.
50+ # class SubTextResultResultCallBack(TextResultResultCallBack):
51+ # @staticmethod
52+ # def text_results_callback_func(frame_id, t_results, user_data):
53+ # print(frame_id)
54+ # for result in t_results:
55+ # text_result = TextResult(result)
56+ # print("Barcode Format : ")
57+ # print(text_result.barcode_format_string)
58+ # print("Barcode Text : ")
59+ # print(text_result.barcode_text)
60+ # print("Localization Points : ")
61+ # print(text_result.localization_result.localization_points)
62+ # print("Exception : ")
63+ # print(text_result.exception)
64+ # print("-------------")
65+
66+
67+ def error_callback_func (frame_id , error_code , user_data ):
68+ print (frame_id )
69+ error_msg = user_data .get_error_string (error_code )
70+ print ('Error : {0} ; {1}' .format (error_code , error_msg ))
71+ ## Or you can inherit the abstract class ErrorCallBack to implement the abstract method error_callback_func.
72+ # class SubErrorCallBack(ErrorCallBack):
73+ # @staticmethod
74+ # def error_callback_func(frame_id, error_code, user_data):
75+ # print(frame_id)
76+ # error_msg = user_data.get_error_string(error_code)
77+ # print('Error : {0} ; {1}'.format(error_code, error_msg))
78+
79+
80+ def get_time ():
81+ localtime = time .localtime ()
82+ capturetime = time .strftime ("%Y%m%d%H%M%S" , localtime )
83+ return capturetime
84+
85+ def read_barcode ():
86+ video_width = 0
87+ video_height = 0
88+
89+ # vc = cv2.VideoCapture(0)
90+ camera = PiCamera ()
91+ camera .resolution = (1024 , 1024 ) # Set image's width and height
92+
93+ # SetPiCameraParameters(camera):
94+ # camera.saturation = 80 # Video or Image saturation
95+ # camera.brightness = 50 # Video or Image brightness
96+ # camera.shutter_speed = 6000000 # The shutter speed
97+ # camera.iso = 800 # ISO
98+ # camera.sharpness = 0 # Image Sharpness
99+ # camera.hflip = True # Whether to flip horizontally
100+ # camera.vflip = True # Whether to flip vertically
101+ # camera.rotation = 0 # Whether to rotate image
102+ # camera.resolution = (280, 160) # Set image's width and height
103+
104+
105+ video_width = camera .resolution [0 ]
106+ video_height = camera .resolution [1 ]
107+ print (camera .resolution )
108+
109+ stride = 0
110+ stream = io .BytesIO ()
111+ camera .capture (stream , format = 'jpeg' )
112+ # 从流构建numpy
113+ data = np .frombuffer (stream .getvalue (), dtype = np .uint8 )
114+ # 通过opencv解码numpy
115+ image = cv2 .imdecode (data , 1 )
116+ stride = image .strides [0 ]
117+ print (stride )
118+ rawCapture = PiRGBArray (camera , size = (video_width , video_height ))
119+
120+ windowName = "Barcode Reader"
121+
122+ parameters = reader .init_frame_decoding_parameters ()
123+ # you can modify these following parameters.
124+ parameters .max_queue_length = 30
125+ parameters .max_result_queue_length = 30
126+ parameters .width = video_width
127+ parameters .height = video_height
128+ parameters .stride = stride
129+ parameters .image_pixel_format = EnumImagePixelFormat .IPF_RGB_888
130+ parameters .region_top = 0
131+ parameters .region_bottom = 100
132+ parameters .region_left = 0
133+ parameters .region_right = 100
134+ parameters .region_measured_by_percentage = 1
135+ parameters .threshold = 0.01
136+ parameters .fps = 0
137+ parameters .auto_filter = 1
138+
139+ reader .start_video_mode (parameters , text_results_callback_func )
140+ # reader.start_video_mode(parameters, SubTextResultResultCallBack.text_results_callback_func)
141+ ## You can use three callbacks at the same time.
142+ # reader.start_video_mode(parameters, text_results_callback_func, "", intermediate_results_callback_func, error_callback_func, reader)
143+ # reader.start_video_mode(parameters, SubTextResultResultCallBack.text_results_callback_func, "", SubIntermediateResultCallBack.intermediate_results_callback_func, SubErrorCallBack.error_callback_func, reader)
144+
145+ for frame in camera .capture_continuous (rawCapture , format = 'bgr' , use_video_port = True ):
146+ image = frame .array
147+
148+ try :
149+ ret = reader .append_video_frame (image )
150+ except :
151+ pass
152+
153+ # 'ESC' for quit
154+ key = cv2 .waitKey (1 )
155+ rawCapture .truncate (0 )
156+ if key == 27 :
157+ break
158+
159+ reader .stop_video_mode ()
160+ cv2 .destroyWindow (windowName )
161+
162+
163+ print ("-------------------start------------------------" )
164+
165+ # Apply for a trial license: https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=github
166+ reader .init_license (license_key )
167+
168+ ## The code snippet below shows how to use the full license in DBR 8.x:
169+ # connection_paras = BarcodeReader.init_lts_connection_parameters()
170+ ## If DBR service is already built on your server, you can fill in the address of your server, or leave this property's default value.
171+ # connection_paras.main_server_url = "Input your own server url"
172+ # connection_paras.handshake_code = "Input your own handshake"
173+ # connection_paras.deployment_type = EnumDMDeploymentType.DM_DT_DESKTOP
174+ # connection_paras.uuid_generation_method = EnumDMUUIDGenerationMethod.DM_UUIDGM_RANDOM
175+ # try:
176+ # error = BarcodeReader.init_license_from_lts(connection_paras)
177+ # if error[0] != EnumErrorCode.DBR_OK:
178+ # print(error[1])
179+ # except BarcodeReaderError as bre:
180+ # print(bre)
181+
182+ error = reader .init_runtime_settings_with_file (json_file )
183+ if error [0 ] != EnumErrorCode .DBR_OK :
184+ print (error [1 ])
185+
186+ read_barcode ()
187+ print ("-------------------over------------------------" )
0 commit comments