Enable Dark Mode!
generate-qr-code-odoo-13.png
By: Akshay Babu

How to Generate QR Code in Odoo 13

Technical Odoo 13

We were first introduced to Quick Response Code or the QR code in the mid-1990s in Japan. QR code is nothing but a two-dimensional barcode or a matrix barcode. The data can be embedded in the QR code using four standardized encoding modes (numeric, alphanumeric, byte/binary, and kanji).


The QR code quickly gained popularity among mass for its faster readability and greater storage capacity. It can be used for product tracking, item identification, time tracking, document managing, and general marketing.


Now let's see how to generate a QR code for a product in Odoo. Here we are generating the QR code based on the internal reference of the product. More information on the product can be added to the QR code.


PYTHON CODE:

import qrcode
import base64
from io import BytesIO
from odoo import models, fields, api
class ProductQR(models.Model):
    _inherit = "product.template"
    qr_code = fields.Binary("QR Code", attachment=True, store=True)
    @api.onchange('default_code')
    def generate_qr_code(self):
        qr = qrcode.QRCode(
            version=1,
            error_correction=qrcode.constants.ERROR_CORRECT_L,
            box_size=10,
            border=4,
        )
        qr.add_data(self.default_code)
        qr.make(fit=True)
        img = qr.make_image()
        temp = BytesIO()
        img.save(temp, format="PNG")
        qr_image = base64.b64encode(temp.getvalue())
        self.qr_code = qr_image

There is a python library that helps in the generation of QR code image called "QRcode".

Import QR code

import qrcode


Define a binary field to store the QR image.


class ProductQR(models.Model):
    _inherit = "product.template"
    qr_code = fields.Binary("QR Code", attachment=True) 


Here the function to generate a QR image is called when there is a change in the internal reference. Any method can be used to call the function to generate the QR code image. Since in this case QR image is generated based on the internal reference value, ‘onchange’ is used.

    @api.onchange('default_code')
    def generate_qr_code(self):
        qr = qrcode.QRCode(
            version=1,
            error_correction=qrcode.constants.ERROR_CORRECT_L,
            box_size=10,
            border=4,
        )
        qr.add_data(self.default_code)
        qr.make(fit=True)
        img = qr.make_image()
        temp = BytesIO()
        img.save(temp, format="PNG")
        qr_image = base64.b64encode(temp.getvalue())
        self.qr_code = qr_image


The QRCode class is used to get more control over the generated QR image.

 

     qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4,
    )


Version

Version ranges from 1 to 40. Depending on the size of the data to be stored, the version of QR will also change. To determine the QR version automatically, set the version to none and while making the code set ‘fit’ parameter as true.


Error Correction

Error correction used for the QR code is controlled by the error correction parameters. There are four constants of error correction in the QRcode package.

1. ERROR_CORRECT_L(7% or fewer errors can be corrected)

2. ERROR_CORRECT_M(15% or fewer errors can be corrected)

3. ERROR_CORRECT_Q(25% or fewer errors can be corrected)

5. ERROR_CORRECT_H(30% or fewer errors can be corrected)


if not specified ERROR_CORRECT_M would be taken as the default


Box Size

The pixel of each box of the QR code is controlled by the Box size parameter


Border

The thickness of the border is controlled by the border parameter. If not set, the default value is taken as ‘4’.


qr.add_data()

The information to be stored in the QR image is added using this method.


qr.make()

This makes the QR code.

Then the image is saved in the png format and later encoded.

img.save(temp, format="PNG")
qr_image = base64.b64encode(temp.getvalue())
self.qr_code = qr_image


XML CODE:

In the XML part, we just have to define the view for that field.   

 

        <record model="ir.ui.view" id="product_qr_code">
           <field name="name">product.template.qr.code</field>
           <field name="model">product.template</field>
           <field name="type">form</field>
           <field name="inherit_id" ref="product.product_template_only_form_view"/>
           <field name="arch" type="xml">
               <field name="barcode" position="after">
                   <field name="qr_code" widget='image' class="oe_avatar"/>
               </field>
           </field>
         </record>

The field defined in the XML should have widget = ‘image’ or else the QR image will not be displayed. Thus we can easily generate a QR code for the product.

generate-qr-code-odoo-13-cybrosys

When the barcode got scanned the following output was obtained.

generate-qr-code-odoo-13-cybrosys


Using this method one can generate a QR code image for any pieces of information in odoo.



If you need any assistance in odoo, we are online, please chat with us.



4
Comments

ayoub

can i do that in Odoo 14 ?

27/01/2022

-

12:43PM

Akshay

Hi Abdul, I have updated the XML code in the blog. Now you will be able to save the generated qr code.

17/03/2020

-

1:58AM

abdul hameed

Dear Sir, I appreciate your work. You're doing good work. I read your blog of qr code and I tried everything worked fine. Just when I save the product. QR Code disappears. May I know the reason. Thanks & Regards, Abdul Hameed

15/03/2020

-

1:23AM

POYA

According to this tutorial, how to display different types of qrcode in odoo report template ?? for instance (stricker barcode : EAN13)... and so on ..

10/11/2020

-

3:34AM



Leave a comment

 


whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message