user3415485
user3415485

Reputation: 1

J/Link (MathLink) - evaluateToImage() always returns null

I'm currently working on an application that uses J/Link (MathLink). However, I am having some trouble with the KernelLink.evaluateToImage() function.

My current code is:

byte[] gifData = kl.evaluateToImage("Plot[x,{x,0,1}]",0,0);
            if (gifData != null) {
                BufferedImage img = ImageIO.read(new ByteArrayInputStream(gifData));
                int w = img.getWidth();
                int h = img.getHeight();
                BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
                Graphics g = bi.getGraphics();
                g.drawImage(img, 0, 0, null);
            }
            else {
                System.out.println("Not a valid Graphics Expression.");
            }

I am always entering the else-clause because evaluateToImage somehow always returns null...

I launched the Kernel with:

kl = MathLinkFactory.createKernelLink("-linkmode launch -linkname 'math -mathlink'");

Can you help me with this issue? Best Regards NikNak

Upvotes: 0

Views: 81

Answers (1)

user3415485
user3415485

Reputation: 1

The solution was using the evaluateToTypeset() function. The correct code must be:

byte[] gifData = mathLink.evaluateToTypeset(mathInput, 0, true);

                    FileOutputStream fileStream = new FileOutputStream(new File("/../1.gif"));
                    fileStream.write(gifData);
                    fileStream.close();

Upvotes: 0

Related Questions