在 Java 中获取 Mathematica 图像
我正在尝试使用 J/Link 将图像从 Mathematica 获取到 Java。我能够像这样在 Mathematica 中打印图像:
Print[ Graphics[Raster[ img[[1]] ], AspectRatio->Automatic, ImageSize->530 ] ];
我尝试以各种方式从 Mathematica 函数返回数据:
Return [ Image[Graphics[Raster[ img[[1]] ], AspectRatio->Automatic, ImageSize->530 ]] ];
Return [ Graphics[Raster[ img[[1]] ], AspectRatio->Automatic, ImageSize->530 ] ];
Return [ Raster[ note1[[1]] ] ];
Return [ note1[[1]] ];
我的 Java 代码:
ml.evaluate("tmp = renderImageGeneric[" + sampleId + ", noteText," + sizeX + "," + sizeY + ", margin," + dpi + "," + lineStep + "," + tabStep + "," + ligatureMatch + "," + maxLigHeightDiff + "," + mmSearch + "," + highToLowGap + "," + lowToHighGap + "," + wordBaselineVariance + "," + debugFlag + "]");
ml.discardAnswer();
byte[] res = ml.evaluateToImage("tmp", 0, 0);
ByteArrayInputStream strm = new ByteArrayInputStream(res);
BufferedImage imag = ImageIO.read(strm);
//BufferedImage imag = ImageIO.read(new InputStream(res));
if(imag != null) {
ImageIO.write(imag, "png", new File("/Users/Rebecca/","test.png"));
}else {
System.out.println("image is null");
}
调试时,我在 res 中得到一个大字节数组。图像确实被保存,但它是空白的(即白色图像)。
如果我只返回一个字符串(“蓝色”),则会保存带有蓝色字符串的图像。
我假设我需要在 renderImageGeneric 中返回不同的东西,但我不知道是什么。
谢谢!
I am attempting to use J/Link to get an image from Mathematica to Java. I am able to print the image in Mathematica like this:
Print[ Graphics[Raster[ img[[1]] ], AspectRatio->Automatic, ImageSize->530 ] ];
I've tried returning the data from the Mathematica function in various ways:
Return [ Image[Graphics[Raster[ img[[1]] ], AspectRatio->Automatic, ImageSize->530 ]] ];
Return [ Graphics[Raster[ img[[1]] ], AspectRatio->Automatic, ImageSize->530 ] ];
Return [ Raster[ note1[[1]] ] ];
Return [ note1[[1]] ];
My Java code:
ml.evaluate("tmp = renderImageGeneric[" + sampleId + ", noteText," + sizeX + "," + sizeY + ", margin," + dpi + "," + lineStep + "," + tabStep + "," + ligatureMatch + "," + maxLigHeightDiff + "," + mmSearch + "," + highToLowGap + "," + lowToHighGap + "," + wordBaselineVariance + "," + debugFlag + "]");
ml.discardAnswer();
byte[] res = ml.evaluateToImage("tmp", 0, 0);
ByteArrayInputStream strm = new ByteArrayInputStream(res);
BufferedImage imag = ImageIO.read(strm);
//BufferedImage imag = ImageIO.read(new InputStream(res));
if(imag != null) {
ImageIO.write(imag, "png", new File("/Users/Rebecca/","test.png"));
}else {
System.out.println("image is null");
}
When debugging, I get a large byte array in res. The image does get saved, but it's blank (i.e. a white image).
If I return just a string ("blue"), an image with the string blue gets saved.
I'm assuming that I need to return something differently in renderImageGeneric, but I can't figure out what.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法正确地从函数中获取返回值。然而,我改变了方向,让 Mathematica 将图像输出到文件中。我传递了 Mathematica 函数的路径,以便 Java 知道它在哪里。这不是最好的解决方案,但它确实有效。
I wasn't able to get the return from the function correctly. However, I changed course and had Mathematica output the image to a file. I passed a path to the Mathematica function so that Java would know where it is. Not the best solution but it does the trick.
你有没有尝试过类似的东西
Did you try somenthing like