Easily Generate Datamatrix in Ruby on Rails with Rdmtx
One of the hardest challenge in my last project was to insert a Datamatrix inside a view or a pdf, especially since it should have been a rectangular one.
I’ve looked at few gems to automate the process of data matrix generation, first of all “semacode”, then I tried to move to other type of gem/libraries.
In any case, I was looking for something easier and usable without too many difficulty.
During my long-term research I came across the gem “Rdmtx” almost by accident, and it solved all my problems.
Rdmtx is a gem that allows you to encode information within Datamatrix of different sizes and decode Datamatrix to get information
Installation
You would need to install rmagick gem
Then:
#Gemfile
gem ‘Rdmtx’#console
bundle install
The basic use of the gem is as follows:
require 'rmagick'
require 'Rdmtx'rdmtx.encode(String, MarginSize, ModuleSize, SizeRequest)
- String (required): The string to encode
- MarginSize (optional): the margin in pixels around the datamatrix
- ModuleSize (optional): the size of one module in pixels
- SizeRequest (optional): has to be one of pre-defined constants
You can find pre-defined constants in:
#ext/rdmtx/Rdmtx.c...
rb_define_global_const(“DmtxSymbol32x32”, INT2FIX(DmtxSymbol32x32)); rb_define_global_const(“DmtxSymbol36x36”, INT2FIX(DmtxSymbol36x36)); rb_define_global_const(“DmtxSymbol40x40”, INT2FIX(DmtxSymbol40x40)); rb_define_global_const(“DmtxSymbol44x44”, INT2FIX(DmtxSymbol44x44));
...
In my case I needed to create a rectangular Datamatrix sized 16x48
rdmtx = Rdmtx.new
i = rdmtx.encode(content, 0, 16, DmtxSymbol16x48)
i.write("output.png")
You can find the gem repository here