【Matlab学习手记】二维码

目的:二维码的识别和生成。

  •  代码
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);


function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
global mtx
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uiputfile(...
    { '*.png', 'PNG File(*.png)'; ...
      '*.bmp', 'BMP File(*.bmp)'; ...
      '*.gif', 'GIF File(*.gif)'; ...
      '*.jpg', 'JPG File(*.jpg)'; ...
      '*.*', 'All Files(*.*)'}, ...
     'Save picture as');
%if user cancels save command, nothing happens
if isequal(filename,0) || isequal(pathname,0)
    return
end
%create a new figure
newFig = figure;
set(gcf,'visible','off');
imshow(mtx,'Border','tight');
% newAxes = copyobj(handles.axes2,newFig);  
% set(newAxes,'Units','default','Position','default','Color','default');
saveas(newFig,fullfile(pathname, filename)) 
%closes the figure
close(newFig)
clear global
  • 源码链接

https://download.csdn.net/download/u012366767/10593067

  • 4
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
### 回答1: Matlab可以通过调用现有的二维码生成库来实现二维码的生成。下面以zxing库为例,简要介绍如何在Matlab中实现二维码生成。 首先,需要下载zxing库并配置到Matlab中。可以从zxing官网(http://github.com/zxing/zxing)下载zxing的Java库文件,并将其添加到Matlab的Java classpath中。 接下来,使用Matlab中的Java对象来调用zxing库中的函数。首先,创建一个Java的BitMatrix对象,使用zxing库中的MultiFormatWriter类的静态方法encode()来生成一个BitMatrix对象。例如,可以使用以下代码生成一个大小为200x200像素的二维码: ```matlab import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix; text = 'Hello, World!'; % 要编码的字符串 width = 200; % 二维码宽度 height = 200; % 二维码高度 bitMatrix = MultiFormatWriter.encode(text, BarcodeFormat.QR_CODE, width, height); ``` 然后,将BitMatrix对象转换为Matlab中的图片格式,并显示二维码图片。通过遍历BitMatrix对象的每个像素点,设置对应的RGB值来创建一个Matlab的图像。例如,可以使用以下代码显示二维码图片: ```matlab import java.awt.image.BufferedImage; image = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for i = 1:width for j = 1:height if bitMatrix.get(i-1, j-1) image.setRGB(i-1, j-1, java.awt.Color.BLACK.getRGB()); else image.setRGB(i-1, j-1, java.awt.Color.WHITE.getRGB()); end end end imshow(image); ``` 以上就是利用Matlab实现二维码生成的简要步骤。通过调用zxing库,可以在Matlab中轻松地生成二维码。需要注意的是,这只是一个基本的示例,实际使用中还可以添加更多的功能和自定义选项来满足具体的需求。 ### 回答2: Matlab编程可以很容易地实现二维码的生成。首先,我们需要下载一个开源的解决方案,比如zxing(Zebra Crossing)。然后,将zxing导入到Matlab的工作环境中。 接下来,我们可以使用Matlab的图像处理工具箱来创建一个空白图像。然后,将二维码的数据编码为字节,并使用zxing库来生成二维码的矩阵。 一旦我们有了二维码的矩阵,我们可以使用Matlab的图像处理工具箱来将矩阵转换成一个图像。我们可以设置二维码的尺寸、颜色等属性,以满足我们的需求。 最后,我们可以使用Matlab的图像处理工具箱中的函数将图像保存为一个文件,比如PNG或JPEG格式。 总结起来,使用Matlab编程实现二维码生成需要借助开源库zxing,并结合Matlab的图像处理工具箱进行矩阵转换以及图像保存等操作。这样,我们就能够实现二维码的生成。 ### 回答3: 在Matlab中实现二维码生成需要使用第三方库来实现。其中一个常用的库是zxing,zxing是一个用Java编写的开源条码图像处理类库,它实现了对二维码的生成和解码。 首先,我们需要在Matlab中调用Java库,可以使用Matlab内置的Java类库支持来实现。首先需要将zxing库的jar文件导入到Matlab的Java路径中。在Matlab中打开引用导入窗口,将zxing的jar文件添加到路径中。 接下来,在Matlab中创建一个Java对象来调用zxing库的功能,生成二维码。可以使用javaObject函数来实例化一个Java对象。然后,使用该对象的方法来生成二维码,将需要编码的数据作为参数传递给该方法。 例如,可以使用zxing库中的MultiFormatWriter类的encode方法来生成二维码。首先需要创建一个BitMatrix对象,该对象用于存储生成的二维码。然后使用MultiFormatWriter的encode方法通过传递需要编码的数据和编码参数来生成二维码。 最后,可以使用Matlab中的图像处理函数将生成的二维码保存为图片文件。可以使用imwrite函数将BitMatrix对象转换为Matlab的图像格式,并保存为图片文件。 需要注意的是,由于在Matlab中调用Java库需要一定的额外配置和步骤,可能需要一些Java编程的基础知识和经验。同时,zxing库中还提供了其他更多的功能和设置,可以根据需求进行进一步定制和调整。 总之,使用Matlab编程实现二维码生成主要是通过调用第三方库来实现,并进行必要的参数设置和图像处理操作。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值