阅读 55

【二维码识别】基于matlab GUI 灰度+二值化+校正二维码生成与识别【含Matlab源码 635期】

一、简介

基于matlab GUI 二维码生成与识别

二、源代码

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 13-Dec-2016 13:30:45

% 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;

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
global mtx
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
prompt = {'Please input (English character) ','Width','Heigth'};
dlg_title = 'Input';
defans = {'Input your text here !','128','128'};
InVlue = inputdlg(prompt,dlg_title,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,'Border','tight');
end

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (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(...
    { '*.png', 'PNG File(*.png)'; ...
      '*.bmp', 'BMP File(*.bmp)'; ...
      '*.gif', 'GIF File(*.gif)'; ...
      '*.jpg', 'JPG File(*.jpg)'; ...
      '*.*', 'All Files(*.*)'}, ...
     'Choose File');
 str = [PathName,FileName];
 p = imread(str);    
 imshow(str,'Border','tight');
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));
set(handles.edit1,'string',ret);
复制代码

三、运行结果

在这里插入图片描述

四、备注

版本:2014a

文章分类
人工智能
文章标签