mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	feature: Add support for zxing as barcode scanning lib
This commit is contained in:
		| @@ -17,7 +17,6 @@ from pikepdf import Page | ||||
| from pikepdf import Pdf | ||||
| from PIL import Image | ||||
| from PIL import ImageSequence | ||||
| from pyzbar import pyzbar | ||||
|  | ||||
| logger = logging.getLogger("paperless.barcodes") | ||||
|  | ||||
| @@ -83,18 +82,35 @@ def barcode_reader(image: Image) -> List[str]: | ||||
|     Returns a list containing all found barcodes | ||||
|     """ | ||||
|     barcodes = [] | ||||
|     # Decode the barcode image | ||||
|     detected_barcodes = pyzbar.decode(image) | ||||
|  | ||||
|     if detected_barcodes: | ||||
|         # Traverse through all the detected barcodes in image | ||||
|     if settings.CONSUMER_BARCODE_SCANNER == "PYZBAR": | ||||
|         logger.debug("Scanning for barcodes using PYZBAR") | ||||
|         from pyzbar import pyzbar | ||||
|  | ||||
|         # Decode the barcode image | ||||
|         detected_barcodes = pyzbar.decode(image) | ||||
|  | ||||
|         if detected_barcodes: | ||||
|             # Traverse through all the detected barcodes in image | ||||
|             for barcode in detected_barcodes: | ||||
|                 if barcode.data: | ||||
|                     decoded_barcode = barcode.data.decode("utf-8") | ||||
|                     barcodes.append(decoded_barcode) | ||||
|                     logger.debug( | ||||
|                         f"Barcode of type {str(barcode.type)} found: {decoded_barcode}", | ||||
|                     ) | ||||
|     elif settings.CONSUMER_BARCODE_SCANNER == "ZXING": | ||||
|         logger.debug("Scanning for barcodes using ZXING") | ||||
|         import zxingcpp | ||||
|  | ||||
|         detected_barcodes = zxingcpp.read_barcodes(image) | ||||
|         for barcode in detected_barcodes: | ||||
|             if barcode.data: | ||||
|                 decoded_barcode = barcode.data.decode("utf-8") | ||||
|                 barcodes.append(decoded_barcode) | ||||
|             if barcode.text: | ||||
|                 barcodes.append(barcode.text) | ||||
|                 logger.debug( | ||||
|                     f"Barcode of type {str(barcode.type)} found: {decoded_barcode}", | ||||
|                     f"Barcode of type {str(barcode.format)} found: {barcode.text}", | ||||
|                 ) | ||||
|  | ||||
|     return barcodes | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -3,6 +3,7 @@ import shutil | ||||
| from pathlib import Path | ||||
| from unittest import mock | ||||
|  | ||||
| import pytest | ||||
| from django.conf import settings | ||||
| from django.test import override_settings | ||||
| from django.test import TestCase | ||||
| @@ -14,6 +15,7 @@ from documents.tests.utils import FileSystemAssertsMixin | ||||
| from PIL import Image | ||||
|  | ||||
|  | ||||
| @override_settings(CONSUMER_BARCODE_SCANNER="PYZBAR") | ||||
| class TestBarcode(DirectoriesMixin, FileSystemAssertsMixin, TestCase): | ||||
|  | ||||
|     SAMPLE_DIR = Path(__file__).parent / "samples" | ||||
| @@ -1030,3 +1032,20 @@ class TestAsnBarcodes(DirectoriesMixin, TestCase): | ||||
|                 tasks.consume_file, | ||||
|                 dst, | ||||
|             ) | ||||
|  | ||||
|  | ||||
| try: | ||||
|     import zxingcpp | ||||
|  | ||||
|     ZXING_AVAILIBLE = True | ||||
| except ImportError: | ||||
|     ZXING_AVAILIBLE = False | ||||
|  | ||||
|  | ||||
| @pytest.mark.skipif( | ||||
|     not ZXING_AVAILIBLE, | ||||
|     reason="No zxingcpp", | ||||
| ) | ||||
| @override_settings(CONSUMER_BARCODE_SCANNER="ZXING") | ||||
| class TestBarcodeZxing(TestBarcode): | ||||
|     pass | ||||
|   | ||||
| @@ -166,4 +166,17 @@ def settings_values_check(app_configs, **kwargs): | ||||
|             ) | ||||
|         return msgs | ||||
|  | ||||
|     return _ocrmypdf_settings_check() + _timezone_validate() | ||||
|     def _barcode_scanner_validate(): | ||||
|         """ | ||||
|         Validates the barcode scanner type | ||||
|         """ | ||||
|         msgs = [] | ||||
|         if settings.CONSUMER_BARCODE_SCANNER not in ["PYZBAR", "ZXING"]: | ||||
|             msgs.append( | ||||
|                 Error(f'Invalid Barcode Scanner "{settings.CONSUMER_BARCODE_SCANNER}"'), | ||||
|             ) | ||||
|         return msgs | ||||
|  | ||||
|     return ( | ||||
|         _ocrmypdf_settings_check() + _timezone_validate() + _barcode_scanner_validate() | ||||
|     ) | ||||
|   | ||||
| @@ -718,6 +718,12 @@ CONSUMER_BARCODE_STRING: Final[str] = os.getenv( | ||||
|     "PATCHT", | ||||
| ) | ||||
|  | ||||
| consumer_barcode_scanner_tmp: Final[str] = os.getenv( | ||||
|     "PAPERLESS_CONSUMER_BARCODE_SCANNER", | ||||
|     "PYZBAR", | ||||
| ) | ||||
| CONSUMER_BARCODE_SCANNER = consumer_barcode_scanner_tmp.upper() | ||||
|  | ||||
| CONSUMER_ENABLE_ASN_BARCODE: Final[bool] = __get_boolean( | ||||
|     "PAPERLESS_CONSUMER_ENABLE_ASN_BARCODE", | ||||
| ) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Marvin Gaube
					Marvin Gaube