Re: Getting an image with JLink
- To: mathgroup at smc.vnet.net
- Subject: [mg116806] Re: Getting an image with JLink
- From: Rebecca Miller-Webster <rebecca at graciouseloise.com>
- Date: Tue, 1 Mar 2011 05:20:40 -0500 (EST)
Hi, Thanks. I really need to call Mathematica from Java so your example doesn't quite work. However, I took your suggestion of saving the image to a file and that was successful ... and seems to be much faster. Thanks, Rebecca On Sun, Feb 27, 2011 at 4:35 AM, David Bailey <dave at removedbailey.co.uk>wrote: > On 25/02/2011 11:37, Rebecca Miller-Webster wrote: > > 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 > > > Wow - that looks a lot of work. J/Link lets you do things a lot easier > than that. Say you have a static class in your Java code called foo, > that takes a string argument, you can call it with: > > LoadJavaClass["something.something.MyClass"]; > something`something`MyClass.foo["xxxxx.jpg"]; > > (No Mathlink code in sight) > > Although it is possible to pass the image directly, it is better, I find > to put the image in a file and pass the name of the file. > > Alternatively, if your aim is to create a Java GUI, you might want to > try my free Super Widget Package, available from my website. > > David Bailey > http://www.dbaileyconsultancy.co.uk > >