Is there a way to call imagej macro (.ijm) from ja

2019-07-03 08:19发布

It is reversed problem to:

How can I call/execute a java program from an ImageJ macro?

Whenever I write imagej I refer to fiji.

Of course a trivial solution is to create a .ijm from java string and call imagej using a system call with .ijm as an argument, but I search for better solution. Alternatively .ijm instructions can be translated into java calls but it is not very convenient (when .jvm is enough for the job it is very convenient way to keep it in this format).

Why is it useful? It can be used e.g. to distribute a .ijm macro in obfuscated way - make it more difficult to the user to extract the macro. String containing instructions for .ijm can be decrypted only when correct password is provided, etc. Thank you for any suggestions!

2条回答
一纸荒年 Trace。
2楼-- · 2019-07-03 08:28

Use the ij.IJ.runMacro(String) method.

Here is an example:

import ij.IJ;
import ij.plugin.PlugIn;

public class Run_Macro implements PlugIn {
    @Override
    public void run(final String arg) {
        final String macro =
            "run(\"Clown (14K)\");\n" +
            "run(\"Make Binary\");\n";
        IJ.runMacro(macro);
    }
}

A word of warning about obfuscation, though: Java is notoriously easy to decompile.

查看更多
混吃等死
3楼-- · 2019-07-03 08:49

Correct me if I am wrong but one could call IJ.runMacroFile to refer to a given macro text file in the macro folder.

public void My_Macro() {
        IJ.runMacroFile("My_Macro.txt");
}

Yet I don't know if you can run macros outside of the macro folders, for example from within a .jar

查看更多
登录 后发表回答