{"id":1986,"date":"2010-10-22T12:52:48","date_gmt":"2010-10-22T07:22:48","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1986"},"modified":"2016-12-19T15:04:07","modified_gmt":"2016-12-19T09:34:07","slug":"datamatrix-for-unique-identification-of-documents-on-server-side-with-libdmtx","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/datamatrix-for-unique-identification-of-documents-on-server-side-with-libdmtx\/","title":{"rendered":"Datamatrix for unique identification of Documents on Server Side with libdmtx"},"content":{"rendered":"<p>I had earlier written a post on how to use QR Code to take care of identification of printed documents and updating the DB based on the <a href=\"http:\/\/www.tothenew.com\/blog\/qr-code-on-pdf-documents-for-identification-and-detection-on-server-side\/\" target=\"_blank\">QR Code detection<\/a>. However, we had to abandon the use of QR Codes in the document because the library wasn&#8217;t performing reliable, even with QR Codes from image files, which were direct screenshots of the PDF. Add to this, the fact that ZXing was also consuming a lot of memory. We believe that it was just not meant for use in a web application, even though it is a low traffic application which caters to a very specific niche.<\/p>\n<p>We decided to try out <a href=\"http:\/\/en.wikipedia.org\/wiki\/Data_matrix_(computer)\" target=\"_blank\">DataMatrix<\/a>. However,\u00a0 there was a dearth of Free java libraries to render\/detect them and the cost of the paid libraries put us off, as well as the lack of clarity on the respective product sites about whether they can be used from inside a web application.<\/p>\n<p>We found that we could use a free native library named libdmtx, which works on top of Imagemagick to take care of our requirements. There was a java wrapper around it, but we couldn&#8217;t get it working even though we tried the steps mentioned on <a href=\"http:\/\/libdmtx.wikidot.com\/libdmtx-java-wrapper\" target=\"_blank\">their page<\/a>. So after a lot of thought and the consideration that we were not going to migrate to a non Unix platform for any of the environments, we decided to use the library to create images and detect them using the libdmtx-utils.<\/p>\n<p>The service code reads something like this.<\/p>\n<p>[java]<\/p>\n<p>def createAndReturnBytes(String text) {<br \/>\nFile tmpFile = new File(System.getProperty(&#8216;java.io.tmpdir&#8217;) + File.separator + &#8216;tempStringHolder.txt&#8217;) \/\/Temporary file to hold the string to be encoded. Using &#8216;echo&#8217; in the didn&#8217;t work<br \/>\ntmpFile.write(text)<br \/>\ndef p = &quot;dmtxwrite ${tmpFile.absolutePath} -o ${filename}&quot;.execute() \/\/Execute the process<br \/>\np.waitFor() \/\/Wait for the process to complete before proceeding further<br \/>\nbyte[] contents = new File(filename).bytes \/\/Retrieve the bytes from the image file<br \/>\nnew File(filename).delete()<br \/>\nreturn contents \/\/Return the bytes for the image<\/p>\n<p>}<\/p>\n<p> String detectDataMatrix(File file){<br \/>\n def p = &quot;dmtxread -N1 ${file.absolutePath}&quot;.execute().text \/\/-N1 flag to break execution after the first datamatrix on the file has been detected<br \/>\n return p<br \/>\n }<\/p>\n<p>[\/java]<\/p>\n<p>This has been working very reliably for us. To enhance the images further, we may use ImageMagick to crop the area, where we think the datamatrix is and zoom on it before sending the file to the datamatrix service for detection.<\/p>\n<p>Hope this helps someone<\/p>\n<p>Vivek<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had earlier written a post on how to use QR Code to take care of identification of printed documents and updating the DB based on the QR Code detection. However, we had to abandon the use of QR Codes in the document because the library wasn&#8217;t performing reliable, even with QR Codes from image [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[7],"tags":[443,445,4840,444],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1986"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1986"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1986\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}