Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Java VideoCapture buffered stream constructor#27284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
asmorkalov merged 17 commits intoopencv:4.xfromdkurt:java_video_capture_read
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from8 commits
Commits
Show all changes
17 commits
Select commitHold shift + click to select a range
1f1cd89
initial commit
dkurtMay 2, 2025
17b2886
dummy java wrapper
dkurtMay 2, 2025
cb2e2a9
Keep jclass in wrapper
dkurtMay 3, 2025
66cc8c5
working test
dkurtMay 5, 2025
8a44f4f
Revert unused files
dkurtMay 5, 2025
a52ebc9
Remove size argument from read
dkurtMay 5, 2025
b6faec7
Check backend available before test
dkurtMay 5, 2025
4822844
Try print exception
dkurtMay 6, 2025
c499ed2
Revert "Try print exception"
dkurtMay 6, 2025
faddefc
Use NewLocalRef for Java stream object
dkurtMay 6, 2025
eed126c
Wrap native seek and read
dkurtMay 7, 2025
a603bce
Try create global ref
dkurtMay 7, 2025
79afad9
Fix long long in Python
dkurtMay 7, 2025
b9b0310
Default IStreamReader constructor
dkurtMay 12, 2025
feee794
Use JavaVM
dkurtMay 12, 2025
f9cb6d4
Add AttachCurrentThread in case of JNI_EDETACHED error
dkurtMay 13, 2025
c3be053
Add constructor note
dkurtMay 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -264,6 +264,9 @@
export_name="ExtractMetaCallback",
required_modules=("gapi",)
),

# videoio, optional
AliasTypeNode.ref_("IStreamReader", "io.BufferedIOBase"),
)

