Skip to content

Commit 0a6c06e

Browse files
committed
update .
1 parent c37f5e9 commit 0a6c06e

File tree

5 files changed

+74
-18
lines changed

5 files changed

+74
-18
lines changed

src/android/java/app/src/main/java/com/pixpark/GPUPixelApp/MainActivity.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import android.util.Log;
1414
import android.view.SurfaceHolder;
1515
import android.view.WindowManager;
16-
import android.widget.SeekBar;
1716
import android.widget.Toast;
1817

1918
import com.pixpark.GPUPixelApp.databinding.ActivityMainBinding;
@@ -168,7 +167,7 @@ public void startCameraFilter() {
168167
sourceCamera.addTarget(lipstickFilter);
169168
lipstickFilter.addTarget(faceReshapFilter);
170169
faceReshapFilter.addTarget(beautyFaceFilter);
171-
beautyFaceFilter.addTargetCallback();
170+
// beautyFaceFilter.addTargetCallback();
172171

173172
sourceCamera.setLandmarkCallbck(new GPUPixel.GPUPixelLandmarkCallback() {
174173
@Override

src/android/java/gpupixel/src/main/java/com/pixpark/gpupixel/GPUPixel.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public class GPUPixel {
2424
public interface GPUPixelLandmarkCallback {
2525
public void onFaceLandmark(float[] landmarks);
2626
}
27+
28+
public interface RawOutputCallback {
29+
public void onRawOutput(byte[] data, int width, int height, int ts);
30+
}
31+
2732
public static final int NoRotation = 0;
2833
public static final int RotateLeft = 1;
2934
public static final int RotateRight = 2;
@@ -227,7 +232,7 @@ public static void copyAssetsToFiles(Context context, String oldPath, String ne
227232
public static native void nativeSourceCameraDestroy(final long classID);
228233
public static native void nativeSourceCameraFinalize(final long classID);
229234
public static native void nativeSourceCameraSetFrame(final long classID, final int width, final int height, final int[] data, final int rotation);
230-
235+
public static native void nativeSourceCameraSetFrameByBuffer(final long classId, final int width, final int height, final ByteBuffer buffer, final int rotation);
231236
// SourceRawDataInput
232237
public static native long nativeSourceRawInputNew();
233238
public static native void nativeSourceRawInputUploadBytes(final long classID, final int[] pixel, final int width, final int height, final int stride);
@@ -238,7 +243,7 @@ public static void copyAssetsToFiles(Context context, String oldPath, String ne
238243
// Source
239244
public static native long nativeSourceAddFilter(final long targetClassId);
240245
public static native long nativeSourceAddTarget(final long classID, final long targetClassID, final int texID, final boolean isFilter);
241-
public static native long nativeSourceAddTargetOutputCallback(final long classId);
246+
public static native long nativeSourceAddTargetOutputCallback(final long classId, final RawOutputCallback callback);
242247
public static native void nativeSourceRemoveTarget(final long classID, final long targetClassID, final boolean isFilter);
243248
public static native void nativeSourceRemoveAllTargets(final long classID);
244249
public static native boolean nativeSourceProceed(final long classID, final boolean bUpdateTargets);

src/android/java/gpupixel/src/main/java/com/pixpark/gpupixel/GPUPixelSource.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public GPUPixelSource addTarget(GPUPixelTarget target) {
2424
return addTarget(target, -1);
2525
}
2626

27-
public void addTargetCallback() {
27+
public void addTargetCallback(final GPUPixel.RawOutputCallback callback) {
2828
GPUPixel.getInstance().runOnDraw(new Runnable() {
2929
@Override
3030
public void run() {
31-
GPUPixel.nativeSourceAddTargetOutputCallback(mNativeClassID);
31+
GPUPixel.nativeSourceAddTargetOutputCallback(mNativeClassID, callback);
3232
}
3333
});
3434
}

src/android/java/gpupixel/src/main/java/com/pixpark/gpupixel/GPUPixelSourceCamera.java

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import android.view.SurfaceHolder;
1919
import android.view.WindowManager;
2020
import java.io.IOException;
21+
import java.nio.ByteBuffer;
2122
import java.nio.IntBuffer;
2223

2324
public class GPUPixelSourceCamera extends GPUPixelSource implements Camera.PreviewCallback {
@@ -73,6 +74,17 @@ public void run() {
7374
proceed(true, true);
7475
}
7576

77+
public void setFrameByBuffer(final ByteBuffer buffer, final int width, final int height) {
78+
GPUPixel.getInstance().runOnDraw(new Runnable() {
79+
@Override
80+
public void run() {
81+
GPUPixel.nativeSourceCameraSetFrameByBuffer(mNativeClassID, width, height, buffer, GPUPixel.NoRotation);
82+
}
83+
});
84+
85+
proceed(true, true);
86+
}
87+
7688
@Override
7789
public void onPreviewFrame(final byte[] data, Camera camera) {
7890
final Camera.Size previewSize = camera.getParameters().getPreviewSize();

src/android/jni/jni_gpupixel.cc

+52-12
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ extern "C" void Java_com_pixpark_gpupixel_GPUPixel_nativeSourceCameraSetFrame(
9191
env->ReleaseIntArrayElements(jdata, data, 0);
9292
};
9393

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+
94112
extern "C" jlong Java_com_pixpark_gpupixel_GPUPixel_nativeSourceRawInputNew(
95113
JNIEnv* env,
96114
jclass) {
@@ -189,22 +207,44 @@ extern "C" jlong Java_com_pixpark_gpupixel_GPUPixel_nativeSourceAddTarget(
189207
extern "C" jlong Java_com_pixpark_gpupixel_GPUPixel_nativeSourceAddTargetOutputCallback(
190208
JNIEnv* env,
191209
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+
193220
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+
197222
std::shared_ptr<Target> target;
198223

199224
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);
208248
});
209249
target = output;
210250

0 commit comments

Comments
 (0)