/**
* Created by web-214 on 2015/8/25.
*/
import com.wolfram.jlink.*;
import java.io.*;
public class SampleProgram {
public static void main(String[] argv) {
KernelLink ml = null;
try {
// ml = MathLinkFactory.createKernelLink(argv);
String jLinkDir = "D:\\Program Files\\Wolfram Research\\Mathematica\\10.2\\SystemFiles\\Links\\JLink";
System.setProperty("com.wolfram.jlink.libdir", jLinkDir);
ml = MathLinkFactory.createKernelLink(
"-linkmode launch -linkname 'D:\\program files\\wolfram research\\mathematica\\10.2\\mathkernel.exe'");
} catch (MathLinkException e) {
System.out.println("Fatal error opening link: " + e.getMessage());
return;
}
try {
// Get rid of the initial InputNamePacket the kernel will send
// when it is launched.
ml.discardAnswer();
ml.evaluate("<<MyPackage.m");
ml.discardAnswer();
ml.evaluate("2+2");
ml.waitForAnswer();
int result = ml.getInteger();
System.out.println("2 + 2 = " + result);
// Here's how to send the same input, but not as a string:
ml.putFunction("EvaluatePacket", 1);
ml.putFunction("Plus", 2);
ml.put(3);
ml.put(3);
ml.endPacket();
ml.waitForAnswer();
result = ml.getInteger();
System.out.println("3 + 3 = " + result);
// If you want the result back as a string, use evaluateToInputForm
// or evaluateToOutputForm. The second arg for either is the
// requested page width for formatting the string. Pass 0 for
// PageWidth->Infinity. These methods get the result in one
// step--no need to call waitForAnswer.
String strResult = ml.evaluateToOutputForm("4+4", 0);
System.out.println("4 + 4 = " + strResult);
String pic_path = "test11.jpg";
File file = new File(pic_path);
InputStream input = new FileInputStream(file);
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buffer = new byte[input.available()];
int rc = 0;
while ((rc = input.read(buffer, 0, 100)) > 0) {
swapStream.write(buffer, 0, rc);
}
// ml.putFunction("EvaluatePacket", 1);
// ml.putFunction("Last", 1);
// ml.putFunction("List", 2);
// ml.put(1);
// ml.put(2);
// ml.putFunction("EvaluatePacket", 1);
ml.putFunction("FindFaces", 1);
ml.putFunction("Import", 1);
ml.put("D:\\workspace\\Test\\test11.jpg");
ml.endPacket();
ml.waitForAnswer();
double[][] result_list = ml.getDoubleArray2();
for (int row = 0; row < result_list.length; row++) {
for (int column = 0; column < result_list[row].length; column += 2) {
System.out.println("{" + result_list[row][column] + "," + result_list[row][column + 1] + "}");
}
}
} catch (MathLinkException e) {
System.out.println("MathLinkException occurred: " + e.getMessage());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
ml.close();
}
}
}
Java通过Mathematica实现人脸识别
最新推荐文章于 2021-04-05 10:44:16 发布