{"id":2593,"date":"2020-03-17T10:23:28","date_gmt":"2020-03-17T10:23:28","guid":{"rendered":"http:\/\/www.bardecode.com\/newsite\/?p=2593"},"modified":"2020-03-17T10:24:14","modified_gmt":"2020-03-17T10:24:14","slug":"update-python-interface","status":"publish","type":"post","link":"https:\/\/www.bardecode.com\/newsite\/update-python-interface\/","title":{"rendered":"Update to Python interface"},"content":{"rendered":"<p>We&#8217;ve made some changes to the recommended way of interfacing Python on Windows to take account of Python 3 and Windows 64-bit.<\/p>\n<p>The first change is in the loading of the DLL file itself..<\/p>\n<p>if platform.machine().endswith(&#8217;64&#8217;):<br \/>\nbardecodeLibrary = os.getcwd() + &#8216;..\/..\/..\/SoftekBarcode64DLL.dll&#8217;<br \/>\na=CDLL(bardecodeLibrary)<br \/>\nelse:<br \/>\nbardecodeLibrary = os.getcwd() + &#8216;..\/..\/..\/SoftekBarcodeDLL.dll&#8217;<br \/>\na=WinDLL(bardecodeLibrary)<\/p>\n<p>Note the use of CDLL on 64-bit but WinDLL on 32-bit.<\/p>\n<p>The second important change saves on some repetition later on&#8230;<\/p>\n<p># Create an instance of the bardecode toolkit<br \/>\na.mtCreateBarcodeInstance.restype = ctypes.c_void_p<br \/>\nhBarcode = <span style=\"color: #ff0000;\">c_void_p<\/span>(a.mtCreateBarcodeInstance())<\/p>\n<p>&#8230;so we ensure that hBarcode is a c_void_p (as you would have probably assumed given the line above). This means you can just use it as normal without having to cast it each time.<\/p>\n<p>&nbsp;<\/p>\n<p>So the entire sample Python script now reads as&#8230;<\/p>\n<p>import os, platform<br \/>\nimport ctypes<br \/>\nfrom ctypes import *<\/p>\n<p># Load the correct DLL file, note the use of CDLL on 64-bit and WinDLL on 32-bit<br \/>\nif platform.machine().endswith(&#8217;64&#8217;):<br \/>\nbardecodeLibrary = os.getcwd() + &#8216;..\/..\/..\/SoftekBarcode64DLL.dll&#8217;<br \/>\na=CDLL(bardecodeLibrary)<br \/>\nelse:<br \/>\nbardecodeLibrary = os.getcwd() + &#8216;..\/..\/..\/SoftekBarcodeDLL.dll&#8217;<br \/>\na=WinDLL(bardecodeLibrary)<\/p>\n<p># Set the name of the image to decode<br \/>\ninputFile = &#8216;file.tif&#8217;<\/p>\n<p># Create an instance of the bardecode toolkit<br \/>\na.mtCreateBarcodeInstance.restype = ctypes.c_void_p<br \/>\nhBarcode = c_void_p(a.mtCreateBarcodeInstance())<\/p>\n<p># Set a license key<br \/>\na.mtSetLicenseKey(hBarcode, &#8216;YOUR LICENSE KEY&#8217;.encode(&#8220;utf-8&#8221;))<\/p>\n<p># Set the ReadQRCode property<br \/>\na.mtSetReadQRCode(hBarcode, True)<\/p>\n<p># Scan the input file for barcodes<br \/>\nnBarcodes = a.mtScanBarCode(hBarcode, inputFile.encode(&#8220;utf-8&#8221;))<\/p>\n<p># Collect the output<br \/>\nfor x in range(1, nBarcodes + 1):<br \/>\na.mtGetBarString.restype = ctypes.c_char_p<br \/>\nbarcodeValue = a.mtGetBarString(hBarcode, x)<br \/>\nprint (barcodeValue.decode(&#8220;utf-8&#8221;))<\/p>\n<p>a.mtDestroyBarcodeInstance(hBarcode)<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve made some changes to the recommended way of interfacing Python on Windows to take account of Python 3 and Windows 64-bit. The first change is in the loading of the DLL file itself.. if platform.machine().endswith(&#8217;64&#8217;): bardecodeLibrary = os.getcwd() + &#8216;..\/..\/..\/SoftekBarcode64DLL.dll&#8217; a=CDLL(bardecodeLibrary) else: bardecodeLibrary = os.getcwd() + &#8216;..\/..\/..\/SoftekBarcodeDLL.dll&#8217; a=WinDLL(bardecodeLibrary) Note the use of CDLL on &hellip; <\/p>\n","protected":false},"author":1,"featured_media":314,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,203,31],"tags":[],"class_list":["post-2593","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-knowledge-base","category-news","category-software-development-kits"],"_links":{"self":[{"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/posts\/2593"}],"collection":[{"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/comments?post=2593"}],"version-history":[{"count":3,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/posts\/2593\/revisions"}],"predecessor-version":[{"id":2596,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/posts\/2593\/revisions\/2596"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/media\/314"}],"wp:attachment":[{"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/media?parent=2593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/categories?post=2593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/tags?post=2593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}