feat: Add console
logging support for Frame Processors (#297)
* Try to log to console via runOnJS * Call `console.log` * Create custom `VisionCameraScheduler` * Fix scheduler call * Call with this * Fix console setting * Move J---- to `java-bindings` * c++ style * Android: 1/2 Create custom Scheduler * Android: 2/2 Use custom Scheduler * Don't use `runOnJS`, use `__callAsync` directly
This commit is contained in:
20
android/src/main/cpp/java-bindings/JArrayList.h
Normal file
20
android/src/main/cpp/java-bindings/JArrayList.h
Normal file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by Marc Rousavy on 24.06.21.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <jni.h>
|
||||
#include <fbjni/fbjni.h>
|
||||
|
||||
namespace vision {
|
||||
|
||||
using namespace facebook;
|
||||
using namespace jni;
|
||||
|
||||
// TODO: Remove when fbjni 0.2.3 releases.
|
||||
struct JArrayList : public JavaClass<JArrayList> {
|
||||
static constexpr auto kJavaDescriptor = "Ljava/util/ArrayList;";
|
||||
};
|
||||
|
||||
} // namespace vision
|
20
android/src/main/cpp/java-bindings/JHashMap.cpp
Normal file
20
android/src/main/cpp/java-bindings/JHashMap.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by Marc Rousavy on 25.06.21.
|
||||
//
|
||||
|
||||
#include "JHashMap.h"
|
||||
|
||||
#include <jni.h>
|
||||
#include <fbjni/fbjni.h>
|
||||
|
||||
|
||||
namespace facebook {
|
||||
namespace jni {
|
||||
|
||||
template <typename K, typename V>
|
||||
local_ref<JHashMap<K, V>> JHashMap<K, V>::create() {
|
||||
return JHashMap<K, V>::newInstance();
|
||||
}
|
||||
|
||||
} // namespace jni
|
||||
} // namespace facebook
|
23
android/src/main/cpp/java-bindings/JHashMap.h
Normal file
23
android/src/main/cpp/java-bindings/JHashMap.h
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Created by Marc Rousavy on 25.06.21.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <jni.h>
|
||||
#include <fbjni/fbjni.h>
|
||||
|
||||
|
||||
namespace facebook {
|
||||
namespace jni {
|
||||
|
||||
// TODO: Remove when fbjni 0.2.3 releases.
|
||||
template <typename K = jobject, typename V = jobject>
|
||||
struct JHashMap : JavaClass<JHashMap<K, V>, JMap<K, V>> {
|
||||
constexpr static auto kJavaDescriptor = "Ljava/util/HashMap;";
|
||||
|
||||
static local_ref<JHashMap<K, V>> create();
|
||||
};
|
||||
|
||||
} // namespace jni
|
||||
} // namespace facebook
|
51
android/src/main/cpp/java-bindings/JImageProxy.cpp
Normal file
51
android/src/main/cpp/java-bindings/JImageProxy.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Created by Marc Rousavy on 22.06.21.
|
||||
//
|
||||
|
||||
#include "JImageProxy.h"
|
||||
|
||||
#include <jni.h>
|
||||
#include <fbjni/fbjni.h>
|
||||
|
||||
namespace vision {
|
||||
|
||||
using namespace facebook;
|
||||
using namespace jni;
|
||||
|
||||
int JImageProxy::getWidth() {
|
||||
static const auto getWidthMethod = getClass()->getMethod<jint()>("getWidth");
|
||||
return getWidthMethod(self());
|
||||
}
|
||||
|
||||
int JImageProxy::getHeight() {
|
||||
static const auto getWidthMethod = getClass()->getMethod<jint()>("getHeight");
|
||||
return getWidthMethod(self());
|
||||
}
|
||||
|
||||
bool JImageProxy::getIsValid() {
|
||||
static const auto utilsClass = findClassStatic("com/mrousavy/camera/frameprocessor/ImageProxyUtils");
|
||||
static const auto isImageProxyValidMethod = utilsClass->getStaticMethod<jboolean(JImageProxy::javaobject)>("isImageProxyValid");
|
||||
return isImageProxyValidMethod(utilsClass, self());
|
||||
}
|
||||
|
||||
int JImageProxy::getPlaneCount() {
|
||||
static const auto getPlanesMethod = getClass()->getMethod<JArrayClass<jobject>()>("getPlanes");
|
||||
auto planes = getPlanesMethod(self());
|
||||
return planes->size();
|
||||
}
|
||||
|
||||
int JImageProxy::getBytesPerRow() {
|
||||
static const auto getPlanesMethod = getClass()->getMethod<JArrayClass<jobject>()>("getPlanes");
|
||||
auto planes = getPlanesMethod(self());
|
||||
auto firstPlane = planes->getElement(0);
|
||||
|
||||
static const auto getRowStrideMethod = findClassLocal("android/media/Image$PlaneProxy")->getMethod<int()>("getRowStride");
|
||||
return getRowStrideMethod(firstPlane.get());
|
||||
}
|
||||
|
||||
void JImageProxy::close() {
|
||||
static const auto closeMethod = getClass()->getMethod<void()>("close");
|
||||
closeMethod(self());
|
||||
}
|
||||
|
||||
} // namespace vision
|
24
android/src/main/cpp/java-bindings/JImageProxy.h
Normal file
24
android/src/main/cpp/java-bindings/JImageProxy.h
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by Marc on 19/06/2021.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <jni.h>
|
||||
#include <fbjni/fbjni.h>
|
||||
|
||||
namespace vision {
|
||||
|
||||
struct JImageProxy : public facebook::jni::JavaClass<JImageProxy> {
|
||||
static constexpr auto kJavaDescriptor = "Landroidx/camera/core/ImageProxy;";
|
||||
|
||||
public:
|
||||
int getWidth();
|
||||
int getHeight();
|
||||
bool getIsValid();
|
||||
int getPlaneCount();
|
||||
int getBytesPerRow();
|
||||
void close();
|
||||
};
|
||||
|
||||
} // namespace vision
|
92
android/src/main/cpp/java-bindings/JImageProxyHostObject.cpp
Normal file
92
android/src/main/cpp/java-bindings/JImageProxyHostObject.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// Created by Marc on 19/06/2021.
|
||||
//
|
||||
|
||||
#include "JImageProxyHostObject.h"
|
||||
#include <android/log.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace vision {
|
||||
|
||||
std::vector<jsi::PropNameID> JImageProxyHostObject::getPropertyNames(jsi::Runtime& rt) {
|
||||
std::vector<jsi::PropNameID> result;
|
||||
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("toString")));
|
||||
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("isValid")));
|
||||
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("width")));
|
||||
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("height")));
|
||||
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("bytesPerRow")));
|
||||
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("planesCount")));
|
||||
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("close")));
|
||||
return result;
|
||||
}
|
||||
|
||||
jsi::Value JImageProxyHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& propNameId) {
|
||||
auto name = propNameId.utf8(runtime);
|
||||
|
||||
if (name == "toString") {
|
||||
auto toString = [this] (jsi::Runtime& runtime, const jsi::Value&, const jsi::Value*, size_t) -> jsi::Value {
|
||||
if (!this->frame) {
|
||||
return jsi::String::createFromUtf8(runtime, "[closed frame]");
|
||||
}
|
||||
auto width = this->frame->getWidth();
|
||||
auto height = this->frame->getHeight();
|
||||
auto str = std::to_string(width) + " x " + std::to_string(height) + " Frame";
|
||||
return jsi::String::createFromUtf8(runtime, str);
|
||||
};
|
||||
return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "toString"), 0, toString);
|
||||
}
|
||||
if (name == "close") {
|
||||
auto close = [this] (jsi::Runtime& runtime, const jsi::Value&, const jsi::Value*, size_t) -> jsi::Value {
|
||||
if (!this->frame) {
|
||||
throw jsi::JSError(runtime, "Trying to close an already closed frame! Did you call frame.close() twice?");
|
||||
}
|
||||
this->close();
|
||||
return jsi::Value::undefined();
|
||||
};
|
||||
return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "close"), 0, close);
|
||||
}
|
||||
|
||||
if (name == "isValid") {
|
||||
return jsi::Value(this->frame && this->frame->getIsValid());
|
||||
}
|
||||
if (name == "width") {
|
||||
this->assertIsFrameStrong(runtime, name);
|
||||
return jsi::Value(this->frame->getWidth());
|
||||
}
|
||||
if (name == "height") {
|
||||
this->assertIsFrameStrong(runtime, name);
|
||||
return jsi::Value(this->frame->getHeight());
|
||||
}
|
||||
if (name == "bytesPerRow") {
|
||||
this->assertIsFrameStrong(runtime, name);
|
||||
return jsi::Value(this->frame->getBytesPerRow());
|
||||
}
|
||||
if (name == "planesCount") {
|
||||
this->assertIsFrameStrong(runtime, name);
|
||||
return jsi::Value(this->frame->getPlaneCount());
|
||||
}
|
||||
|
||||
return jsi::Value::undefined();
|
||||
}
|
||||
|
||||
void JImageProxyHostObject::assertIsFrameStrong(jsi::Runtime& runtime, const std::string& accessedPropName) const {
|
||||
if (!this->frame) {
|
||||
auto message = "Cannot get `" + accessedPropName + "`, frame is already closed!";
|
||||
throw jsi::JSError(runtime, message.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
JImageProxyHostObject::~JImageProxyHostObject() {
|
||||
this->close();
|
||||
}
|
||||
|
||||
void JImageProxyHostObject::close() {
|
||||
if (this->frame) {
|
||||
this->frame->close();
|
||||
this->frame.release();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace vision
|
39
android/src/main/cpp/java-bindings/JImageProxyHostObject.h
Normal file
39
android/src/main/cpp/java-bindings/JImageProxyHostObject.h
Normal file
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// Created by Marc on 19/06/2021.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <jsi/jsi.h>
|
||||
#include <jni.h>
|
||||
#include <fbjni/fbjni.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "JImageProxy.h"
|
||||
|
||||
namespace vision {
|
||||
|
||||
using namespace facebook;
|
||||
|
||||
class JSI_EXPORT JImageProxyHostObject : public jsi::HostObject {
|
||||
public:
|
||||
explicit JImageProxyHostObject(jni::local_ref<JImageProxy::javaobject> image): frame(image) {}
|
||||
~JImageProxyHostObject();
|
||||
|
||||
public:
|
||||
jsi::Value get(jsi::Runtime &, const jsi::PropNameID &name) override;
|
||||
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;
|
||||
|
||||
void close();
|
||||
|
||||
public:
|
||||
jni::local_ref<JImageProxy> frame;
|
||||
|
||||
private:
|
||||
static auto constexpr TAG = "VisionCamera";
|
||||
|
||||
void assertIsFrameStrong(jsi::Runtime& runtime, const std::string& accessedPropName) const; // NOLINT(runtime/references)
|
||||
};
|
||||
|
||||
} // namespace vision
|
19
android/src/main/cpp/java-bindings/JReadableArray.h
Normal file
19
android/src/main/cpp/java-bindings/JReadableArray.h
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by Marc Rousavy on 24.06.21.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <jni.h>
|
||||
#include <fbjni/fbjni.h>
|
||||
|
||||
namespace vision {
|
||||
|
||||
using namespace facebook;
|
||||
using namespace jni;
|
||||
|
||||
struct JReadableArray : public JavaClass<JReadableArray> {
|
||||
static constexpr auto kJavaDescriptor = "Lcom/facebook/react/bridge/ReadableArray;";
|
||||
};
|
||||
|
||||
} // namespace vision
|
19
android/src/main/cpp/java-bindings/JReadableMap.h
Normal file
19
android/src/main/cpp/java-bindings/JReadableMap.h
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by Marc Rousavy on 24.06.21.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <jni.h>
|
||||
#include <fbjni/fbjni.h>
|
||||
|
||||
namespace vision {
|
||||
|
||||
using namespace facebook;
|
||||
using namespace jni;
|
||||
|
||||
struct JReadableMap : public JavaClass<JReadableMap> {
|
||||
static constexpr auto kJavaDescriptor = "Lcom/facebook/react/bridge/ReadableMap;";
|
||||
};
|
||||
|
||||
} // namespace vision
|
Reference in New Issue
Block a user