diff --git a/mediapipe/java/com/google/mediapipe/framework/PacketGetter.java b/mediapipe/java/com/google/mediapipe/framework/PacketGetter.java index 92cf723e..5a13e9c0 100644 --- a/mediapipe/java/com/google/mediapipe/framework/PacketGetter.java +++ b/mediapipe/java/com/google/mediapipe/framework/PacketGetter.java @@ -24,6 +24,7 @@ import com.google.protobuf.Parser; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; +import javax.annotation.Nullable; /** * Converts the {@link Packet} to java accessible data types. @@ -187,6 +188,10 @@ public final class PacketGetter { return nativeGetImageHeight(packet.getNativeHandle()); } + public static int getImageNumChannels(final Packet packet) { + return nativeGetImageNumChannels(packet.getNativeHandle()); + } + /** * Returns the native image buffer in ByteBuffer. It assumes the output buffer stores pixels * contiguously. It returns false if this assumption does not hold. @@ -199,6 +204,18 @@ public final class PacketGetter { return nativeGetImageData(packet.getNativeHandle(), buffer); } + /** + * Returns a read-only view of the native image buffer as a ByteBuffer. As this method does not + * copy the data, the result only remains valid while the backing MediaPipe image is on the stack. + * The image must store contiguous pixels, otherwise the method returns {@code null}. + * + *
Note: this function does not assume the pixel format.
+ */
+ @Nullable
+ public static ByteBuffer getImageDataDirectly(final Packet packet) {
+ return nativeGetImageDataDirect(packet.getNativeHandle()).asReadOnlyBuffer();
+ }
+
/** Returns the size of Image list. This helps to determine size of allocated ByteBuffer array. */
public static int getImageListSize(final Packet packet) {
return nativeGetImageListSize(packet.getNativeHandle());
@@ -384,8 +401,12 @@ public final class PacketGetter {
private static native int nativeGetImageHeight(long nativePacketHandle);
+ private static native int nativeGetImageNumChannels(long nativePacketHandle);
+
private static native boolean nativeGetImageData(long nativePacketHandle, ByteBuffer buffer);
+ private static native ByteBuffer nativeGetImageDataDirect(long nativePacketHandle);
+
private static native int nativeGetImageListSize(long nativePacketHandle);
private static native boolean nativeGetImageList(
diff --git a/mediapipe/java/com/google/mediapipe/framework/jni/packet_getter_jni.cc b/mediapipe/java/com/google/mediapipe/framework/jni/packet_getter_jni.cc
index 234209b8..d5bd773f 100644
--- a/mediapipe/java/com/google/mediapipe/framework/jni/packet_getter_jni.cc
+++ b/mediapipe/java/com/google/mediapipe/framework/jni/packet_getter_jni.cc
@@ -334,6 +334,20 @@ JNIEXPORT jint JNICALL PACKET_GETTER_METHOD(nativeGetImageHeight)(
return image.Height();
}
+JNIEXPORT jint JNICALL PACKET_GETTER_METHOD(nativeGetImageNumChannels)(
+ JNIEnv* env, jobject thiz, jlong packet) {
+ mediapipe::Packet mediapipe_packet =
+ mediapipe::android::Graph::GetPacketFromHandle(packet);
+ const bool is_image =
+ mediapipe_packet.ValidateAsType