我需要在服务器上3D渲染一个静止的图像。我被告知Java servlet可以做到这一点。我正在研究它的快速原型,但卡住了。
如何在Java中调用Blender??
import java.io.*;
import javax.servlet.http.*;
@WebServlet("/HelloworldInServlet")
public class HelloworldInServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//Run Blender on server (localhost for now)
// I need something here??
}
}发布于 2012-12-03 22:23:54
我能想到的最简单的解决方案是这样的:
public class RenderSceneServlet extends HttpServlet {
protected void doGet(HttServletRequest request, HttpServletResponse response) {
String outputFile = "/path/to/output-file";
// execute command to render a frame from scene.blend
Runtime runtime = Runtime.getRuntime();
Process exec = runtime.exec("blender -b scene.blend -o " + outputFile + " -F JPEG -x 1 -f 1");
// wait for blender to finish
exec.waitFor();
// serve the rendered file
File file = new File(outputFile);
byte[] bytes = FileUtils.readFileToByteArray(file);
response.setContentLength(bytes.length);
response.setContentType("image/jpeg");
response.getOutputStream().write(bytes);
}
}你应该记住几件事:
blender使用备用名称,否则并发请求将覆盖文件每个time.
https://stackoverflow.com/questions/13684011
复制相似问题