opencv/modules/videoio/misc/java/test/VideoCaptureTest.java
Dmitry Kurtaev 809090f203
Merge pull request #27421 from dkurt:dkurt-patch-1
Cover all seek directions in VideoCapture Java test #27421

### Pull Request Readiness Checklist

required for https://github.com/opencv/opencv/pull/27370

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
2025-06-10 14:51:32 +03:00

149 lines
4.4 KiB
Java

package org.opencv.test.videoio;
import java.util.List;
import java.io.File;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.io.FileNotFoundException;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.core.MatOfInt;
import org.opencv.videoio.Videoio;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.IStreamReader;
import org.opencv.test.OpenCVTestCase;
public class VideoCaptureTest extends OpenCVTestCase {
private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
private VideoCapture capture;
private boolean isOpened;
private boolean isSucceed;
private File testDataPath;
@Override
protected void setUp() throws Exception {
super.setUp();
capture = null;
isSucceed = false;
isOpened = false;
String envTestDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
if(envTestDataPath == null) throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");
testDataPath = new File(envTestDataPath);
}
public void testGrab() {
capture = new VideoCapture();
isSucceed = capture.grab();
assertFalse(isSucceed);
}
public void testIsOpened() {
capture = new VideoCapture();
assertFalse(capture.isOpened());
}
public void testDefaultConstructor() {
capture = new VideoCapture();
assertNotNull(capture);
assertFalse(capture.isOpened());
}
public void testConstructorWithFilename() {
capture = new VideoCapture("some_file.avi");
assertNotNull(capture);
}
public void testConstructorWithFilenameAndExplicitlySpecifiedAPI() {
capture = new VideoCapture("some_file.avi", Videoio.CAP_ANY);
assertNotNull(capture);
}
public void testConstructorWithIndex() {
capture = new VideoCapture(0);
assertNotNull(capture);
}
public void testConstructorWithIndexAndExplicitlySpecifiedAPI() {
capture = new VideoCapture(0, Videoio.CAP_ANY);
assertNotNull(capture);
}
public void testConstructorStream() throws FileNotFoundException {
// Check backend is available
Integer apiPref = Videoio.CAP_ANY;
for (Integer backend : Videoio.getStreamBufferedBackends())
{
if (!Videoio.hasBackend(backend))
continue;
if (!Videoio.isBackendBuiltIn(backend))
{
int[] abi = new int[1], api = new int[1];
Videoio.getStreamBufferedBackendPluginVersion(backend, abi, api);
if (abi[0] < 1 || (abi[0] == 1 && api[0] < 2))
continue;
}
apiPref = backend;
break;
}
if (apiPref == Videoio.CAP_ANY)
{
throw new TestSkipException();
}
RandomAccessFile f = new RandomAccessFile(new File(testDataPath, "cv/video/768x576.avi"), "r");
IStreamReader stream = new IStreamReader()
{
@Override
public long read(byte[] buffer, long size)
{
assertEquals(buffer.length, size);
try
{
return Math.max(f.read(buffer), 0);
}
catch (IOException e)
{
System.out.println(e.getMessage());
return 0;
}
}
@Override
public long seek(long offset, int origin)
{
try
{
if (origin == 0)
f.seek(offset);
else if (origin == 1)
f.seek(f.getFilePointer() + offset);
else if (origin == 2)
f.seek(f.length() + offset);
return f.getFilePointer();
}
catch (IOException e)
{
System.out.println(e.getMessage());
return 0;
}
}
};
capture = new VideoCapture(stream, apiPref, new MatOfInt());
assertNotNull(capture);
assertTrue(capture.isOpened());
Mat frame = new Mat();
assertTrue(capture.read(frame));
assertEquals(frame.rows(), 576);
assertEquals(frame.cols(), 768);
}
}