From 0d3f2cbd9d318cad5ade5f0b4ebcc9f6ef46b265 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Fri, 20 Aug 2021 16:02:34 +0200 Subject: [PATCH] fix: Fix crash when accessing `planesCount` or `bytesPerRow` (#380) * fix: Fix `bytesPerRow` and `planeCount` crashing * use direct call * Call `getPlanesCount` and `getBytesPerRow` through java * revert camerapage changes * remove removed header import * Update CameraPage.tsx --- .../main/cpp/java-bindings/JImageProxy.cpp | 24 ++++++++++--------- .../src/main/cpp/java-bindings/JImageProxy.h | 2 +- .../java-bindings/JImageProxyHostObject.cpp | 2 +- .../frameprocessor/ImageProxyUtils.java | 13 ++++++++++ 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/android/src/main/cpp/java-bindings/JImageProxy.cpp b/android/src/main/cpp/java-bindings/JImageProxy.cpp index 9e8f32e..b804f0a 100644 --- a/android/src/main/cpp/java-bindings/JImageProxy.cpp +++ b/android/src/main/cpp/java-bindings/JImageProxy.cpp @@ -22,25 +22,27 @@ int JImageProxy::getHeight() { return getWidthMethod(self()); } +alias_ref getUtilsClass() { + static const auto ImageProxyUtilsClass = findClassStatic("com/mrousavy/camera/frameprocessor/ImageProxyUtils"); + return ImageProxyUtilsClass; +} + bool JImageProxy::getIsValid() { - static const auto utilsClass = findClassStatic("com/mrousavy/camera/frameprocessor/ImageProxyUtils"); + auto utilsClass = getUtilsClass(); static const auto isImageProxyValidMethod = utilsClass->getStaticMethod("isImageProxyValid"); return isImageProxyValidMethod(utilsClass, self()); } -int JImageProxy::getPlaneCount() { - static const auto getPlanesMethod = getClass()->getMethod()>("getPlanes"); - auto planes = getPlanesMethod(self()); - return planes->size(); +int JImageProxy::getPlanesCount() { + auto utilsClass = getUtilsClass(); + static const auto getPlanesCountMethod = utilsClass->getStaticMethod("getPlanesCount"); + return getPlanesCountMethod(utilsClass, self()); } int JImageProxy::getBytesPerRow() { - static const auto getPlanesMethod = getClass()->getMethod()>("getPlanes"); - auto planes = getPlanesMethod(self()); - auto firstPlane = planes->getElement(0); - - static const auto getRowStrideMethod = findClassLocal("android/media/Image$PlaneProxy")->getMethod("getRowStride"); - return getRowStrideMethod(firstPlane.get()); + auto utilsClass = getUtilsClass(); + static const auto getBytesPerRowMethod = utilsClass->getStaticMethod("getBytesPerRow"); + return getBytesPerRowMethod(utilsClass, self()); } void JImageProxy::close() { diff --git a/android/src/main/cpp/java-bindings/JImageProxy.h b/android/src/main/cpp/java-bindings/JImageProxy.h index 137165e..cf9fda4 100644 --- a/android/src/main/cpp/java-bindings/JImageProxy.h +++ b/android/src/main/cpp/java-bindings/JImageProxy.h @@ -16,7 +16,7 @@ struct JImageProxy : public facebook::jni::JavaClass { int getWidth(); int getHeight(); bool getIsValid(); - int getPlaneCount(); + int getPlanesCount(); int getBytesPerRow(); void close(); }; diff --git a/android/src/main/cpp/java-bindings/JImageProxyHostObject.cpp b/android/src/main/cpp/java-bindings/JImageProxyHostObject.cpp index 1743ee7..a7d1d6b 100644 --- a/android/src/main/cpp/java-bindings/JImageProxyHostObject.cpp +++ b/android/src/main/cpp/java-bindings/JImageProxyHostObject.cpp @@ -64,7 +64,7 @@ jsi::Value JImageProxyHostObject::get(jsi::Runtime& runtime, const jsi::PropName } if (name == "planesCount") { this->assertIsFrameStrong(runtime, name); - return jsi::Value(this->frame->getPlaneCount()); + return jsi::Value(this->frame->getPlanesCount()); } return jsi::Value::undefined(); diff --git a/android/src/main/java/com/mrousavy/camera/frameprocessor/ImageProxyUtils.java b/android/src/main/java/com/mrousavy/camera/frameprocessor/ImageProxyUtils.java index 3553921..e03a55a 100644 --- a/android/src/main/java/com/mrousavy/camera/frameprocessor/ImageProxyUtils.java +++ b/android/src/main/java/com/mrousavy/camera/frameprocessor/ImageProxyUtils.java @@ -7,6 +7,7 @@ import androidx.annotation.Keep; import androidx.camera.core.ImageProxy; import com.facebook.proguard.annotations.DoNotStrip; +@SuppressWarnings("unused") // used through JNI @DoNotStrip @Keep public class ImageProxyUtils { @@ -26,4 +27,16 @@ public class ImageProxyUtils { return false; } } + + @DoNotStrip + @Keep + public static int getPlanesCount(ImageProxy imageProxy) { + return imageProxy.getPlanes().length; + } + + @DoNotStrip + @Keep + public static int getBytesPerRow(ImageProxy imageProxy) { + return imageProxy.getPlanes()[0].getRowStride(); + } }