Code covered by the BSD License  

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
QR Code encode and decode

4.3 | 17 ratings Rate this file 78 Downloads (last 30 days) File Size: 14.9 KB File ID: #29239 Version: 1.0
image thumbnail

QR Code encode and decode


Lior Shapira (view profile)


Functions to encode and decode a QR bar-code in images

| Watch this File

File Information

A wrapper to the zxing library ( This submission includes files to encode a QR code from a string message, and decode a string message from an image containing an existing QR code. With little work these functions can be expanded to search for multiple bar-codes in images, decode multiple bar-codes in a single image etc.


This file inspired Qr Code Encoder.

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.11 (R2010b)
Other requirements Download and build the core and javase components of the zxing library (see description) and make sure they are in Matlab's dynamic java classpath.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (37)
18 Mar 2016 GP

GP (view profile)

05 Feb 2016 Mina Fouad  
22 Apr 2015 VIBHATH V B

I cannot run this code. I am looking for decoding an image containing qr code. How to do that?
When I run the decode-qr code, I am getting an error "Error using im2java2d Expected input number 1, Image, to be one of these types:uint8, uint16, double, logical".

Comment only
30 Mar 2015 Apurva Bhargava

hello,when i am running the file test_qr, qr code is being generated and then i save the image at desktop location,But when i use another file named "decode_qr" to decode the same saved image, which was generated by test_qr, it displays an error titled "no qr code found." this decode_qr.m file is working for other downloaded qr codes. kindly help me out.

Comment only
04 Feb 2015 mathivanan ponnambalm

I have Two questions related to QR Code
1. what the maximum size of data that can be embedded inside a QR Code
2. whether 7% of error is acceptable to decode 100 data

Comment only
23 Jan 2015 paria

paria (view profile)

Hey, when I want use the decode file, it shows error like:
Error in decode_qr (line 34)
source = BufferedImageLuminanceSource(jimg);
would you please help me how to solve it?

Comment only
14 Sep 2014 kailash singh  

Ok, it worked for me. Thank you very much.

05 Jun 2014 Nasir Ali

it gives an error on "BufferedImage"
This is error message.can you please tell me what is the solution?

??? Undefined function or method 'BufferedImageLuminanceSource' for input arguments of type

Error in ==> decode_qr at 34
source = BufferedImageLuminanceSource(jimg);

Comment only
08 May 2014 li

li (view profile)

the great program of QR encoder,i have a quetion how to use RSENC in matlab to get error codewords with rs code mathod.i only know how to use BCHENC .hope your help ,thank you!

22 Apr 2014 Bahtosai

Hi all,
I am curently working on QR Barcode. I found the link zxing have move. Can someone share to me or guide me step by step how to encode/decode QR barcode using matlab. I just know to use Matlab, and no eperience with JAVA etc. Many thanks.

if anyone have fully matlab source code for QR barcode encode/decode please share with me. Thanks.

Comment only
20 Mar 2014 dreamsyoung

Hello,have you build this qrcode file successifully?I have some error problems in this matlab file building,can you send your successful qrcode file to my email?thank you!

Comment only
17 Feb 2014 haem

haem (view profile)

03 Dec 2013 Peng Wenbin

Better to use java directly.

07 Aug 2013 Yanshuai Tu

the two jar files can be download via google search. Does anyone know how to control the Error Correction Level in generation

21 Apr 2013 Eugenijus Januskevicius

Hi Wang,

i had thee same problem with Lithuanian symbols.

Here is the patch: add

qr_hints.put(EncodeHintType.CHARACTER_SET, 'UTF-8');

just after qr_hints.put(EncodeHintType.ERROR_CORRECTION, qr_quality);
in the 'encode_qr.m'

It will probably make QR code unreadable by any other QR decoder, except ZXing's.

Comment only
07 Apr 2013 wang xiaoer

I got two problems:one was that a question mark would be found in the front of the information embedded when it contained a chinese character at least;the other was that,I got nothing when it contained more than sixty-six characters including punctuations,blank spaces.Can you give me some suggestions?Thanks in advance.

31 Jul 2012 Rob

Rob (view profile)

Thank you for your code.
I have the code working using zxing 2.0 with Gonzalo modifications. Well, no
error message but nothing is displayed on
my image, no QR! I get a green 32x92 square! can figure out what is wrong, can any one help please?. Thanking you in advance.

31 Jul 2012 Rob

Rob (view profile)

24 Jun 2012 ejs

ejs (view profile)

Thanks to Gonzalo, i was able to get the code to work. Seems zxing made an Unexpected Update recently.
Besides, i've made it to use error correction hints

14 May 2012 Gonzalo Garateguy

Matt, I was having the same problem but I find out that the method to obtain the height and width are slightly different you have to change M_java.height for M_java.getHeight() and M_jata.width for M_java.getWidth()

Comment only
04 May 2012 Adnan Jahangir

There is a problem in encoding algorithm. Due to this you cannot decode the code generated by 'encode_qr'. To solve this problem open the function file 'encode_qr.m'. At the end of the file you will find the line - 'qr = logical(qr);'. Replace this with 'qr = 1-logical(qr);' (without apostrophe). Now plot the image with 'imagesc' command and type 'colormap(gray)' in command window. Now save the image to any image file type. Now you will be able to decode it with 'decode_qr' command.

23 Apr 2012 ARIZ ZUBAIR

when i run test_qr.m following error is generated:-
Warning: Duplicate entry,C:\Users\ARIZ ZUBAIR\Desktop\qrcode\3rd_party\zxing-1.6\core\core.jar
> In javaclasspath>local_validate_dynamic_path at 247
In javaclasspath>local_javapath at 157
In javaclasspath at 102
In javaaddpath at 68
In test_qr at 6
Warning: Duplicate entry,C:\Users\ARIZ ZUBAIR\Desktop\qrcode\3rd_party\zxing-1.6\javase\javase.jar
> In javaclasspath>local_validate_dynamic_path at 247
In javaclasspath>local_javapath at 157
In javaclasspath at 102
In javaaddpath at 68
In test_qr at 7
??? Undefined function or variable 'QRCodeWriter'.

Error in ==> encode_qr at 28
qr_writer = QRCodeWriter;

Error in ==> test_qr at 10
test_encode = encode_qr('la la la', [32 32]);

please help me.

Comment only
15 Feb 2012 Matt

Matt (view profile)

Thanks for doing this code. I have installed ZXing and compiled the core and javase jar files.
My problem is that the test_qr.m function is giving me an error when I try to run it. The actual error is 'No appropriate method, property, or field
height for class'.
It indicates the error is in encoder_qr.m file on line 31. When I set the debugger to stop at line 31, I saw that M_java var on line 30 was only of size 1x1.
Is this variable supposed to behave like a struct and have some field values like .height and .width?
So the source of the error was that Matlab did not recognize M_java.height on line 31.
When I type M_java in the command line it gives me a picture of the QR code printed out, thought it doesn't appear to be sized 32 by 32.
Any help would be appreciated.

I'm a TA in class where we would like to used the QR codes to give cmds to the students' projects.

Thank you, Matt

Comment only
03 Feb 2012 Ahmed

Ahmed (view profile)

i am getting this. help me out

Warning: Invalid file or directory 'C:\Users\shehzil\Desktop\Lata\zxing-1.6\core\core.jar'.
> In javaclasspath>local_validate_dynamic_path at 270
In javaclasspath>local_javapath at 184
In javaclasspath at 119
In javaaddpath at 69
In test_qr at 6
Warning: Invalid file or directory 'C:\Users\shehzil\Desktop\Lata\zxing-1.6\javase\javasc.jar'.
> In javaclasspath>local_validate_dynamic_path at 270
In javaclasspath>local_javapath at 184
In javaclasspath at 119
In javaaddpath at 69
In test_qr at 7
??? Error: File: encode_qr.m Line: 25 Column: 8
Arguments to IMPORT must either end with ".*"
or else specify a fully qualified class name: "" fails this test.

Comment only
27 Nov 2011 Andreas Kraushaar

I downlowed both Zxing1.6 and 1.7 but I can't import, which is needed for QR-decode.
All other resources work fine.

05 Nov 2011 Jay

Jay (view profile)

Very handy! I am not familiar with Java programming, so what do I need to be able to tweak the encoding parameters such as the recommended 30% error correction level? Can I include it in my function call or I have to dive into the Java source code?

Comment only
05 Nov 2011 Jay

Jay (view profile)

18 Aug 2011 Jackie

Jackie (view profile)

Can't decode form QR-code image(include the encode function generator image)

Comment only
21 Jul 2011 Brandon

Never mind; I think I found the correct version of the file.

Comment only
21 Jul 2011 Brandon

where do I find Thanks!

Comment only
07 Jun 2011 Michael Chan

Michael Chan (view profile)

could you update the link to download zxing? The link you provided is not longer valid. Thank you.

Comment only
07 Jun 2011 mingming  
02 Apr 2011 Sven

Sven (view profile)

Sorry, my bad - works perfectly, had a bad path issue.

Comment only
22 Feb 2011 Tim Zaman  
10 Jan 2011 Lior Shapira

Lior Shapira (view profile)

See the example in test_qr.m, you need to add the java path to the QR library:

Make sure you unzip the zxing library and build it if necessary so you have the jar's

Comment only
19 Dec 2010 Mohammed Huq

how can i add the QR code class path! i do not find any of "QRreader" folder in "" can you please help!!

Comment only

Contact us