Skip to content

Commit

Permalink
- Update the URL for requesting the trial license key
Browse files Browse the repository at this point in the history
- Add a DBR v.10 example
  • Loading branch information
yushulx committed Oct 18, 2024
1 parent 6f6c6db commit 9c3975c
Show file tree
Hide file tree
Showing 21 changed files with 244 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist
/examples/official/9.x/zxing_zbar/__pycache__/*.pyc
/examples/official/9.x/jupyter_notebook/.ipynb_checkpoints
/examples/official/9.x/qt_gui/__pycache__/*.pyc
.DS_Store
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
# Python Extension: Barcode and QR Code SDK
This project provides a CPython binding to the [Dynamsoft C/C++ Barcode Reader SDK v9.x](https://www.dynamsoft.com/barcode-reader/sdk-desktop-server/). It demonstrates how to build a **Python 1D/2D barcode SDK** package for `Windows`, `Linux` and `macOS` from scratch. Beyond desktop PCs, it's also compatible with embedded and IoT devices such as `Raspberry Pi` and `Jetson Nano`. You are **free** to customize the Python API for Dynamsoft Barcode Reader to suit your specific needs.

> Note: This project is an unofficial, community-maintained Python wrapper for the Dynamsoft Barcode SDK. For those seeking the most reliable and fully-supported solution, Dynamsoft offers an official Python package. Visit the [Dynamsoft Barcode Reader](https://pypi.org/project/dbr/) page on PyPI for more details.
> Note: This project is an unofficial, community-maintained Python wrapper for the Dynamsoft Barcode SDK. For those seeking the most reliable and fully-supported solution, Dynamsoft offers an official Python package. Visit the [Dynamsoft Capture Vision Bundle](https://pypi.org/project/dynamsoft-capture-vision-bundle/) page on PyPI for more details.
## About Dynamsoft Python Barcode SDK
## About Dynamsoft Capture Vision Bundle
- Get a [30-day FREE trial license](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform) to activate the SDK.
- Install the official Python barcode SDK via `pip install dbr`.
- Install the SDK via `pip install dynamsoft-capture-vision-bundle`.

### Comparison Table
| Feature | Unofficial Wrapper (Community) | Official Dynamsoft Python Barcode SDK |
| --- | --- | --- |
| Support | Community-driven, best effort | Official support from Dynamsoft |
| Documentation | README only | [Comprehensive Online Documentation](https://www.dynamsoft.com/barcode-reader/programming/python/index.html) |
| Documentation | README only | [Comprehensive Online Documentation](https://www.dynamsoft.com/capture-vision/docs/server/programming/python/?lang=python) |
| API Coverage | Limited | Full API coverage |
|Feature Updates| May lag behind the official SDK | First to receive new features |
| Compatibility | Limited testing across environments| Thoroughly tested across all supported environments|
| OS Support | Windows, Linux, macOS | Windows, Linux, macOS |

## Supported Python Edition
* Python 3.x
Expand Down
2 changes: 1 addition & 1 deletion examples/ctypes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ This project explores an alternative approach to invoking C APIs from shared lib
cmake --build .
```

2. Get a valid license key from [Dynamsoft](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr). Then, update the license key in the `success.py` file.
2. Get a valid license key from [Dynamsoft](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform). Then, update the license key in the `success.py` file.

```python
license_key = b"LICENSE-KEY"
Expand Down
2 changes: 1 addition & 1 deletion examples/ctypes/failure.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class TextResultArray(Structure):
license_key = b"DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="
error_msg_buffer = create_string_buffer(256)
error_msg_buffer_len = len(error_msg_buffer)
# https://www.dynamsoft.com/customer/license/trialLicense?product=dbr
# https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform
ret = DBR_InitLicense(license_key, error_msg_buffer, error_msg_buffer_len)
print('initLicense: {}'.format(ret))

Expand Down
2 changes: 1 addition & 1 deletion examples/ctypes/success.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ResultList(Structure):
license_key = b"DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="
error_msg_buffer = create_string_buffer(256)
error_msg_buffer_len = len(error_msg_buffer)
# https://www.dynamsoft.com/customer/license/trialLicense?product=dbr
# https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform
ret = DBR_InitLicense(license_key, error_msg_buffer, error_msg_buffer_len)

# DBR_CreateInstance
Expand Down
74 changes: 74 additions & 0 deletions examples/official/10.x/file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import sys
from dynamsoft_capture_vision_bundle import *
import os
import cv2
import numpy as np
from utils import *

if __name__ == '__main__':

print("**********************************************************")
print("Welcome to Dynamsoft Capture Vision - Barcode Sample")
print("**********************************************************")

error_code, error_message = LicenseManager.init_license(
"DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
if error_code != EnumErrorCode.EC_OK and error_code != EnumErrorCode.EC_LICENSE_CACHE_USED:
print("License initialization failed: ErrorCode:",
error_code, ", ErrorString:", error_message)
else:
cvr_instance = CaptureVisionRouter()
while (True):
image_path = input(
">> Input your image full path:\n"
">> 'Enter' for sample image or 'Q'/'q' to quit\n"
).strip('\'"')

if image_path.lower() == "q":
sys.exit(0)

if image_path == "":
image_path = "../../../images/multi.png"

if not os.path.exists(image_path):
print("The image path does not exist.")
continue
result = cvr_instance.capture(
image_path, EnumPresetTemplate.PT_READ_BARCODES.value)
if result.get_error_code() != EnumErrorCode.EC_OK:
print("Error:", result.get_error_code(),
result.get_error_string())
else:
cv_image = cv2.imread(image_path)

items = result.get_items()
print('Found {} barcodes.'.format(len(items)))
for item in items:
format_type = item.get_format()
text = item.get_text()
print("Barcode Format:", format_type)
print("Barcode Text:", text)

location = item.get_location()
x1 = location.points[0].x
y1 = location.points[0].y
x2 = location.points[1].x
y2 = location.points[1].y
x3 = location.points[2].x
y3 = location.points[2].y
x4 = location.points[3].x
y4 = location.points[3].y
del location

cv2.drawContours(
cv_image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)

cv2.putText(cv_image, text, (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)

cv2.imshow(
"Original Image with Detected Barcodes", cv_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

input("Press Enter to quit...")
2 changes: 2 additions & 0 deletions examples/official/10.x/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dynamsoft-capture-vision-bundle
opencv-python
138 changes: 138 additions & 0 deletions examples/official/10.x/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
from dynamsoft_capture_vision_bundle import *
import numpy as np


def convertImageData2Mat(normalized_image):
ba = bytearray(normalized_image.get_bytes())
width = normalized_image.get_width()
height = normalized_image.get_height()

channels = 3
if normalized_image.get_image_pixel_format() == EnumImagePixelFormat.IPF_BINARY:
channels = 1
all = []
skip = normalized_image.stride * 8 - width

index = 0
n = 1
for byte in ba:

byteCount = 7
while byteCount >= 0:
b = (byte & (1 << byteCount)) >> byteCount

if index < normalized_image.stride * 8 * n - skip:
if b == 1:
all.append(255)
else:
all.append(0)

byteCount -= 1
index += 1

if index == normalized_image.stride * 8 * n:
n += 1

mat = np.array(all, dtype=np.uint8).reshape(height, width, channels)
return mat

elif normalized_image.get_image_pixel_format() == EnumImagePixelFormat.IPF_GRAYSCALED:
channels = 1

mat = np.array(ba, dtype=np.uint8).reshape(height, width, channels)

return mat


def convertMat2ImageData(mat):
if len(mat.shape) == 3:
height, width, channels = mat.shape
pixel_format = EnumImagePixelFormat.IPF_RGB_888
else:
height, width = mat.shape
channels = 1
pixel_format = EnumImagePixelFormat.IPF_GRAYSCALED

stride = width * channels
imagedata = ImageData(mat.tobytes(), width, height, stride, pixel_format)
return imagedata


class MRZResult:
def __init__(self, item: ParsedResultItem):
self.doc_type = item.get_code_type()
self.raw_text = []
self.doc_id = None
self.surname = None
self.given_name = None
self.nationality = None
self.issuer = None
self.gender = None
self.date_of_birth = None
self.date_of_expiry = None
if self.doc_type == "MRTD_TD3_PASSPORT":
if item.get_field_value("passportNumber") != None and item.get_field_validation_status("passportNumber") != EnumValidationStatus.VS_FAILED:
self.doc_id = item.get_field_value("passportNumber")
elif item.get_field_value("documentNumber") != None and item.get_field_validation_status("documentNumber") != EnumValidationStatus.VS_FAILED:
self.doc_id = item.get_field_value("documentNumber")

line = item.get_field_value("line1")
if line is not None:
if item.get_field_validation_status("line1") == EnumValidationStatus.VS_FAILED:
line += ", Validation Failed"
self.raw_text.append(line)
line = item.get_field_value("line2")
if line is not None:
if item.get_field_validation_status("line2") == EnumValidationStatus.VS_FAILED:
line += ", Validation Failed"
self.raw_text.append(line)
line = item.get_field_value("line3")
if line is not None:
if item.get_field_validation_status("line3") == EnumValidationStatus.VS_FAILED:
line += ", Validation Failed"
self.raw_text.append(line)

if item.get_field_value("nationality") != None and item.get_field_validation_status("nationality") != EnumValidationStatus.VS_FAILED:
self.nationality = item.get_field_value("nationality")
if item.get_field_value("issuingState") != None and item.get_field_validation_status("issuingState") != EnumValidationStatus.VS_FAILED:
self.issuer = item.get_field_value("issuingState")
if item.get_field_value("dateOfBirth") != None and item.get_field_validation_status("dateOfBirth") != EnumValidationStatus.VS_FAILED:
self.date_of_birth = item.get_field_value("dateOfBirth")
if item.get_field_value("dateOfExpiry") != None and item.get_field_validation_status("dateOfExpiry") != EnumValidationStatus.VS_FAILED:
self.date_of_expiry = item.get_field_value("dateOfExpiry")
if item.get_field_value("sex") != None and item.get_field_validation_status("sex") != EnumValidationStatus.VS_FAILED:
self.gender = item.get_field_value("sex")
if item.get_field_value("primaryIdentifier") != None and item.get_field_validation_status("primaryIdentifier") != EnumValidationStatus.VS_FAILED:
self.surname = item.get_field_value("primaryIdentifier")
if item.get_field_value("secondaryIdentifier") != None and item.get_field_validation_status("secondaryIdentifier") != EnumValidationStatus.VS_FAILED:
self.given_name = item.get_field_value("secondaryIdentifier")

def to_string(self):
msg = (f"Raw Text:\n")
for index, line in enumerate(self.raw_text):
msg += (f"\tLine {index + 1}: {line}\n")
msg += (f"Parsed Information:\n"
f"\tDocumentType: {self.doc_type or ''}\n"
f"\tDocumentID: {self.doc_id or ''}\n"
f"\tSurname: {self.surname or ''}\n"
f"\tGivenName: {self.given_name or ''}\n"
f"\tNationality: {self.nationality or ''}\n"
f"\tIssuingCountryorOrganization: {self.issuer or ''}\n"
f"\tGender: {self.gender or ''}\n"
f"\tDateofBirth(YYMMDD): {self.date_of_birth or ''}\n"
f"\tExpirationDate(YYMMDD): {self.date_of_expiry or ''}\n")
return msg


def print_results(result: ParsedResult) -> None:
tag = result.get_original_image_tag()
if isinstance(tag, FileImageTag):
print("File:", tag.get_file_path())
if result.get_error_code() != EnumErrorCode.EC_OK:
print("Error:", result.get_error_string())
else:
items = result.get_items()
print("Parsed", len(items), "MRZ Zones.")
for item in items:
mrz_result = MRZResult(item)
print(mrz_result.to_string())
2 changes: 1 addition & 1 deletion examples/official/9.x/django/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This sample demonstrates how to create an online Barcode and QR Code Reader usin

- **SDK License**

To use the Dynamsoft Barcode Reader SDK, request a [30-day free trial license](https://www.dynamsoft.com/customer/license/trialLicense/?product=dbr).
To use the Dynamsoft Barcode Reader SDK, request a [30-day free trial license](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform).

## Usage
1. Set the license key in `scanbarcode/views.py`:
Expand Down
14 changes: 9 additions & 5 deletions examples/official/9.x/django/scanbarcode/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
from dbr import *
import json

# Apply for a trial license: https://www.dynamsoft.com/customer/license/trialLicense?product=dbr
BarcodeReader.init_license("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
# Apply for a trial license: https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform
BarcodeReader.init_license(
"DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
reader = BarcodeReader()


def index(request):
return render(request, 'scanbarcode/index.html')
return render(request, 'scanbarcode/index.html')


def upload(request):
out = "No barcode found"
if request.method == 'POST':
filePath = handle_uploaded_file(request.FILES['RemoteFile'], str(request.FILES['RemoteFile']))
filePath = handle_uploaded_file(
request.FILES['RemoteFile'], str(request.FILES['RemoteFile']))
try:
text_results = reader.decode_file(filePath)
if text_results != None:
Expand All @@ -31,7 +35,6 @@ def upload(request):
print(bre)
return HttpResponse(out)


return HttpResponse(out)
# image = Image()
# image.name = request.FILES['RemoteFile'].name
Expand All @@ -41,6 +44,7 @@ def upload(request):

return HttpResponse(out)


def handle_uploaded_file(file, filename):
if not os.path.exists('upload/'):
os.mkdir('upload/')
Expand Down
2 changes: 1 addition & 1 deletion examples/official/9.x/flet_chat_gemini/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pip install -r requirements.txt
![Gemini API key](https://www.dynamsoft.com/codepool/img/2024/09/gemini-api-key.png)


2. Request a [free trial license](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr) for Dynamsoft Barcode Reader and replace the value of `LICENSE-KEY` in `chatbot.py`.
2. Request a [free trial license](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform) for Dynamsoft Barcode Reader and replace the value of `LICENSE-KEY` in `chatbot.py`.

```python
license_key = "LICENSE-KEY"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"id": "49130381"
},
"source": [
"[![](https://img.shields.io/badge/Get-30--day%20FREE%20Trial%20License-blue)](https://www.dynamsoft.com/customer/license/trialLicense/?product=dbr)"
"[![](https://img.shields.io/badge/Get-30--day%20FREE%20Trial%20License-blue)](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/official/9.x/nonstandard_1D_barcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This sample demonstrates how to use the [Dynamsoft Barcode Reader SDK](https://w
Python 3.x

## Prerequisites
- Obtain a [Dynamsoft Barcode Reader trial license](https://www.dynamsoft.com/customer/license/trialLicense/?product=dbr)
- Obtain a [Dynamsoft Barcode Reader trial license](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform)
- Install the Dynamsoft Barcode Reader SDK for Python:

```bash
Expand Down
2 changes: 1 addition & 1 deletion examples/official/9.x/qrcode_template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pip install -r requirements.txt
```

## Usage
1. Obtain a [30-day FREE trial license](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr) for the Dynamsoft Barcode Reader SDK and save the license key in a `license.txt` file.
1. Obtain a [30-day FREE trial license](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform) for the Dynamsoft Barcode Reader SDK and save the license key in a `license.txt` file.

2. Run the sample scripts:

Expand Down
2 changes: 1 addition & 1 deletion examples/official/9.x/qt_gui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This is a cross-platform GUI barcode reader application built with `Python 3`, `
python3 -m pip install dbr
```

- [Dynamsoft Barcode SDK License](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr)
- [Dynamsoft Barcode SDK License](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform)


## Usage
Expand Down
2 changes: 2 additions & 0 deletions examples/official/9.x/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dbr
opencv-python
2 changes: 1 addition & 1 deletion examples/official/9.x/webcam/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
This repository provides samples demonstrating how to create a simple barcode and QR code reader using a webcam in Python. The OpenCV Stitcher API is utilized to stitch multiple barcode and QR code results together.

## License Activation
To activate the [Dynamsoft Barcode Reader SDK](https://www.dynamsoft.com/barcode-reader/sdk-desktop-server/), obtain a desktop license key from [here](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr):
To activate the [Dynamsoft Barcode Reader SDK](https://www.dynamsoft.com/barcode-reader/sdk-desktop-server/), obtain a desktop license key from [here](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform):

```python
BarcodeReader.init_license("LICENSE-KEY")
Expand Down
2 changes: 1 addition & 1 deletion examples/official/9.x/yolo_qr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This repository provides samples demonstrating how to detect QR codes using **YO
```
pip install dbr
```
- Obtain a [Dynamsoft Barcode Reader trial license](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr) and update your code with the provided license key:
- Obtain a [Dynamsoft Barcode Reader trial license](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform) and update your code with the provided license key:
```python
from dbr import *
Expand Down
Loading

0 comments on commit 9c3975c

Please sign in to comment.