PREDEFINED_TYPES = dict(
Expand Down
2 changes: 1 addition & 1 deletionmodules/videoio/include/opencv2/videoio.hpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -721,7 +721,7 @@ enum VideoCaptureOBSensorProperties{

/** @brief Read data stream interface
*/
classCV_EXPORTS_W IStreamReader
classCV_EXPORTS IStreamReader
{
public:
virtual ~IStreamReader();
Expand Down
1 change: 1 addition & 0 deletionsmodules/videoio/misc/java/filelist_common
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
misc/java/src/cpp/videoio_converters.hpp
20 changes: 20 additions & 0 deletionsmodules/videoio/misc/java/gen_dict.json
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
{
"type_dict": {
"Ptr_IStreamReader": {
"j_type": "IStreamReader",
"jn_type": "IStreamReader",
"jni_name": "n_%(n)s",
"jni_type": "jclass",
"jni_var": "auto n_%(n)s = makePtr<JavaStreamReader>(env, source)",
"j_import": "org.opencv.videoio.IStreamReader"
},
"vector_VideoCaptureAPIs": {
"j_type": "List<Integer>",
"jn_type": "List<Integer>",
"jni_type": "jobject",
"jni_var": "std::vector< cv::VideoCaptureAPIs > %(n)s",
"suffix": "Ljava_util_List",
"v_type": "vector_VideoCaptureAPIs"
}
}
}
46 changes: 46 additions & 0 deletionsmodules/videoio/misc/java/src/cpp/videoio_converters.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
#include "videoio_converters.hpp"

JavaStreamReader::JavaStreamReader(JNIEnv* _env, jclass _obj) : env(_env), obj(_obj) {}

long long JavaStreamReader::read(char* buffer, long long size)
{
jmethodID m_read = env->GetMethodID(env->GetObjectClass(obj), "read", "([B)J");
if (!m_read)
return 0;
jbyteArray jBuffer = env->NewByteArray(static_cast<jsize>(size));
if (!jBuffer)
return 0;
jlong res = env->CallIntMethod(obj, m_read, jBuffer);
env->GetByteArrayRegion(jBuffer, 0, static_cast<jsize>(size), reinterpret_cast<jbyte*>(buffer));
env->DeleteLocalRef(jBuffer);
return res;
}

long long JavaStreamReader::seek(long long offset, int way)
{
jmethodID m_seek = env->GetMethodID(env->GetObjectClass(obj), "seek", "(JJ)J");
if (!m_seek)
return 0;
jlong res = env->CallIntMethod(obj, m_seek, offset, way);
return res;
}

// Same as dnn::vector_Target_to_List
jobject vector_VideoCaptureAPIs_to_List(JNIEnv* env, std::vector<cv::VideoCaptureAPIs>& vs)
{
static jclass juArrayList = ARRAYLIST(env);
static jmethodID m_create = CONSTRUCTOR(env, juArrayList);
jmethodID m_add = LIST_ADD(env, juArrayList);

static jclass jInteger = env->FindClass("java/lang/Integer");
static jmethodID m_create_Integer = env->GetMethodID(jInteger, "<init>", "(I)V");

jobject result = env->NewObject(juArrayList, m_create, vs.size());
for (size_t i = 0; i < vs.size(); ++i)
{
jobject element = env->NewObject(jInteger, m_create_Integer, vs[i]);
env->CallBooleanMethod(result, m_add, element);
env->DeleteLocalRef(element);
}
return result;
}
23 changes: 23 additions & 0 deletionsmodules/videoio/misc/java/src/cpp/videoio_converters.hpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
#ifndef VIDEOIO_CONVERTERS_HPP
#define VIDEOIO_CONVERTERS_HPP

#include <jni.h>
#include "opencv_java.hpp"
#include "opencv2/core.hpp"
#include "opencv2/videoio/videoio.hpp"

class JavaStreamReader : public cv::IStreamReader
{
public:
JavaStreamReader(JNIEnv* env, jclass obj);
long long read(char* buffer, long long size) CV_OVERRIDE;
long long seek(long long offset, int way) CV_OVERRIDE;

private:
JNIEnv* env;
jclass obj;
};

jobject vector_VideoCaptureAPIs_to_List(JNIEnv* env, std::vector<cv::VideoCaptureAPIs>& vs);

#endif
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
package org.opencv.videoio;

public interface IStreamReader {
public long read(byte[] buffer);
public long seek(long offset, long origin);
}
85 changes: 84 additions & 1 deletionmodules/videoio/misc/java/test/VideoCaptureTest.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
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;
isTestCaseEnabled = false;
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() {
Expand DownExpand Up@@ -61,4 +75,73 @@ public void testConstructorWithIndexAndExplicitlySpecifiedAPI() {
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)
{
try
{
return f.read(buffer);
}
catch (IOException e)
{
System.out.println(e.getMessage());
return 0;
}
}

@Override
public long seek(long offset, long origin)
{
try
{
if (origin == 0)
{
f.seek(offset);
return offset;
}
else
return 0;
}
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);
}
}
8 changes: 4 additions & 4 deletionsmodules/videoio/src/backend_plugin.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -472,8 +472,8 @@ class PluginCapture : public cv::IVideoCapture
auto is = reinterpret_cast<IStreamReader*>(opaque);
try {
return is->read(buffer, size);
} catch (...) {
CV_LOG_WARNING(NULL, "IStreamReader::read(" << size << ") failed");
} catch (std::exception& e) {
CV_LOG_WARNING(NULL, "IStreamReader::read(" << size << ") failed: " << e.what());
return 0;
}
},
Expand All@@ -482,8 +482,8 @@ class PluginCapture : public cv::IVideoCapture
auto is = reinterpret_cast<IStreamReader*>(opaque);
try {
return is->seek(offset, way);
} catch (...) {
CV_LOG_WARNING(NULL, "IStreamReader::seek(" << offset << ", way=" << way << ") failed");
} catch (std::exception& e) {
CV_LOG_WARNING(NULL, "IStreamReader::seek(" << offset << ", way=" << way << ") failed: " << e.what());
return -1;
}
}, c_params, n_params, &capture))
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp