2011-02-25 39 views
5

我正在嘗試使用J/Link將圖像從Mathematica轉換爲Java。我能夠將圖像打印在數學這樣的:在Java中獲取Mathematica圖像

Print[ Graphics[Raster[ img[[1]] ], AspectRatio->Automatic, ImageSize->530 ] ]; 

我試着從數學函數以不同的方式返回數據:

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中返回不同的東西,但我無法弄清楚什麼。

謝謝!

回答

2

我是不是能夠正確地獲得從函數的返回。但是,我改變了課程,並且讓Mathematica將圖像輸出到文件中。我通過了一條Mathematica函數的路徑,以便Java知道它在哪裏。不是最好的解決方案,但它有訣竅。

0

你嘗試somenthing喜歡

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

這也沒有工作。我不確定發生了什麼,但JLink似乎並沒有正確評估函數的返回值。 – rmw 2011-02-28 19:34:57