1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| function qr = encode_qr(message, varargin)
% ENCODE_QR create a 2D QR code containing a message
%
% This function creates a QR code containing a string message. The QR code
% can be of varying sizes.
%
% Note that this function requires zxing (<a href="http://code.google.com/p/zxing/" target="_blank">http://code.google.com/p/zxing/</a>)
% installed, and core/core.jar, javase/javase.jar on the classpath
%
% This is the second attempt, based on the code of Lior Shapira, with the
% update to ZXing v. 2.1 codebase and 'parsepropval' for cleaner list of
% the parameters
% Parameters:
%
% message - string containing message
% 'Size' - size of the QR code, in pixels. As QR is quadratic, you
% can supply either scalar or vector. Of you supply vector,
% the size must be equal. If the parameter is not supplied.
% it is determined from 'Version'.
% 'Version', 'v[1..40]' - version of the QR code
% 'Quality', 'L|M|H|Q' - quality of the error correction (Low,
% Medium, High, super (not suree ;)
% 'M' quality is the default
% 'Character_set' - charecter set for encoding QR code. 'UTF-8' is
% set to default, other values to be retrieved from
% <a href="https://github.com/zxing/zxing/blob/master/core/src/com/google/zxing/common/CharacterSetECI.java" target="_blank">https://github.com/zxing/zxing/blob/...terSetECI.java</a>
% Using 'Character_set' any other than 'ISO-8859-1' will probably
% make QR unreadable to any other QR decoder, except ZXing's
%
% Returns:
%
% qr - logical matrix of size s containing the QR code
%
% Example:
% encode_qr(message, 'Character_set', 'ISO-8859-1')
% encode_qr(message, 'Size', 25)
% encode_qr(message, 'Size', [33 33])
% encode_qr(message, 'Version', 3, 'Quality', 'h', 'Character_set', 'UTF-16BE')
%
%% AUTHOR : Lior Shapira
%% $DATE : 02-Nov-2010 11:20:45 $
%% $Revision : 1.00 $
%% DEVELOPED : 7.11.0.584 (R2010b)
%% FILENAME : encode_qr.m
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
%% parse arguments
% using parsepropval from <a href="http://www.mathworks.com/matlabcentral/fileexchange/22671-parse-propertyvalue-pairs-and-structures/content/parsepropval.m" target="_blank">http://www.mathworks.com/matlabcentr...parsepropval.m</a>
props.size = [ 1 1 ]; % WRONG SIZE. Intentional
props.version = 'v1';
props.quality = 'M';
props.character_set = 'UTF-8';
props = parsepropval(props,varargin{:});
qr_init = 17; % minimal size of the QR code
qr_delta = 4; % size of the quiet zone
qr_quality = ['L' 'M' 'Q' 'H']; % error correction quality
% check supplied version
if (isa(props.version,'char') || isa(props.version,'numeric'))
if isa(props.version,'char')
qr_ver = str2num(props.version(2:end));
if not(strcmpi(props.version(1),'v'))
error('Try removing quotes at "version" parameter value')
end
else
qr_ver = props.version;
end
else
error('"version" must be string "v[1..40]" or numerical value [1..40]')
end
if ((qr_ver < 1) || (qr_ver > 40))
error('"version" must be in [1..40] range')
end
if (props.size(1) == 1) % matrix size was supplied, don't recalculate
props.size = qr_init+qr_delta*qr_ver;
end
% check quality
if isempty(strfind(qr_quality, upper(props.quality)))
error('"quality" must be string "L | M | Q | H"')
else
switch upper(props.quality)
case 'M'
qr_quality = ErrorCorrectionLevel.M;
case 'L'
qr_quality = ErrorCorrectionLevel.L;
case 'H'
qr_quality = ErrorCorrectionLevel.H;
case 'Q'
qr_quality = ErrorCorrectionLevel.Q;
end
end
% check supplied size
if isscalar(props.size)
props.size = [props.size props.size];
end
% re-check size
if (props.size(1) == props.size(2)) % must be equal
tmp = props.size(1) - 17;
if not(mod(tmp,4) == 0)
error('Matrix does not meet sizing requirements (17+N*4)')
end
else
error('Matrix must be square')
end
%% encoding qr
qr_writer = QRCodeWriter;
qr_hints = java.util.Hashtable;
% use hint for encoding type
qr_hints.put(EncodeHintType.ERROR_CORRECTION, qr_quality);
% use hint for character set
qr_hints.put(EncodeHintType.CHARACTER_SET, props.character_set);
M_java = qr_writer.encode(message, BarcodeFormat.QR_CODE, props.size(2), props.size(1), qr_hints);
qr = zeros(M_java.getHeight(), M_java.getWidth());
for i=1:M_java.getHeight()
for j=1:M_java.getWidth()
qr(i,j) = M_java.get(j-1,i-1);
end
end
clear qr_writer;
clear M_java;
qr = 1-logical(qr);
% Created with NEWFCN.m by Frank Gonzlez-Morphy
% Contact...: <a href="mailto:frank.gonzalez-morphy@mathworks.de">frank.gonzalez-morphy@mathworks.de</a>
% ===== EOF ====== [encode_qr.m] ====== |
Partager