forked from fanruan/finekit
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
2.9 KiB
111 lines
2.9 KiB
6 years ago
|
package com.fanruan.api.script;
|
||
|
|
||
|
import com.fr.general.IOUtils;
|
||
|
import com.fr.graph.g2d.canvas.CanvasPainter;
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
|
||
|
import java.io.InputStream;
|
||
|
import java.awt.image.BufferedImage;
|
||
|
|
||
|
/**
|
||
|
* @author Bjorn
|
||
|
* @version 10.0
|
||
|
* Created by Bjorn on 2019-09-30
|
||
|
*/
|
||
|
public class FineCanvas {
|
||
|
|
||
|
CanvasPainter.Builder canvasBuilder;
|
||
|
|
||
|
/**
|
||
|
* 构造函数,创建js引擎并加载绘制使用的通用适配js文件
|
||
|
*/
|
||
|
public FineCanvas() throws Exception {
|
||
|
canvasBuilder = CanvasPainter.newDefaultBuilder();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 构造函数,创建js引擎并加载绘制使用的通用适配js文件,并加载指定的js文件
|
||
|
*
|
||
|
* @param filePath 指定的js文件路径的集合,使用相对路径即可。
|
||
|
*/
|
||
|
public FineCanvas(String... filePath) throws Exception {
|
||
|
canvasBuilder = CanvasPainter.newDefaultBuilder();
|
||
|
loadAndExecute(filePath);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 将js语句加载到缓存区。
|
||
|
*
|
||
|
* @param text js语句集合
|
||
|
*/
|
||
|
public void loadText(String... text) {
|
||
|
canvasBuilder.loadText(text);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 立即执行js语句
|
||
|
*
|
||
|
* @param text js语句集合
|
||
|
*/
|
||
|
public void loadTextAndExecute(String... text) throws Exception {
|
||
|
canvasBuilder.loadTextAndExecute(text);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 加载js文件
|
||
|
*
|
||
|
* @param filePath 指定的js文件路径的集合,使用相对路径即可。
|
||
|
*/
|
||
|
public void loadAndExecute(String... filePath) throws Exception {
|
||
|
for (String path : filePath) {
|
||
|
String content = readFileBody(path);
|
||
|
canvasBuilder.build().execute(content);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 执行缓存区的js语句,并返回canvas中的BufferedImage对象
|
||
|
*
|
||
|
* @return 绘制完成的图片对象
|
||
|
*/
|
||
|
public BufferedImage paint() throws Exception {
|
||
|
CanvasPainter painter = canvasBuilder.build();
|
||
|
try {
|
||
|
return painter.paint();
|
||
|
} catch (Exception ex) {
|
||
|
throw ex;
|
||
|
} finally {
|
||
|
painter.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 执行指定的js方法,并返回canvas中的BufferedImage对象
|
||
|
*
|
||
|
* @return 绘制完成的图片对象
|
||
|
*/
|
||
|
public BufferedImage paint(String functionName, Object... parameters) throws Exception {
|
||
|
CanvasPainter painter = canvasBuilder.build();
|
||
|
try {
|
||
|
return painter.paint(functionName, parameters);
|
||
|
} catch (Exception ex) {
|
||
|
throw ex;
|
||
|
} finally {
|
||
|
painter.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private String readFileBody(String filePath) {
|
||
|
InputStream in = FineCanvas.class.getResourceAsStream(filePath);
|
||
|
String script = StringUtils.EMPTY;
|
||
|
try {
|
||
|
script = IOUtils.inputStream2String(in);
|
||
|
} catch (Exception e) {
|
||
|
FineLoggerFactory.getLogger().error(e.getMessage(), e);
|
||
|
}
|
||
|
return script;
|
||
|
}
|
||
|
|
||
|
}
|