您的位置:首页 > 编程语言 > MATLAB

解决了java+matlab混编+web(jsp)调用Matlab,网页中显示Figure,详细实例

2018-05-20 16:10 2081 查看
官方文档介绍的比较详细,建议先参照Implement a WebFigure开始学习

官方文档:

一: Figures and Images(官方汇总贴)

Manipulate MATLAB® graphics from a deployed application

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.

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 WebFigure

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

Image Streams

Execution of Applications that Create Figures

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

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 class
PATH
only 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
getPlot
function 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\lib
folder of the web application. The JSP file should be in the root folder of the web application.

Open the
WEB-INF\web.xml
file 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-INF
folder of your web application.

In the JSP file, add a reference to the
WebFigure
tag 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.tld
file from the
WEB-INF
folder 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:
name
and
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 NameDescriptionOptional?Default Value
name
Name used when attaching your figure to a cache. Case sensitive.YesThe 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
application
or
session
).
YesIf 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.YesIf this is not passed, a basic iFrame is used.
height
Height of the iFrame that will be embedded.YesIf this is not passed, the height of the WebFigure is retrieved from cache.
width
Width of the iFrame that will be embedded.YesIf this is not passed, the width of the WebFigure is retrieved from cache.
root
Name used to map the
WebFiguresServlet
for a figure.
YesIf 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的配置我就不说了,如果想做这个,首先应该配置好环境)祝君好运!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