1

I have written a simple Python code to detect qrCode. Code:

import zxing
reader = zxing.BarCodeReader() 
barcode = reader.decode('../images/QR_CODE-easy.png')

print(barcode)

Now, when I run it, I get the following error:

FileNotFoundError: [WinError 2] The system cannot find the file specified

I have check this file location is valid by using cv.imread command. Please let me know if someone has a solution to this problem.

Striezel
  • 3,693
  • 7
  • 23
  • 37
Hasan Iqbal
  • 173
  • 16
  • 1
    I got the same problem. But here's some error info u didnt mention: ``` Traceback (most recent call last): File "", line 1, in File "d:\Anaconda3\envs\test_py3\lib\site-packages\zxing\__init__.py", line 38, in decode p = sp.Popen(cmd, stdout=sp.PIPE, universal_newlines=False) File "d:\Anaconda3\envs\test_py3\lib\subprocess.py", line 775, in __init__ restore_signals, start_new_session) File "d:\Anaconda3\envs\test_py3\lib\subprocess.py", line 1178, in _execute_child startupinfo) FileNotFoundError: [WinError 2] ``` Since its not that barCode picture cant be found, I think it may – 陈泽霖 Feb 28 '19 at 02:34
  • 1
    I'm back! it is about java file. I didnt install JDK, after installation, its done, hope this helps you. – 陈泽霖 Feb 28 '19 at 03:38

2 Answers2

2

You need to install Java Development Kit.

The readme of ZXing Python wrapper says the following:

Dependencies and installation

Use the Python 3 version of pip (usually invoked via pip3) to install: pip3 install zxing

  • You'll neeed to have a recent java binary somewhere in your path. (Tested with OpenJDK.)
  • pip will automatically download the relevant JAR files for the Java ZXing libraries (currently v3.4.1)
Striezel
  • 3,693
  • 7
  • 23
  • 37
Steve Nyke
  • 19
  • 2
  • It may be the right solution, but please elaborate why one needs to install the JDK to fix a Python error. This may not be obvious. – Striezel Mar 14 '21 at 17:13
  • @Striezel, I'm just look documentation for ZXing library: Dependencies and installation Use the Python 3 version of pip (usually invoked via pip3) to install: pip3 install zxing You'll neeed to have a recent java binary somewhere in your path. (Tested with OpenJDK.) pip will automatically download the relevant JAR files for the Java ZXing libraries (currently v3.4.1) And i think this JAVA library, python ZXlibrari just api for Java library) – Steve Nyke Mar 15 '21 at 18:16
  • Alright. I've added that information to your answer. – Striezel Mar 16 '21 at 16:15
0

You appear to be on Windows (as the error code suggests), which uses the backslash for file paths.

It's not good practice as it won't be widely compatible, but if you're in a hurry and know you won't want to use the code on Mac or Linux, you can use double backslashes:

reader.decode('..\\images\\QR_CODE-easy.png')

Otherwise you should use os.path.join or pathlib (assuming your using Python 3)

import os.path qr_file = os.path.join("..", "images", "QR_CODE-easy.png")

Or

from pathlib import Path qr_file = Path("../images/QR_CODE-easy.png")

There are further details of a few options here:

https://medium.com/@ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f

Edit: it is also worth confirming that your relative path is indeed correct when starting in your current working directory. You can check the current working directory with: cwd = os.getcwd() . You may want to try an absolute path to your file too, just to confirm whether it works with that first.

More details on cwd here: https://stackoverflow.com/a/5137509/142780

Neil
  • 686
  • 1
  • 9
  • 27
  • I did all the remedies you mentioned, but the same problem persists. Its really frustrating. I think zxing don't work on python3 – Hasan Iqbal Nov 16 '18 at 08:31
  • Sorry to hear that. According to the README in the repo, it is compatible with Python 3: https://github.com/dlenski/python-zxing/blob/master/README.md – Neil Nov 17 '18 at 10:43
  • I've added a point about confirming your current working directory and suggest trying an absolute path just to be 100% sure it's not a mix up with the relative path – Neil Nov 17 '18 at 10:44