# 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 Capture Vision Bundle](https://pypi.org/project/dynamsoft-capture-vision-bundle/) page on PyPI for more details.
## 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 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/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
## Installation of Dependencies
To show UI, you need to install the OpenCV package:
```bash
pip install opencv-python
```
## Command-line Usage
```bash
$ scanbarcode <file-name> -l <license-key>
# Show the image with OpenCV
$ scanbarcode <file-name> -u 1 -l <license-key>
```

## How to Build the Python Barcode and QR Code Extension
- Create a source distribution:
```bash
python setup.py sdist
```
- setuptools:
```bash
python setup_setuptools.py build
python setup_setuptools.py develop # Copy libraries to barcodeQrSDK folder
```
- scikit-build:
```bash
python setup.py build
python setup.py develop # Copy libraries to barcodeQrSDK folder
```
- Build wheel:
```bash
pip wheel . --verbose
# Or
python setup_setuptools.py bdist_wheel
# Or
python setup.py bdist_wheel
```
## Quick Start
- Console App
```python
import barcodeQrSDK
# set license
barcodeQrSDK.initLicense("LICENSE-KEY")
reader = barcodeQrSDK.createInstance()
results, elapsed_time = reader.decodeFile("IMAGE-FILE")
for result in results:
print(result.format)
print(result.text)
print(result.x1)
print(result.y1)
print(result.x2)
print(result.y2)
print(result.x3)
print(result.y3)
print(result.x4)
print(result.y4)
```
- Video App
```python
import barcodeQrSDK
import numpy as np
import cv2
import json
g_results = None
def callback(results, elapsed_time):
global g_results
g_results = (results, elapsed_time)
def run():
# set license
barcodeQrSDK.initLicense("LICENSE-KEY")
# initialize barcode scanner
scanner = barcodeQrSDK.createInstance()
params = scanner.getParameters()
# Convert string to JSON object
json_obj = json.loads(params)
# json_obj['ImageParameter']['ExpectedBarcodesCount'] = 999
params = json.dumps(json_obj)
ret = scanner.setParameters(params)
scanner.addAsyncListener(callback)
cap = cv2.VideoCapture(0)
while True:
ret, image = cap.read()
if image is not None:
scanner.decodeMatAsync(image)
if g_results != None:
print('Elapsed time: ' + str(g_results[1]) + 'ms')
cv2.putText(image, 'Elapsed time: ' + str(g_results[1]) + 'ms', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
for result in g_results[0]:
x1 = result.x1
y1 = result.y1
x2 = result.x2
y2 = result.y2
x3 = result.x3
y3 = result.y3
x4 = result.x4
y4 = result.y4
cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
cv2.putText(image, result.text, (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow('Barcode QR Code Scanner', image)
ch = cv2.waitKey(1)
if ch == 27:
break
scanner.clearAsyncListener()
if __name__ == '__main__':
run()
```

