Skip to content

Commit

Permalink
Add JImage
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jun 28, 2021
1 parent 911ec8d commit 149c5ac
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
1 change: 1 addition & 0 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ add_library(
src/main/cpp/FrameProcessorRuntimeManager.cpp
src/main/cpp/FrameProcessorPlugin.cpp
src/main/cpp/CameraView.cpp
src/main/cpp/JImage.cpp
src/main/cpp/JImageProxy.cpp
src/main/cpp/JImageProxyHostObject.cpp
src/main/cpp/JHashMap.cpp
Expand Down
27 changes: 27 additions & 0 deletions android/src/main/cpp/JImage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Created by Marc Rousavy on 28.06.21.
//

#include "JImage.h"

#include <jni.h>
#include <fbjni/fbjni.h>

namespace vision {

using namespace facebook;
using namespace jni;


bool JImage::getIsValid() {
static const auto getNativeContextMethod = getClass()->getMethod<jlong()>("getNativeContext");
try {
getNativeContextMethod(javaClassLocal());
return true;
} catch (...) {
// function throws if the image is not active anymore
return false;
}
}

} // namespace vision
19 changes: 19 additions & 0 deletions android/src/main/cpp/JImage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Created by Marc Rousavy on 28.06.21.
//

#pragma once

#include <jni.h>
#include <fbjni/fbjni.h>

namespace vision {

struct JImage : public facebook::jni::JavaClass<JImage> {
static constexpr auto kJavaDescriptor = "Landroid/media/Image;";

public:
bool getIsValid();
};

} // namespace vision
13 changes: 4 additions & 9 deletions android/src/main/cpp/JImageProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <jni.h>
#include <fbjni/fbjni.h>

#include "JImage.h"

namespace vision {

using namespace facebook;
Expand All @@ -23,17 +25,10 @@ int JImageProxy::getHeight() {
}

bool JImageProxy::getIsValid() {
static const auto getImageMethod = getClass()->getMethod<jobject()>("getImage");
static const auto getImageMethod = getClass()->getMethod<JImage::javaobject()>("getImage");
auto image = getImageMethod(javaClassLocal());

static const auto getHardwareBufferMethod = findClassLocal("android/media/Image")->getMethod<jobject()>("getHardwareBuffer");
try {
getHardwareBufferMethod(image.get());
return true;
} catch (...) {
// function throws if the image is not active anymore
return false;
}
return image->getIsValid();
}

int JImageProxy::getPlaneCount() {
Expand Down

0 comments on commit 149c5ac

Please sign in to comment.