Calling zxing code in MATLAB to generate QR codes

Source: Internet
Author: User
Tags creative commons attribution in python
This work is licensed under the Creative Commons Attribution-NonCommercial use-Share 3.0 Unported License agreement in the same way. Non-commercial reprint is permitted, but the author and source should be noted.
Author: Xialulee
Originally released: October 30, 2011, Http://blog.sina.com.cn/xialulee If you do not compile Java source code, or if you do not use Java in MATLAB, you can refer to this:
The detailed steps of Matlab calling Zxing-two-D code generation

Many computers in the class are not networked, sometimes in the above to see the MATLAB document, there are some links want to check, so you can come up with the Internet handheld devices, such as mobile phone Ah, MP3 player what, to see the content of these links. But it's not pleasant to enter a link address on a virtual keyboard on the touchscreen of a handheld device. Python scripts that were previously used in Python to generate two-dimensional codes via Jpype call zxing can convert text to QR Code, which allows you to use the camera on your device to get a link address directly from a scan. However, to use this script, you need to have Python and Jpype installed on your computer, but many computers do not have these. Since most of the computers here have matlab, and MATLAB is integrated with Java, so, wrote a Matlab function, call zxing to generate two-dimensional code.
In the "Python calls zxing generation QR code by Jpype", the Multiformatwriter object is used to obtain the Bitmatrix object of the QR code, which is written to an image file by Matrixtoimagewriter. However, it is a bit cumbersome to create a file for the generated link each time, so a little research on the Bitmatrix object returned by Multiformatwriter's Encode method, after discovering that the ToString method was called, A two-dimensional code image represented by a string is obtained, where two contiguous spaces represent a light pixel, an ' X ' and a space represent a deep pixel. Thus, in MATLAB, this string is converted into a 0-1 matrix and used imshow to display it, avoiding the creation of the file. The code for the function is as follows. To use this function normally, we need to download the source code of zxing, compile it, and package the compiled related class into Zxing.jar.


function MTX = Qrgen (content, width, height)
% Generate QR using zxing
% compile the zxing source code and zip it into a jar file named
% Zxing.jar
% Put Zxing.jar in the same directory of this file.
% Parameters:
% content:string to be encoded
% width:the width of the image
% height:the height of the image
% Matlab 7.11
% Xialulee 2011.10.30 PM 08:18
Zxingpath = FullFile (Fileparts (mfilename (' FullPath ')), ' Zxing.jar ');
c = Oncleanup (@ () Javarmpath (Zxingpath));
Javaaddpath (Zxingpath);
writer = Com.google.zxing.MultiFormatWriter ();
BITMTX = Writer.encode (content, Com.google.zxing.BarcodeFormat.QR_CODE, ...)
width, height);
MTX = char (BITMTX);
Clear BITMTX Writer
MTX (mtx==10) = []; % remove \ n
MTX = Reshape (MTX (1:2:end), width, height) '; % remove extra space and transpose
MTX (mtx~= ' X ') = 1;
MTX (mtx== ' X ') = 0;
MTX = double (MTX);
if nargout = = 0
Imshow (MTX);
End
End% c executes Javarmpath (Zxingpath) here

The packaged Zxing.jar is placed in the same directory as the qrgen.m above, because Qrgen's code uses Mfilename to get its own directory, and Zxing.jar is also in this directory. Use the effect as follows:


Transferred from: http://blog.sina.com.cn/s/blog_4513dde601019mfh.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.