## Methods
- `barcodeQrSDK.initLicense('YOUR-LICENSE-KEY')`: Set the global license key for the barcode SDK.
```python
barcodeQrSDK.initLicense("LICENSE-KEY")
```
- `barcodeQrSDK.createInstance()`: Create a new barcode reader instance.
```python
reader = barcodeQrSDK.createInstance()
```
- `decodeFile(filename)`: Decode barcodes and QR codes from an image file.
```python
results, elapsed_time = reader.decodeFile("IMAGE-FILE")
```
- `decodeMat(Mat image)`: Decode barcodes and QR codes from an OpenCV Mat.
```python
image = cv2.imread("IMAGE-FILE")
results = reader.decodeMat(image)
for result in results:
print(result.format)
print(result.text)
print(result.x1)
print(result.y1)
print(result.x2)
print(result.y2)
print(result.x3)
print(result.y3)
print(result.x4)
print(result.y4)
```
- `getParameters()`: Retrieve the current SDK parameters as a JSON string.
```python
params = reader.getParameters()
```
- `setParameters(JSON string)`: Set barcode SDK parameters using a JSON string.
```python
import json
json_obj = json.loads(params)
json_obj['ImageParameter']['DPMCodeReadingModes'][0]['Mode'] = 'DPMCRM_GENERAL'
json_obj['ImageParameter']['LocalizationModes'][0]['Mode'] = 'LM_STATISTICS_MARKS'
params = json.dumps(json_obj)
ret = reader.setParameters(params)
```
- `addAsyncListener(callback function)`: Register a Python function to receive barcode results asynchronously.
- `decodeMatAsync(<opencv mat data>)`: Asynchronously decode barcodes and QR codes from an OpenCV Mat.
```python
def callback(results, elapsed_time):
print(results)
import cv2
image = cv2.imread("IMAGE-FILE")
reader.addAsyncListener(callback)
reader.decodeMatAsync(image)
sleep(1)
```
- `clearAsyncListener()`: Stop the asynchronous listener and clear the registered callback.
- `decodeBytes(bytes, width, height, stride, imageformat)`: Decode barcodes from a raw image byte array.
```python
import cv2
image = cv2.imread("IMAGE-FILE")
results, elapsed_time = scanner.decodeBytes(image.tobytes(), image.shape[1], image.shape[0], image.strides[0], barcodeQrSDK.ImagePixelFormat.IPF_BGR_888)
```
- `decodeBytesAsync`: Asynchronously decode image byte arrays.
```python
def callback(results, elapsed_time):
print(results)
import cv2
image = cv2.imread("IMAGE-FILE")
imagebytes = image.tobytes()
scanner.decodeBytesAsync(image.tobytes(), image.shape[1], image.shape[0], image.strides[0], barcodeQrSDK.ImagePixelFormat.IPF_BGR_888)
sleep(1)
```
## Supported Barcode Symbologies
- Linear Barcodes (1D)
- Code 39 (including Code 39 Extended)
- Code 93
- Code 128
- Codabar
- Interleaved 2 of 5
- EAN-8
- EAN-13
- UPC-A
- UPC-E
- Industrial 2 of 5
- 2D Barcodes:
- QR Code (including Micro QR Code)
- Data Matrix
- PDF417 (including Micro PDF417)
- Aztec Code
- MaxiCode (mode 2-5)
- Patch Code
- GS1 Composite Code
Raw data
{
"_id": null,
"home_page": "https://github.com/yushulx/python-barcode-qrcode-sdk",
"name": "barcode-qr-code-sdk",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "yushulx",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/05/4f/ab4130a91b35ca80b6822a2b64780d3cbd1367e19d22763d6361de8d4313/barcode_qr_code_sdk-9.6.40.2.tar.gz",
"platform": null,
"description": "# Python Extension: Barcode and QR Code SDK \nThis 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.\n\n> 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.\n\n## About Dynamsoft Capture Vision Bundle\n- Get a [30-day FREE trial license](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform) to activate the SDK.\n- Install the SDK via `pip install dynamsoft-capture-vision-bundle`.\n\n### Comparison Table\n| Feature | Unofficial Wrapper (Community) | Official Dynamsoft Python Barcode SDK |\n| --- | --- | --- |\n| Support | Community-driven, best effort | Official support from Dynamsoft |\n| Documentation | README only | [Comprehensive Online Documentation](https://www.dynamsoft.com/capture-vision/docs/server/programming/python/?lang=python) |\n| API Coverage | Limited | Full API coverage |\n|Feature Updates| May lag behind the official SDK | First to receive new features |\n| Compatibility | Limited testing across environments| Thoroughly tested across all supported environments|\n| OS Support | Windows, Linux, macOS | Windows, Linux, macOS |\n\n## Supported Python Edition\n* Python 3.x\n\n## Installation of Dependencies\nTo show UI, you need to install the OpenCV package:\n```bash \npip install opencv-python\n```\n\n## Command-line Usage\n```bash \n$ scanbarcode <file-name> -l <license-key>\n\n# Show the image with OpenCV\n$ scanbarcode <file-name> -u 1 -l <license-key>\n```\n\n\n\n\n## How to Build the Python Barcode and QR Code Extension\n- Create a source distribution:\n \n ```bash\n python setup.py sdist\n ```\n\n- setuptools:\n \n ```bash\n python setup_setuptools.py build\n python setup_setuptools.py develop # Copy libraries to barcodeQrSDK folder\n ```\n\n- scikit-build:\n \n ```bash\n python setup.py build\n python setup.py develop # Copy libraries to barcodeQrSDK folder\n ```\n- Build wheel:\n \n ```bash\n pip wheel . --verbose\n # Or\n python setup_setuptools.py bdist_wheel\n # Or\n python setup.py bdist_wheel\n ```\n\n\n## Quick Start\n- Console App\n ```python\n import barcodeQrSDK\n\n # set license\n barcodeQrSDK.initLicense(\"LICENSE-KEY\")\n\n reader = barcodeQrSDK.createInstance()\n\n results, elapsed_time = reader.decodeFile(\"IMAGE-FILE\")\n for result in results:\n print(result.format)\n print(result.text)\n print(result.x1)\n print(result.y1)\n print(result.x2)\n print(result.y2)\n print(result.x3)\n print(result.y3)\n print(result.x4)\n print(result.y4)\n ```\n- Video App\n ```python\n import barcodeQrSDK\n import numpy as np\n import cv2\n import json\n\n g_results = None\n\n def callback(results, elapsed_time):\n global g_results\n g_results = (results, elapsed_time)\n\n def run():\n # set license\n barcodeQrSDK.initLicense(\"LICENSE-KEY\")\n\n # initialize barcode scanner\n scanner = barcodeQrSDK.createInstance()\n params = scanner.getParameters()\n # Convert string to JSON object\n json_obj = json.loads(params)\n # json_obj['ImageParameter']['ExpectedBarcodesCount'] = 999\n params = json.dumps(json_obj)\n ret = scanner.setParameters(params)\n \n scanner.addAsyncListener(callback)\n\n cap = cv2.VideoCapture(0)\n while True:\n ret, image = cap.read()\n if image is not None:\n scanner.decodeMatAsync(image)\n \n if g_results != None:\n print('Elapsed time: ' + str(g_results[1]) + 'ms')\n cv2.putText(image, 'Elapsed time: ' + str(g_results[1]) + 'ms', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)\n for result in g_results[0]:\n x1 = result.x1\n y1 = result.y1\n x2 = result.x2\n y2 = result.y2\n x3 = result.x3\n y3 = result.y3\n x4 = result.x4\n y4 = result.y4\n \n cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)\n cv2.putText(image, result.text, (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)\n\n cv2.imshow('Barcode QR Code Scanner', image)\n ch = cv2.waitKey(1)\n if ch == 27:\n break\n \n scanner.clearAsyncListener()\n\n if __name__ == '__main__':\n run()\n ```\n \n \n\n\n\n## Methods\n- `barcodeQrSDK.initLicense('YOUR-LICENSE-KEY')`: Set the global license key for the barcode SDK.\n \n ```python\n barcodeQrSDK.initLicense(\"LICENSE-KEY\")\n ```\n\n- `barcodeQrSDK.createInstance()`: Create a new barcode reader instance.\n \n ```python\n reader = barcodeQrSDK.createInstance()\n ```\n- `decodeFile(filename)`: Decode barcodes and QR codes from an image file.\n\n ```python\n results, elapsed_time = reader.decodeFile(\"IMAGE-FILE\")\n ```\n- `decodeMat(Mat image)`: Decode barcodes and QR codes from an OpenCV Mat.\n ```python\n image = cv2.imread(\"IMAGE-FILE\")\n results = reader.decodeMat(image)\n for result in results:\n print(result.format)\n print(result.text)\n print(result.x1)\n print(result.y1)\n print(result.x2)\n print(result.y2)\n print(result.x3)\n print(result.y3)\n print(result.x4)\n print(result.y4)\n ```\n\n- `getParameters()`: Retrieve the current SDK parameters as a JSON string.\n \n ```python\n params = reader.getParameters()\n ```\n\n- `setParameters(JSON string)`: Set barcode SDK parameters using a JSON string.\n \n ```python\n import json\n json_obj = json.loads(params)\n json_obj['ImageParameter']['DPMCodeReadingModes'][0]['Mode'] = 'DPMCRM_GENERAL'\n json_obj['ImageParameter']['LocalizationModes'][0]['Mode'] = 'LM_STATISTICS_MARKS'\n params = json.dumps(json_obj)\n ret = reader.setParameters(params)\n ```\n\n- `addAsyncListener(callback function)`: Register a Python function to receive barcode results asynchronously.\n- `decodeMatAsync(<opencv mat data>)`: Asynchronously decode barcodes and QR codes from an OpenCV Mat.\n ```python\n def callback(results, elapsed_time):\n print(results)\n \n import cv2\n image = cv2.imread(\"IMAGE-FILE\")\n reader.addAsyncListener(callback)\n reader.decodeMatAsync(image)\n sleep(1)\n ```\n- `clearAsyncListener()`: Stop the asynchronous listener and clear the registered callback.\n- `decodeBytes(bytes, width, height, stride, imageformat)`: Decode barcodes from a raw image byte array.\n\n ```python\n import cv2\n image = cv2.imread(\"IMAGE-FILE\")\n results, elapsed_time = scanner.decodeBytes(image.tobytes(), image.shape[1], image.shape[0], image.strides[0], barcodeQrSDK.ImagePixelFormat.IPF_BGR_888)\n ```\n- `decodeBytesAsync`: Asynchronously decode image byte arrays.\n\n ```python\n def callback(results, elapsed_time):\n print(results)\n \n import cv2\n image = cv2.imread(\"IMAGE-FILE\")\n imagebytes = image.tobytes()\n scanner.decodeBytesAsync(image.tobytes(), image.shape[1], image.shape[0], image.strides[0], barcodeQrSDK.ImagePixelFormat.IPF_BGR_888)\n sleep(1)\n ```\n\n## Supported Barcode Symbologies\n- Linear Barcodes (1D)\n\n - Code 39 (including Code 39 Extended)\n - Code 93\n - Code 128\n - Codabar\n - Interleaved 2 of 5\n - EAN-8\n - EAN-13\n - UPC-A\n - UPC-E\n - Industrial 2 of 5\n\n- 2D Barcodes:\n - QR Code (including Micro QR Code)\n - Data Matrix\n - PDF417 (including Micro PDF417)\n - Aztec Code\n - MaxiCode (mode 2-5)\n\n- Patch Code\n- GS1 Composite Code\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Barcode and QR code scanning SDK for Python",
"version": "9.6.40.2",
"project_urls": {
"Homepage": "https://github.com/yushulx/python-barcode-qrcode-sdk"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "16c882ca6aa5e2b5e51fc3ea4855943015da6b78303d38d139b973fdc3efaeae",
"md5": "9f13443df38ab0dac4a8d4f9aba2b8bd",
"sha256": "885125ffaf2563bfa11433083ceba81a5d7eacf593e483f112a2a9546f1f0f4e"
},
"downloads": -1,
"filename": "barcode_qr_code_sdk-9.6.40.2-cp39-cp39-manylinux_2_24_aarch64.whl",
"has_sig": false,
"md5_digest": "9f13443df38ab0dac4a8d4f9aba2b8bd",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": null,
"size": 10869018,
"upload_time": "2024-10-21T05:43:37",
"upload_time_iso_8601": "2024-10-21T05:43:37.422397Z",
"url": "https://files.pythonhosted.org/packages/16/c8/82ca6aa5e2b5e51fc3ea4855943015da6b78303d38d139b973fdc3efaeae/barcode_qr_code_sdk-9.6.40.2-cp39-cp39-manylinux_2_24_aarch64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a9a05c5a98fab1c5605dc737eb80b6f90d84c78173154b9d3bf702c7785a0c9c",
"md5": "2e9ae991c92091be43d3532d210e4f88",
"sha256": "14035ca27069accd6ae0aaae970b2d4c1ed0eb3531d538b361711fdefbfecb1f"
},
"downloads": -1,
"filename": "barcode_qr_code_sdk-9.6.40.2-cp39-cp39-manylinux_2_24_x86_64.whl",
"has_sig": false,
"md5_digest": "2e9ae991c92091be43d3532d210e4f88",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": null,
"size": 11676934,
"upload_time": "2024-10-21T05:43:40",
"upload_time_iso_8601": "2024-10-21T05:43:40.757661Z",
"url": "https://files.pythonhosted.org/packages/a9/a0/5c5a98fab1c5605dc737eb80b6f90d84c78173154b9d3bf702c7785a0c9c/barcode_qr_code_sdk-9.6.40.2-cp39-cp39-manylinux_2_24_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "448eaa1104dd0b3ef96e2c88070099da350a676042a3e138b7a9d5c1fc07a131",
"md5": "257651632d58fc1f648a98b5aaf47abe",
"sha256": "4eedc434a9d2d8b647b30b33dd08b2f5fe3f5551116242ac1e39843ccc38eb64"
},
"downloads": -1,
"filename": "barcode_qr_code_sdk-9.6.40.2-cp39-cp39-win_amd64.whl",
"has_sig": false,
"md5_digest": "257651632d58fc1f648a98b5aaf47abe",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": null,
"size": 10656488,
"upload_time": "2024-10-21T05:43:43",
"upload_time_iso_8601": "2024-10-21T05:43:43.136724Z",
"url": "https://files.pythonhosted.org/packages/44/8e/aa1104dd0b3ef96e2c88070099da350a676042a3e138b7a9d5c1fc07a131/barcode_qr_code_sdk-9.6.40.2-cp39-cp39-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "054fab4130a91b35ca80b6822a2b64780d3cbd1367e19d22763d6361de8d4313",
"md5": "f1c1cbe906db1af2b3eb4247e5a71287",
"sha256": "2694500ff9d3d01156ceca44b1c8d3a71e9cfa766ca3ed0733b62d449076de7d"
},
"downloads": -1,
"filename": "barcode_qr_code_sdk-9.6.40.2.tar.gz",
"has_sig": false,
"md5_digest": "f1c1cbe906db1af2b3eb4247e5a71287",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 60907807,
"upload_time": "2024-10-21T05:43:47",
"upload_time_iso_8601": "2024-10-21T05:43:47.919203Z",
"url": "https://files.pythonhosted.org/packages/05/4f/ab4130a91b35ca80b6822a2b64780d3cbd1367e19d22763d6361de8d4313/barcode_qr_code_sdk-9.6.40.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-21 05:43:47",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "yushulx",
"github_project": "python-barcode-qrcode-sdk",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "barcode-qr-code-sdk"
}