dlenski / python-zxing

python wrapper for the ZXing barcode library
GNU Lesser General Public License v3.0
121 stars 36 forks source link

I don't know what the problem is #5

Closed zzll0909 closed 4 years ago

zzll0909 commented 4 years ago

zxing: error: Unknown Java exception: b'Exception in thread "main" java.lang.IllegalArgumentException: URI path component is empty\r\n\tat sun.nio.fs.WindowsUriSupport.fromUri(Unknown Source)\r\n\tat sun.nio.fs.WindowsFileSystemProvider.getPath(Unknown Source)\r\n\tat java.nio.file.Paths.get(Unknown Source)\r\n\tat com.google.zxing.client.j2se.CommandLineRunner.expand(CommandLineRunner.java:112)\r\n\tat com.google.zxing.client.j2se.CommandLineRunner.main(CommandLineRunner.java:76)\r\n'

zzll0909 commented 4 years ago

image

dlenski commented 4 years ago

Hmmm… I don't know either. The zxing Java binary is notably terrible at parsing the filenames sent to it by Python (it's possible that the Chinese characters in the path are confusing it). The fact that you're running on Windows doesn't help…

Can you try adding these two lines and re-running? It should print two extra lines with the filename and “file URI” before failing.

diff --git a/zxing/__init__.py b/zxing/__init__.py
index bd0dd31..c767f0a 100644
--- a/zxing/__init__.py
+++ b/zxing/__init__.py
@@ -58,7 +58,9 @@ class BarCodeReader(object):
         elif stdout.startswith(b'''Exception in thread "main" java.io.IOException: Could not load '''):
             raise BarCodeReaderException("Java library could not read image; is it in a supported format?", filename)
         elif stdout.startswith(b'''Exception '''):
+            print(filename)
+            print(file_uri)
             raise BarCodeReaderException("Unknown Java exception: %s" % stdout)
         elif p.returncode:
             raise BarCodeReaderException("Unexpected subprocess return code %d" % p.returncode, self.java)
vladvrabie commented 4 years ago

I had the same problem (I'm also on Windows). I got the package through pip and I saw that there are some differences in __init__.py compared to the one on github.

My downloaded __init__.py had in the decode method: file_uri = 'file://' + quote(os.path.abspath(filename))

While on github it looks like this (link): file_uri = pathlib.Path(filename).absolute().as_uri()

The version on github worked for me.

file_uri variable value in 1st case: file://D%3A%5Czxing_test%5Cimage.jpg file_uri variable value in 2nd case: file:///D:/zxing_test/image.jpg

Maybe something needs to be updated on pypi?

dlenski commented 4 years ago

The version on github worked for me.

I appreciate the feedback about the working change, but your way of demonstrating the difference is a little excruciating. :sweat:

A simple git log v0.10..HEAD will show you that we changed this in 06ce16c8e088e9fc3e74303bec92eeb8fa05098c.

Maybe something needs to be updated on pypi?

Indeed. The aforementioned changes haven't yet been rolled into a release.

Update: Done. Latest version tagged as v0.11 and pushed to PyPI.