function varargout = QRcode(varargin)
% QRCODE MATLAB code for QRcode.fig
% QRCODE, by itself, creates a new QRCODE or raises the existing
% singleton*.
%
% H = QRCODE returns the handle to a new QRCODE or the handle to
% the existing singleton*.
%
% QRCODE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in QRCODE.M with the given input arguments.
%
% QRCODE('Property','Value',...) creates a new QRCODE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before QRcode_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to QRcode_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help QRcode
% Last Modified by GUIDE v2.5 12-Jun-2013 02:11:59
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @QRcode_OpeningFcn, ...
'gui_OutputFcn', @QRcode_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before QRcode is made visible.
function QRcode_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to QRcode (see VARARGIN)
% Choose default command line output for QRcode
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes QRcode wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = QRcode_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --------------------------------------------------------------------
function recognize_Callback(hObject, eventdata, handles)
% hObject handle to recognize (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[FileName, PathName]=uigetfile(...
{ '*.jpg', 'JPG格式(*.jpg;*.jpeg;*.jpe)'; ...
'*.bmp', 'BMP格式(*.bmp)'; ...
'*.gif', 'GIF格式(*.gif)'; ...
'*.png', 'PNG格式(*.png)'; ...
'*.*', '所有文件(*.*)'}, ...
'选择文件');
str = [PathName,FileName];
p = imread(str); %将图片赋值于句柄
imshow(str);
im = rgb2gray(p);
zxingpath = fullfile(fileparts(mfilename('fullpath')), 'zxing.jar');
javaaddpath(zxingpath);
import com.google.zxing.*
im = im2java(im);
width = im.getWidth(); % if getWidth is not called, getBufferedImage will fail.
height = im.getHeight();
source = client.j2se.BufferedImageLuminanceSource(im.getBufferedImage());
binarizer = common.HybridBinarizer(source);
bitmap = BinaryBitmap(binarizer);
reader = MultiFormatReader();
ret = char(reader.decode(bitmap));
xlabel(ret);
% --------------------------------------------------------------------
function generation_Callback(hObject, eventdata, handles)
% hObject handle to generation (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str = {'请输入(英文字符)','图片宽度','图片高度'};
defans = {'happy_every_day','100','100'};
InVlue = inputdlg(str,'输入',1,defans);
content = InVlue{1};
width = str2double(InVlue{2});
height = str2double(InVlue{3});
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