@@ -91,6 +91,24 @@ extern "C" void Java_com_pixpark_gpupixel_GPUPixel_nativeSourceCameraSetFrame(
91
91
env->ReleaseIntArrayElements (jdata, data, 0 );
92
92
};
93
93
94
+ extern " C" void Java_com_pixpark_gpupixel_GPUPixel_nativeSourceCameraSetFrameByBuffer (
95
+ JNIEnv* env,
96
+ jclass,
97
+ jlong classId,
98
+ jint width,
99
+ jint height,
100
+ jobject buffer,
101
+ jint rotation) {
102
+ // Get the pointer to the ByteBuffer data
103
+ void * pixels = env->GetDirectBufferAddress (buffer);
104
+ if (pixels == nullptr ) {
105
+ // Handle error: Direct buffer address could not be obtained
106
+ return ;
107
+ }
108
+ ((SourceCamera*)classId)
109
+ ->setFrameData (width, height, pixels, (RotationMode)rotation);
110
+ };
111
+
94
112
extern " C" jlong Java_com_pixpark_gpupixel_GPUPixel_nativeSourceRawInputNew (
95
113
JNIEnv* env,
96
114
jclass) {
@@ -189,22 +207,44 @@ extern "C" jlong Java_com_pixpark_gpupixel_GPUPixel_nativeSourceAddTarget(
189
207
extern " C" jlong Java_com_pixpark_gpupixel_GPUPixel_nativeSourceAddTargetOutputCallback (
190
208
JNIEnv* env,
191
209
jclass,
192
- jlong classId) {
210
+ jlong classId,
211
+ jobject src) {
212
+
213
+ jobject globalSourceRef = env->NewGlobalRef (src);
214
+
215
+ jclass cls = env->GetObjectClass (globalSourceRef);
216
+ jmethodID mid = env->GetMethodID (cls, " onRawOutput" , " ([BIII)V" );
217
+
218
+ if (!mid) return 0 ;
219
+
193
220
Source* source = (Source*)classId;
194
- // std::shared_ptr<Target> target =
195
- // isFilter ? std::shared_ptr<Target>((Filter*)targetClassId)
196
- // : std::shared_ptr<Target>((Target*)targetClassId);
221
+
197
222
std::shared_ptr<Target> target;
198
223
199
224
std::shared_ptr<TargetRawDataOutput> output = TargetRawDataOutput::create ();
200
- output->setPixelsCallbck ([&](const uint8_t * data, int width, int height, int64_t ts) {
201
- for (int i = 0 ; i < width * height; ++i) {
202
- int a = data[i * 4 + 3 ]; // Alpha
203
- int r = data[i * 4 + 0 ]; // Red
204
- int g = data[i * 4 + 1 ]; // Green
205
- int b = data[i * 4 + 2 ]; // Blue
206
- __android_log_print (ANDROID_LOG_INFO, " OpenGL" , " value: %d %d %d %d" , a, r, g, b);
207
- }
225
+
226
+ JavaVM* jvm;
227
+ env->GetJavaVM (&jvm);
228
+
229
+ output->setPixelsCallbck ([=](const uint8_t * data, int width, int height, int64_t ts) {
230
+ size_t frame_size = width * height * 4 ;
231
+
232
+ JNIEnv* myNewEnv;
233
+ JavaVMAttachArgs args;
234
+ args.version = JNI_VERSION_1_6; // choose your JNI version
235
+ args.name = NULL ; // you might want to give the java thread a name
236
+ args.group = NULL ; // you might want to assign the java thread to a ThreadGroup
237
+ jvm->AttachCurrentThread (reinterpret_cast <JNIEnv **>((void **) &myNewEnv), &args);
238
+
239
+ jbyte* by = (jbyte*)data;
240
+
241
+ jbyteArray yArray = myNewEnv->NewByteArray (frame_size);
242
+
243
+ myNewEnv->SetByteArrayRegion (yArray, 0 , frame_size, by);
244
+
245
+ myNewEnv->CallVoidMethod (globalSourceRef, mid, yArray, width, height, ts);
246
+
247
+ myNewEnv->DeleteLocalRef (yArray);
208
248
});
209
249
target = output;
210
250
0 commit comments