package org.opencv.test;
+import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
+import java.io.FileReader;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
}
protected static String readFile(String path) {
- FileInputStream stream = null;
try {
- stream = new FileInputStream(new File(path));
- FileChannel fc = stream.getChannel();
- MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0,
- fc.size());
- return Charset.defaultCharset().decode(bb).toString();
+ BufferedReader br = new BufferedReader(new FileReader(path));
+ String line;
+ StringBuffer result = new StringBuffer();
+ while ((line = br.readLine()) != null) {
+ result.append(line);
+ result.append("\n");
+ }
+ return result.toString();
} catch (IOException e) {
OpenCVTestRunner.Log("Failed to read file \"" + path
+ "\". Exception is thrown: " + e);
return null;
- } finally {
- if (stream != null)
- try {
- stream.close();
- } catch (IOException e) {
- OpenCVTestRunner.Log("Exception is thrown: " + e);
- }
}
}
extractor.write(filename);
String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.ORB</name>\n<WTA_K>2</WTA_K>\n<edgeThreshold>31</edgeThreshold>\n<firstLevel>0</firstLevel>\n<nFeatures>500</nFeatures>\n<nLevels>8</nLevels>\n<patchSize>31</patchSize>\n<scaleFactor>1.2000000476837158e+00</scaleFactor>\n<scoreType>0</scoreType>\n</opencv_storage>\n";
- assertEquals(truth, readFile(filename));
+ String actual = readFile(filename);
+ actual = actual.replaceAll("e\\+000", "e+00"); // NOTE: workaround for different platforms double representation
+ assertEquals(truth, actual);
}
public void testWriteYml() {
extractor.write(filename);
String truth = "%YAML:1.0\nname: \"Feature2D.ORB\"\nWTA_K: 2\nedgeThreshold: 31\nfirstLevel: 0\nnFeatures: 500\nnLevels: 8\npatchSize: 31\nscaleFactor: 1.2000000476837158e+00\nscoreType: 0\n";
- assertEquals(truth, readFile(filename));
+ String actual = readFile(filename);
+ actual = actual.replaceAll("e\\+000", "e+00"); // NOTE: workaround for different platforms double representation
+ assertEquals(truth, actual);
}
}
extractor.write(filename);
String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.SIFT</name>\n<contrastThreshold>4.0000000000000001e-02</contrastThreshold>\n<edgeThreshold>10.</edgeThreshold>\n<nFeatures>0</nFeatures>\n<nOctaveLayers>3</nOctaveLayers>\n<sigma>1.6000000000000001e+00</sigma>\n</opencv_storage>\n";
- assertEquals(truth, readFile(filename));
+ String actual = readFile(filename);
+ actual = actual.replaceAll("e([+-])0(\\d\\d)", "e$1$2"); // NOTE: workaround for different platforms double representation
+ assertEquals(truth, actual);
}
public void testWriteYml() {
extractor.write(filename);
String truth = "%YAML:1.0\nname: \"Feature2D.SIFT\"\ncontrastThreshold: 4.0000000000000001e-02\nedgeThreshold: 10.\nnFeatures: 0\nnOctaveLayers: 3\nsigma: 1.6000000000000001e+00\n";
- assertEquals(truth, readFile(filename));
+ String actual = readFile(filename);
+ actual = actual.replaceAll("e([+-])0(\\d\\d)", "e$1$2"); // NOTE: workaround for different platforms double representation
+ assertEquals(truth, actual);
}
}
package org.opencv.test;
+import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
+import java.io.FileReader;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
protected void setUp() throws Exception {
super.setUp();
- try {
- System.loadLibrary("opencv_java");
- } catch (SecurityException e) {
- System.out.println(e.toString());
- System.exit(-1);
- } catch (UnsatisfiedLinkError e) {
- System.out.println(e.toString());
- System.exit(-1);
- }
-
- String pwd;
- try {
- pwd = new File(".").getCanonicalPath() + File.separator;
- } catch (IOException e) {
- System.out.println(e);
- return;
- }
-
- OpenCVTestRunner.LENA_PATH = pwd + "res/drawable/lena.jpg";
- OpenCVTestRunner.CHESS_PATH = pwd + "res/drawable/chessboard.jpg";
- OpenCVTestRunner.LBPCASCADE_FRONTALFACE_PATH = pwd + "res/raw/lbpcascade_frontalface.xml";
-
- assert(new File(OpenCVTestRunner.LENA_PATH).exists());
- assert(new File(OpenCVTestRunner.CHESS_PATH).exists());
- assert(new File(OpenCVTestRunner.LBPCASCADE_FRONTALFACE_PATH).exists());
+ try {
+ System.loadLibrary("opencv_java");
+ } catch (SecurityException e) {
+ System.out.println(e.toString());
+ System.exit(-1);
+ } catch (UnsatisfiedLinkError e) {
+ System.out.println(e.toString());
+ System.exit(-1);
+ }
+
+ String pwd;
+ try {
+ pwd = new File(".").getCanonicalPath() + File.separator;
+ } catch (IOException e) {
+ System.out.println(e);
+ return;
+ }
+
+ OpenCVTestRunner.LENA_PATH = pwd + "res/drawable/lena.jpg";
+ OpenCVTestRunner.CHESS_PATH = pwd + "res/drawable/chessboard.jpg";
+ OpenCVTestRunner.LBPCASCADE_FRONTALFACE_PATH = pwd + "res/raw/lbpcascade_frontalface.xml";
+
+ assert(new File(OpenCVTestRunner.LENA_PATH).exists());
+ assert(new File(OpenCVTestRunner.CHESS_PATH).exists());
+ assert(new File(OpenCVTestRunner.LBPCASCADE_FRONTALFACE_PATH).exists());
dst = new Mat();
assertTrue(dst.empty());
}
protected static String readFile(String path) {
- FileInputStream stream = null;
try {
- stream = new FileInputStream(new File(path));
- FileChannel fc = stream.getChannel();
- MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0,
- fc.size());
- return Charset.defaultCharset().decode(bb).toString();
+ BufferedReader br = new BufferedReader(new FileReader(path));
+ String line;
+ StringBuffer result = new StringBuffer();
+ while ((line = br.readLine()) != null) {
+ result.append(line);
+ result.append("\n");
+ }
+ return result.toString();
} catch (IOException e) {
OpenCVTestRunner.Log("Failed to read file \"" + path
+ "\". Exception is thrown: " + e);
return null;
- } finally {
- if (stream != null)
- try {
- stream.close();
- } catch (IOException e) {
- OpenCVTestRunner.Log("Exception is thrown: " + e);
- }
}
}