我需要在服务器中制作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??
}
}
答案 0 :(得分:3)
我能想到的最简单的解决方案如下:
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);
}
}
你应该记住一些事情:
outputFile
的交替名称,否则并发请求将每次覆盖该文件。