在 Java 中获取 Mathematica 图像

发布于 2024-10-19 06:29:37 字数 1375 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初雪 2024-10-26 06:29:37

我无法正确地从函数中获取返回值。然而,我改变了方向,让 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.

断念 2024-10-26 06:29:37

你有没有尝试过类似的东西

     Return[ExportString[Graphics[Disk[], ImageSize -> 100], "PNG"]]

Did you try somenthing like

     Return[ExportString[Graphics[Disk[], ImageSize -> 100], "PNG"]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文