我试图使用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中返回不同的东西,但我无法弄清楚是什么.
谢谢!