解决了java+matlab混编+web(jsp)调用Matlab,网页中显示Figure,详细实例
2018-05-20 16:10
2081 查看
官方文档介绍的比较详细,建议先参照Implement a WebFigure开始学习
Java® applications can display MATLAB generated figures and images in three ways:
Display the generated image on the local machine.
When your MATLAB code opens a figure window, the MATLAB Runtime displays the window on the local machine. The displayed figure is a fully functional MATLAB figure and can be manipulated as such. The only limitation is that it cannot be displayed on remote machines and is therefore not suitable for distributed applications.
Export the figure as an image stream.
Image streams are the native Java mechanism for handling image data. Generated MATLAB images are returned the to the Java application as static binary image streams. The Java application layer is free to manipulate the data using its own image processing capabilities. To leverage the MATLAB capabilities, the Java application must have the MATLAB code generate a new image.
Export the figure using the WebFigures feature.
WebFigures displays the generated images in a web browser using Java Servlet Page(JSP)s. The displayed figures and images have basic controls for zooming, rotating, and positioning.
Example illustrating how to implement a WebFigure
Attach a WebFigure to a Server Cache
Information on ways to attach a WebFigure to server cache
Reference a WebFigure Using the JSP Tag
Information on using a JSP tag to reference a WebFigure
Reference a WebFigure Using an Embeddable String
Information on using an embeddable string to reference a WebFigure
Use MATLAB Compiler SDK Web Example Guide
Brief description of the web example guide
Example illustrating usage of waitForFigures in developing a console-based Java application that generates MATLAB figures
Display a MATLAB Plot on the Web using a Java Servlet
Example illustrating how to display a MATLAB plot on the web through a Java servlet
Work with MATLAB Figure and Image Data
Techniques to effectively working with figure and image data
Information on how WebFigures work
Supported Renderers for WebFigures
Information on supported renderers for WebFigures
Configure the web server.
Install the MATLAB® Runtime in a location where it is accessible to the web server.
Add
Verify that your web server is properly configured by deploying the WebFigureQuickStart application.
Deploy
Open
The following default figure page appears:
Click and drag to manipulate the figure.
For example, to zoom in the figure, click the magnifying glass icon, then hover over the figure.
Compile the
Create a web application containing the jar file generated by MATLAB Compiler SDK™ and a JSP file to display the figure.
The generated jar file should be placed in the
Open the
Copy
In the JSP file, add a reference to the
In the JSP file, add a reference to the package you created using MATLAB Compiler SDK.
In the JSP file, add references to the MATLAB Compiler SDK packages that implement WebFigures.
In the JSP file, add code to instantiate the deployed class.
Return the WebFigure and attach it to tag for display.
Run your application.
Your custom WebFigure appears:
The code references the
WebFigure Tag Attributes and Their Default Values describes the WebFigure tag attributes.
WebFigure Tag Attributes and Their Default Values
===============================================================
例子简介
网上java+matlab混编的挺多,大多数实例也都能实现出来,但是将matlab生成出来Figure显示在jsp页面中并配合WebFigure实现的例子却很少,所以我将自己的尝试结果详细的写出来。
为什么我要强调环境配置都一样呢,首先,我之前Matlab用的是2013a32位的,会报错,Java和Matlab混编要求环境是统一的,同时我之前jdk用的是1.7.0_75,画不出图来,因为matlab必须联合1.6的jdk才能画出图来。
生成的文件夹
1.4 新建一个 Java Project工程,工程名JavaUseMatlab
右键点击工程名,添加(.jar)包
在Java build Path的Libraries下,点击Add External JARS添加javabuilder.jar和建立的matlabplot.jar,如下图所示
javabuilder.jar和matlabplot.jar的位置如下图
并在,MathPlotTest.java文件编写程序
结果如下图所示
这个过程就完成,这个例子网上有很多人做,我主要是在后面将生成的Figure,用JSP发布在网页上。
生成的目录文件如下图所示
右键点击建立的myweb工程,建立一个jsp文件
在libraries下面,Add External JARs,将由matlab 生成的plotWeb.jar添加进来;plotWeb.jar在plotWeb的disturb文件夹下,以及matlab安装文件下的javaBuilder.jar文件,文件路径:D:\Program Files\MATLAB\R2013a\toolbox\javabuilder\jar\win64
同时将plotWeb.jar和javaBuilder.jar,拷贝到myweb工程下,WEB-INF下lib文件夹下面,如下图。
同时拷贝webfigures.tld文件,文件目录
D:\ProgramFiles\MATLAB\R2013a\toolbox\javabuilder\webfigures\ 到myweb的
WEB-INF文件夹下,如下图所示。
最后的工程项目机构图如下图所示:
<%@ taglib prefix="wf"uri="/WEB-INF/webfigures.tld"%>
引用WebFigure库,以及自己编写的Matlab类库,另外引用
com.mathworks.toolbox.javabuilder.*,也是必须的,因为其中包含一些Errors定义以及必要的类和接口定义。
<%@ page import="com.mathworks.toolbox.javabuilder.webfigures.WebFigure"%>
<%@ page import="com.mathworks.toolbox.javabuilder.*" %>
<%@ page import="com.mathworks.toolbox.javabuilder.internal.*" %>
<%@ page import="plotWeb.MathPlotSin"%>
最后再body体内添加代码:如图所示
我也是不断试验好久才搞出来的,这是我以画正弦曲线,写的一个详细例子,(Tomcat的配置我就不说了,如果想做这个,首先应该配置好环境)祝君好运!
官方文档:
一: Figures and Images(官方汇总贴)
Manipulate MATLAB® graphics from a deployed applicationJava® applications can display MATLAB generated figures and images in three ways:
Display the generated image on the local machine.
When your MATLAB code opens a figure window, the MATLAB Runtime displays the window on the local machine. The displayed figure is a fully functional MATLAB figure and can be manipulated as such. The only limitation is that it cannot be displayed on remote machines and is therefore not suitable for distributed applications.
Export the figure as an image stream.
Image streams are the native Java mechanism for handling image data. Generated MATLAB images are returned the to the Java application as static binary image streams. The Java application layer is free to manipulate the data using its own image processing capabilities. To leverage the MATLAB capabilities, the Java application must have the MATLAB code generate a new image.
Export the figure using the WebFigures feature.
WebFigures displays the generated images in a web browser using Java Servlet Page(JSP)s. The displayed figures and images have basic controls for zooming, rotating, and positioning.
Functions
webfigure | Export a figure to a deployed application |
figToImStream | Stream figure as byte array encoded in specified format |
Examples and How To
WebFigures
Implement a WebFigureExample illustrating how to implement a WebFigure
Attach a WebFigure to a Server Cache
Information on ways to attach a WebFigure to server cache
Reference a WebFigure Using the JSP Tag
Information on using a JSP tag to reference a WebFigure
Reference a WebFigure Using an Embeddable String
Information on using an embeddable string to reference a WebFigure
Use MATLAB Compiler SDK Web Example Guide
Brief description of the web example guide
Image Streams
Execution of Applications that Create FiguresExample illustrating usage of waitForFigures in developing a console-based Java application that generates MATLAB figures
Display a MATLAB Plot on the Web using a Java Servlet
Example illustrating how to display a MATLAB plot on the web through a Java servlet
Work with MATLAB Figure and Image Data
Techniques to effectively working with figure and image data
Concepts
How Do WebFigures Work?Information on how WebFigures work
Supported Renderers for WebFigures
Information on supported renderers for WebFigures
二:Implement a WebFigure(基本使用)
This example implements a WebFigure. It deploys the WebFigure service and the page that has the WebFigure embedded in it on a single server. This configuration allows you to quickly reference your WebFigure from a JSP page with minimal configuration.Configure the web server.
Install the MATLAB® Runtime in a location where it is accessible to the web server.
Add
matlabroot
\toolbox\javabuilder\jar\javabuilder.jar to the classpath of the web server.Caution
This file uses native resources. It is critical that it exist in your Web server's classPATHonly once. Embedding this file into Web applications causes errors.
Verify that your web server is properly configured by deploying the WebFigureQuickStart application.
Deploy
matlabroot
\toolbox\javabuilder\jar\WebFigureQuickStart.war to your web server.Open
http://hostName
:portNumber/WebFigureQuickStart/WebFigureExample.jsp in a web browser.
The following default figure page appears:
Click and drag to manipulate the figure.
For example, to zoom in the figure, click the magnifying glass icon, then hover over the figure.
Tip
Once you have verified that your web server is properly configured you can undeploy the default WebFigure.Compile the
getPlotfunction as a Java® package.
function df = getPlot() f = figure('Visible','off'); x = -2:0.25:2; [X,Y] = meshgrid(x); Z = X.*exp(-X.^2-Y.^2); contour3(X,Y,Z,30); df = webfigure(f); close(f); end
Create a web application containing the jar file generated by MATLAB Compiler SDK™ and a JSP file to display the figure.
The generated jar file should be placed in the
WEB-INF\libfolder of the web application. The JSP file should be in the root folder of the web application.
Open the
WEB-INF\web.xmlfile and add the reference to
WebFigureServlet.
<servlet> <servlet-name>WebFigures</servlet-name> <servlet-class> com.mathworks.toolbox.javabuilder.webfigures.WebFiguresServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>WebFigures</servlet-name> <url-pattern>/WebFigures/*</url-pattern> </servlet-mapping>
Copy
MATLABROOT
/toolbox/javabuilder/webfigures/webfigures.tld, the WebFigures customer tag handler file, to the WEB-INFfolder of your web application.
In the JSP file, add a reference to the
WebFiguretag by including the following line of code at the beginning of the file.
<%@ taglib prefix="wf" uri="http://www.mathworks.com/builderja/webfigures.tld" %>
In the JSP file, add a reference to the package you created using MATLAB Compiler SDK.
<%@ page import="getPlot.Class1" %>
In the JSP file, add references to the MATLAB Compiler SDK packages that implement WebFigures.
<%@ page import="com.mathworks.toolbox.javabuilder.webfigures.WebFigure" %> <%@ page import="com.mathworks.toolbox.javabuilder.*" %>
In the JSP file, add code to instantiate the deployed class.
<%! Class1 myDeployedComponent; %> <%! public void jspInit() { try { //Instantiate the Deployed Component myDeployedComponent = new Class1(); } catch (Exception e) { e.printStackTrace(); } } %> <%! public void jspDestroy() { if (myDeployedComponent!=null) { myDeployedComponent.dispose(); } myDeployedComponent = null; } %>
Return the WebFigure and attach it to tag for display.
<% if (myDeployedComponent!=null) { try { // Get the WebFigure from your function's output WebFigure webFigure = (WebFigure) ((MWJavaObjectRef)myDeployedComponent.getPlot(1)[0]).get(); // set it to the tag request.getSession().setAttribute("MyFigure", webFigure); } catch(ClassCastException e) { throw new Exception("Issue casting deployed components outputs to WebFigure", e); } catch (Exception e) { e.printStackTrace(); } } else { out.println("no go"); } %> <!--height:指定高度,width:指定宽度 --> <wf:web-figure name="MyFigure" scope="session" height="800" width="1000"/>
Run your application.
Your custom WebFigure appears:
三:Reference a WebFigure Using the JSP Tag(JSP标签参数)
Once the WebFigure has been retrieved from the function output, you can attach it to one of your server's caches and reference it from the JSP tag.Initialize the JSP Tag
Reference the tag library by adding the following line to a JSP page:<%@ taglib prefix="wf" uri="http://www.mathworks.com/builderja/webfigures.tld" %>
The code references the
webfigures.tldfile from the
WEB-INFfolder of your web application folder. This URI must be typed exactly as shown above for the name to properly resolve the reference. Once this tag has been referenced, you can add tags to the page similar to this:
<wf:web-figure />
Note
If you use an empty tag as shown above, the default WebFigure appears. To bind the tag to your WebFigure, see Attach a WebFigure to a Server Cache.Attributes of a WebFigure Tag
The WebFigure tag has two key attributes:nameand
scope. These attributes indicate which figure to use in which cache on your server. To display a WebFigure,
MyFigure, attached to the session cache add the JSP tag:
<wf:web-figure name="MyFigure" scope="session"/>
WebFigure Tag Attributes and Their Default Values describes the WebFigure tag attributes.
WebFigure Tag Attributes and Their Default Values
Attribute Name | Description | Optional? | Default Value |
---|---|---|---|
name | Name used when attaching your figure to a cache. Case sensitive. | Yes | The name of the default WebFigure built into WebFigureService. If you provide an empty WebFigure tag, this figure is displayed. |
scope | Scope that your figure has been saved to (either applicationor session). | Yes | If this is not specified, an error is thrown unless the name is also not specified. In this case, the default figure is attached to the session scope and is used. |
style | Style attribute that you want embedded and attached to the iFrame. | Yes | If this is not passed, a basic iFrame is used. |
height | Height of the iFrame that will be embedded. | Yes | If this is not passed, the height of the WebFigure is retrieved from cache. |
width | Width of the iFrame that will be embedded. | Yes | If this is not passed, the width of the WebFigure is retrieved from cache. |
root | Name used to map the WebFiguresServletfor a figure. | Yes | If this is not specified, it is assumed to be mapped to WebFigures. If it is specified to a relative servlet end point, that is used. |
转帖文档
參考此文章,jdk1.8.0、Eclipse、Matlab2017b也成功运行。例子简介
网上java+matlab混编的挺多,大多数实例也都能实现出来,但是将matlab生成出来Figure显示在jsp页面中并配合WebFigure实现的例子却很少,所以我将自己的尝试结果详细的写出来。
一从Java和Matlab混合编程
1.1环境配置
我采用的是Java、jdk1.6.0_45、Eclipse、Matlab2013a,都是64位:为什么我要强调环境配置都一样呢,首先,我之前Matlab用的是2013a32位的,会报错,Java和Matlab混编要求环境是统一的,同时我之前jdk用的是1.7.0_75,画不出图来,因为matlab必须联合1.6的jdk才能画出图来。
1.2编写plotsin.m和plotcos.m文件
1.3编译写好的两个函数制作一个java包(.jar)(complie the pltosin function as a java Package)如下图所示,使用deploytool命令,然后build
生成的文件夹
1.4 新建一个 Java Project工程,工程名JavaUseMatlab
右键点击工程名,添加(.jar)包
在Java build Path的Libraries下,点击Add External JARS添加javabuilder.jar和建立的matlabplot.jar,如下图所示
javabuilder.jar和matlabplot.jar的位置如下图
1.4 新建个.java文件,右键工程名JavaUseMatlab,如下图所示
并在,MathPlotTest.java文件编写程序
1.5 Run As程序Java Application
结果如下图所示
这个过程就完成,这个例子网上有很多人做,我主要是在后面将生成的Figure,用JSP发布在网页上。
二 Java和Matlab混编 将生成figure发布网页上,直接使用JSP中的网页标签
以绘画的正弦曲线为例,进行说明2.1 绘制正弦曲线函数plotsin.m
2.2 编译写好的plotsin函数制作一个java包(.jar)(complie the pltosin function as ajava Package)如下图所示,使用deploytool命令,然后build
生成的目录文件如下图所示
2.3新建一个Dynamic Web Project
Eclipse+Tomacat7.0.59+JDK1.6.0_45右键点击建立的myweb工程,建立一个jsp文件
2.4右键myweb工程,点击Build Path下面的 Configure Build Path
在libraries下面,Add External JARs,将由matlab 生成的plotWeb.jar添加进来;plotWeb.jar在plotWeb的disturb文件夹下,以及matlab安装文件下的javaBuilder.jar文件,文件路径:D:\Program Files\MATLAB\R2013a\toolbox\javabuilder\jar\win64
同时将plotWeb.jar和javaBuilder.jar,拷贝到myweb工程下,WEB-INF下lib文件夹下面,如下图。
同时拷贝webfigures.tld文件,文件目录
D:\ProgramFiles\MATLAB\R2013a\toolbox\javabuilder\webfigures\ 到myweb的
WEB-INF文件夹下,如下图所示。
最后的工程项目机构图如下图所示:
2.5在生成的JSP文件下头部添加相应的引用项
引用webfigures.tld文件,定义标签项<%@ taglib prefix="wf"uri="/WEB-INF/webfigures.tld"%>
引用WebFigure库,以及自己编写的Matlab类库,另外引用
com.mathworks.toolbox.javabuilder.*,也是必须的,因为其中包含一些Errors定义以及必要的类和接口定义。
<%@ page import="com.mathworks.toolbox.javabuilder.webfigures.WebFigure"%>
<%@ page import="com.mathworks.toolbox.javabuilder.*" %>
<%@ page import="com.mathworks.toolbox.javabuilder.internal.*" %>
<%@ page import="plotWeb.MathPlotSin"%>
最后再body体内添加代码:如图所示
2.6修改目录下web.xml文件,加入如下代码
2.7部署myweb项目到Tomcat,启动Tomcat
在浏览器中输入如下网址: http://127.0.0.1:8080/myweb/Test.jsp我也是不断试验好久才搞出来的,这是我以画正弦曲线,写的一个详细例子,(Tomcat的配置我就不说了,如果想做这个,首先应该配置好环境)祝君好运!
相关文章推荐
- 解决了java+matlab混编+web(jsp)调用Matlab,网页中显示Figure,详细实例
- 解决了java+matlab混编+web(jsp)调用Matlab,网页中显示Figure,详细实例
- 使用输出流解决在网页(JSP)中显示文档(PDF等)问题
- iOS开发问题之:解决iOS网页调用系统相册显示英文的问题
- 求帮助——网页错误详细信息jsp代码中datetimepicker控件不能显示
- JVM虚拟机内存量详细显示--JSP网页版
- 解决jsp中input标签读取时间格式实例显示到页面
- C#源码刷新网页 最小化托盘http get和post请求配置保存版权时间限制定时调用 单实例运行,如果已经运行则激活窗口到最前显示
- MATLAB在调用C/C++程序成功,使用opencv库时出错,错误显示“找不到指定模块”(问题未解决)
- c#调用C/C++代码时出现的PInvokeStackImbalance异常实例及解决方式
- 实例讲解JSP调用SQL Server的存储过程
- WebView加载网页不显示图片解决办法
- 用 ajax 的方法解决网页广告显示的问题
- Android webview网页视频无法全屏解决(onShowCustomView不调用)
- JSP调用ORALCE存储过程显示简单的查询结果(以表格形式显示)
- 关于JSP 调用 OCX的问题【已解决】
- JSP自定义标签实例教程(网络整理较详细)
- 在jsp上显示pdf||网页直接打开PDF 在IE中显示pdf文档的方法及参数设置
- jsp中调用oracle存储过程的实例
- Linux系统下Jsp验证码显示不出来解决方法