Getting an image with JLink
- To: mathgroup at smc.vnet.net
- Subject: [mg116736] Getting an image with JLink
- From: Rebecca Miller-Webster <rebecca at graciouseloise.com>
- Date: Fri, 25 Feb 2011 06:37:15 -0500 (EST)
Hi, I am writing a Java program to get an image (or byte array) from a Mathematica program. My Java code looks something like this: ml.evaluate("tmp = renderImageGenericLig[" + sampleFormId + ", noteText," + sizeX + "," + sizeY + ", margin," + dpi + "," + lineStep + "," + tabStep + "," + ligatureMatch + "," + maxLigHeightDiff + "," + mmSearch + "," + highToLowGap + "," + lowToHighGap + "," + wordBaselineVariance + "," + debugFlag + "]"); ml.discardAnswer(); byte[] res = ml.evaluateToImage("tmp", 0, 0); try { BufferedImage imag = ImageIO.read(new ByteArrayInputStream(res)); //BufferedImage imag = ImageIO.read(new InputStream(res)); if(imag != null) { ImageIO.write(imag, "jpg", new File("/Users/Rebecca/","test.jpg")); }else { System.out.println("image is null"); } } catch (IOException ex) { System.out.println("IOException occurred: " + ex.getMessage()); } The renderImageGenericLig function is returning: Return [ Graphics[Raster[ note1[[1]] ], AspectRatio -> Automatic, ImageSize -> 530 ] ]; Note: I've also tried returning just Raster as well as Image[Graphics ... In debug mode, from a notebook, I can render the image using: Print[ Graphics[Raster[ note1[[1]] ], AspectRatio -> Automatic, ImageSize -> 530 ] ]; However, when I check the saved file, it is just a white image with nothing in it. When debugging the Java code, I see to be getting data in the byte array, but when I convert to image with Java, I just end up with 0s or 1s. I don't know Mathematica very well and I am having a hard time finding examples of JLink in Java connecting to Mathematica. Any help would be appreciated. Thanks! Rebecca