VideoCaptureTest.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package org.opencv.test.videoio;
  2. import java.util.List;
  3. import java.io.File;
  4. import java.io.RandomAccessFile;
  5. import java.io.IOException;
  6. import java.io.FileNotFoundException;
  7. import org.opencv.core.Mat;
  8. import org.opencv.core.Size;
  9. import org.opencv.core.MatOfInt;
  10. import org.opencv.videoio.Videoio;
  11. import org.opencv.videoio.VideoCapture;
  12. import org.opencv.videoio.IStreamReader;
  13. import org.opencv.test.OpenCVTestCase;
  14. public class VideoCaptureTest extends OpenCVTestCase {
  15. private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
  16. private VideoCapture capture;
  17. private boolean isOpened;
  18. private boolean isSucceed;
  19. private File testDataPath;
  20. @Override
  21. protected void setUp() throws Exception {
  22. super.setUp();
  23. capture = null;
  24. isSucceed = false;
  25. isOpened = false;
  26. String envTestDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
  27. if(envTestDataPath == null) throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");
  28. testDataPath = new File(envTestDataPath);
  29. }
  30. public void testGrab() {
  31. capture = new VideoCapture();
  32. isSucceed = capture.grab();
  33. assertFalse(isSucceed);
  34. }
  35. public void testIsOpened() {
  36. capture = new VideoCapture();
  37. assertFalse(capture.isOpened());
  38. }
  39. public void testDefaultConstructor() {
  40. capture = new VideoCapture();
  41. assertNotNull(capture);
  42. assertFalse(capture.isOpened());
  43. }
  44. public void testConstructorWithFilename() {
  45. capture = new VideoCapture("some_file.avi");
  46. assertNotNull(capture);
  47. }
  48. public void testConstructorWithFilenameAndExplicitlySpecifiedAPI() {
  49. capture = new VideoCapture("some_file.avi", Videoio.CAP_ANY);
  50. assertNotNull(capture);
  51. }
  52. public void testConstructorWithIndex() {
  53. capture = new VideoCapture(0);
  54. assertNotNull(capture);
  55. }
  56. public void testConstructorWithIndexAndExplicitlySpecifiedAPI() {
  57. capture = new VideoCapture(0, Videoio.CAP_ANY);
  58. assertNotNull(capture);
  59. }
  60. public void testConstructorStream() throws FileNotFoundException {
  61. // Check backend is available
  62. Integer apiPref = Videoio.CAP_ANY;
  63. for (Integer backend : Videoio.getStreamBufferedBackends())
  64. {
  65. if (!Videoio.hasBackend(backend))
  66. continue;
  67. if (!Videoio.isBackendBuiltIn(backend))
  68. {
  69. int[] abi = new int[1], api = new int[1];
  70. Videoio.getStreamBufferedBackendPluginVersion(backend, abi, api);
  71. if (abi[0] < 1 || (abi[0] == 1 && api[0] < 2))
  72. continue;
  73. }
  74. apiPref = backend;
  75. break;
  76. }
  77. if (apiPref == Videoio.CAP_ANY)
  78. {
  79. throw new TestSkipException();
  80. }
  81. RandomAccessFile f = new RandomAccessFile(new File(testDataPath, "cv/video/768x576.avi"), "r");
  82. IStreamReader stream = new IStreamReader()
  83. {
  84. @Override
  85. public long read(byte[] buffer, long size)
  86. {
  87. assertEquals(buffer.length, size);
  88. try
  89. {
  90. return Math.max(f.read(buffer), 0);
  91. }
  92. catch (IOException e)
  93. {
  94. System.out.println(e.getMessage());
  95. return 0;
  96. }
  97. }
  98. @Override
  99. public long seek(long offset, int origin)
  100. {
  101. try
  102. {
  103. if (origin == 0)
  104. f.seek(offset);
  105. else if (origin == 1)
  106. f.seek(f.getFilePointer() + offset);
  107. else if (origin == 2)
  108. f.seek(f.length() + offset);
  109. return f.getFilePointer();
  110. }
  111. catch (IOException e)
  112. {
  113. System.out.println(e.getMessage());
  114. return 0;
  115. }
  116. }
  117. };
  118. capture = new VideoCapture(stream, apiPref, new MatOfInt());
  119. assertNotNull(capture);
  120. assertTrue(capture.isOpened());
  121. Mat frame = new Mat();
  122. assertTrue(capture.read(frame));
  123. assertEquals(frame.rows(), 576);
  124. assertEquals(frame.cols(), 768);
  125. }
  126. }