2009年9月27日日曜日

NyARToolkit-2.3.1 for Java on Snow Leopard (using OpenCV/JNI)

Environment
01. MacOSX 10.6.1 (Snow Leopard)
02. Eclipse (Java IDE)

Resouce
01. NyARToolkit-2.3.1 for Java (NyARToolkit-2.3.1.zip)
02. OpenCV / Processing and Java Library (opencv_01.zip) (reference: this, this)

Eclipse
01. Make a 'Java Project'
02. DnD (Drag and Drop) from 'NyARToolkit-2.3.1/src/jp' folder to 'src' folder.
03. DnD from 'NyARToolkit-2.3.1/src.utils/qt/jp' folder to 'src' folder.
04. DnD from 'NyARToolkit-2.3.1/sample/qt/jp' folder to 'src' folder.
05. DnD from 'NyARToolkit-2.3.1/Data' folder to the project top (project icon).
06. DnD from 'OpenCV/library' folder to the project top (project icon).
07. Add to build path the 'OpenCV/library/OpenCV.jar' file.
08. Delete the 'jp.nyatla.nyartoolkit.qt.utils.QtCameraCapture.java' file.
09. Create the 'test.CameraCapture.java' file.
package test;

import hypermedia.video.OpenCV;
import jp.nyatla.nyartoolkit.qt.utils.QtCaptureListener;

public class CameraCapture implements Runnable {
private QtCaptureListener listener;
protected byte[] p;

int w;
int h;
int r;
private OpenCV cv;
private Thread t;

public CameraCapture(int _w, int _h, float _r) {
w = _w;
h = _h;
r = (int)(1000.0 / _r);
p = new byte[w * h * 3];

cv = new OpenCV();
cv.capture(w, h);
}

public void setCaptureListener(QtCaptureListener _listener) {
listener = _listener;
}

public void start() {
t = new Thread(this);
t.start();
}

public void stop() {
t = null;
cv.dispose();
}

public void run() {
while (t != null && cv != null) {
try {
Thread.sleep(r);
cv.read();
int[] p_int = cv.pixels();
int j = 0;
for (int i = 0; i < w * h; i++) {
p[j++] = (byte) (p_int[i] >> 16);
p[j++] = (byte) (p_int[i] >> 8);
p[j++] = (byte) (p_int[i]);
}
listener.onUpdateBuffer(p);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

Package Explorer like this

09. Edit the 'jp.nyatla.nyartoolkit.qt.sample.NyarToolkitLinkTest.java' file.
09.a the 2 variables's value
 private final String CARCODE_FILE = "./Data/patt.hiro";
private final String PARAM_FILE = "./Data/camera_para.dat";

09.b Replace from 'QtCameraCapture' to 'CameraCapture'.

10. Edit the 'jp.nyatla.nyartoolkit.qt.sample.NyarToolkitLinkTest.java' file and the 'jp.nyatla.nyartoolkit.qt.sample.QtCaptureTest.java'
10.a Add the 'import test.CameraCapture;'
10.b Replace from 'QtCameraCapture' to 'CameraCapture'.

11. Run
11.a Set the run configuretion

VM arguments: -d32 -Djava.library.path=${project_loc}/library
(The upper image is used a wrong name (lib).

11.b Run the 'jp.nyatla.nyartoolkit.qt.sample.NyarToolkitLinkTest'.


Result

0 件のコメント: