Skip to content

Commit be841cb

Browse files
author
pioner921227
committed
init
1 parent 853d106 commit be841cb

File tree

12 files changed

+448
-227
lines changed

12 files changed

+448
-227
lines changed

CODE_OF_CONDUCT.md

Lines changed: 0 additions & 133 deletions
This file was deleted.

android/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cmake_minimum_required(VERSION 3.4.1)
2+
project(saveimagetogallery)
3+
4+
set (CMAKE_VERBOSE_MAKEFILE ON)
5+
set (CMAKE_CXX_STANDARD 17)
6+
7+
8+
add_library(saveimagetogallery SHARED
9+
cpp-adapter.cpp
10+
)
11+
12+
13+
find_package(ReactAndroid REQUIRED CONFIG)
14+
15+
find_package(fbjni REQUIRED CONFIG)
16+
17+
target_include_directories(
18+
saveimagetogallery PRIVATE
19+
"${NODE_MODULES_DIR}/react-native/React"
20+
"${NODE_MODULES_DIR}/react-native/React/Base"
21+
"${NODE_MODULES_DIR}/react-native/ReactCommon"
22+
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
23+
"${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker"
24+
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule"
25+
"${NODE_MODULES_DIR}/react-native/ReactCommon/runtimeexecutor"
26+
)
27+
28+
target_link_libraries(saveimagetogallery
29+
ReactAndroid::jsi
30+
log
31+
ReactAndroid::reactnative
32+
fbjni::fbjni
33+
)

android/build.gradle

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
import groovy.json.JsonSlurper
2+
import org.apache.tools.ant.filters.ReplaceTokens
3+
import java.nio.file.Paths
4+
5+
static def findNodeModules(baseDir) {
6+
def basePath = baseDir.toPath().normalize()
7+
// Node's module resolution algorithm searches up to the root directory,
8+
// after which the base path will be null
9+
while (basePath) {
10+
def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
11+
def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
12+
if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
13+
return nodeModulesPath.toString()
14+
}
15+
basePath = basePath.getParent()
16+
}
17+
throw new GradleException("react-native-img-buffer-save: Failed to find node_modules/ path!")
18+
}
19+
20+
21+
def nodeModules = findNodeModules(projectDir)
22+
logger.warn("react-native-img-buffer-save: node_modules/ found at: ${nodeModules}")
23+
24+
125
buildscript {
226
ext.getExtOrDefault = {name ->
327
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['ImgBufferSave_' + name]
@@ -15,6 +39,10 @@ buildscript {
1539
}
1640
}
1741

42+
def reactNativeArchitectures() {
43+
def value = rootProject.getProperties().get("reactNativeArchitectures")
44+
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
45+
}
1846

1947
def isNewArchitectureEnabled() {
2048
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
@@ -57,10 +85,26 @@ android {
5785
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
5886
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
5987
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
88+
89+
externalNativeBuild {
90+
cmake {
91+
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
92+
abiFilters (*reactNativeArchitectures())
93+
arguments "-DANDROID_STL=c++_shared",
94+
"-DNODE_MODULES_DIR=${nodeModules}"
95+
}
96+
}
6097
}
6198

99+
externalNativeBuild {
100+
cmake {
101+
path "CMakeLists.txt"
102+
}
103+
}
104+
62105
buildFeatures {
63106
buildConfig true
107+
prefab true
64108
}
65109

66110
buildTypes {
@@ -69,6 +113,18 @@ android {
69113
}
70114
}
71115

116+
packagingOptions {
117+
excludes = [
118+
"**/libjsi.so",
119+
"**/libreactnativejni.so",
120+
"**/libreact_nativemodule_core.so",
121+
"**/libturbomodulejsijni.so",
122+
"**/libc++_shared.so",
123+
"**/libfbjni.so",
124+
"**/libreactnative.so",
125+
]
126+
}
127+
72128
lintOptions {
73129
disable "GradleCompatible"
74130
}

android/cpp-adapter.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <jni.h>
2+
#include "jsi/jsi.h"
3+
#include <android/log.h>
4+
#include <ReactCommon/CallInvoker.h>
5+
#include <ReactCommon/CallInvokerHolder.h>
6+
#include <fbjni/fbjni.h>
7+
#include <fbjni/detail/Registration.h>
8+
#include <typeinfo>
9+
#include <fstream>
10+
#include <vector>
11+
#include <sys/stat.h>
12+
13+
14+
using namespace facebook;
15+
16+
struct ImgBufferSaveModuleBridge : jni::JavaClass<ImgBufferSaveModuleBridge> {
17+
public:
18+
static constexpr auto kJavaDescriptor = "Lcom/imgbuffersave/ImgBufferSaveModule;";
19+
20+
static void registerNatives() {
21+
javaClassStatic()->registerNatives({
22+
makeNativeMethod("nativeInstall",
23+
ImgBufferSaveModuleBridge::nativeInstall)
24+
});
25+
}
26+
27+
private:
28+
static void nativeInstall(
29+
jni::alias_ref<jni::JObject> thiz,
30+
jlong jsiRuntimePointer,
31+
jni::alias_ref<react::CallInvokerHolder::javaobject> jsCallInvokerHolder
32+
) {
33+
34+
jsi::Runtime *runtime_ptr = reinterpret_cast<jsi::Runtime *>(jsiRuntimePointer);
35+
36+
auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker();
37+
38+
auto saveImageToGallery = jsi::Function::createFromHostFunction(
39+
*runtime_ptr, jsi::PropNameID::forAscii(*runtime_ptr, "saveImageToGallery"), 1,
40+
[](jsi::Runtime &runtime, const jsi::Value &thisValue,
41+
const jsi::Value *arguments, size_t count) -> jsi::Value {
42+
43+
if (count < 1 || !arguments[0].isObject()) {
44+
throw jsi::JSError(runtime, "[SaveImageToGallery] argument must be an ArrayBuffer");
45+
}
46+
47+
jsi::Object obj = arguments[0].asObject(runtime);
48+
49+
if (!obj.isArrayBuffer(runtime)) {
50+
throw jsi::JSError(runtime,
51+
"[SaveImageToGallery] argument must be an ArrayBuffer");
52+
}
53+
54+
auto buffer = obj.getArrayBuffer(runtime);
55+
auto *data = reinterpret_cast<uint8_t *>(buffer.data(runtime));
56+
size_t length = buffer.size(runtime);
57+
58+
std::string fileName = "INTCH_PROFILE_QR_" + std::to_string(time(nullptr)) + ".png";
59+
60+
auto javaClass = jni::findClassStatic(
61+
"com/imgbuffersave/ImgBufferSaveModule");
62+
auto method = javaClass->getStaticMethod<void(jni::alias_ref<jni::JString>,
63+
jni::alias_ref<jni::JPrimitiveArray<jbyteArray>>)>(
64+
"addImageToGalleryAsync");
65+
66+
jni::local_ref<jni::JString> jFileName = jni::make_jstring(fileName);
67+
68+
auto jByteArray = jni::JPrimitiveArray<jbyteArray>::newArray(
69+
static_cast<jsize>(length));
70+
jByteArray->setRegion(0, static_cast<jsize>(length),
71+
reinterpret_cast<const jbyte *>(data));
72+
73+
method(javaClass, jFileName, jByteArray);
74+
75+
return true;
76+
});
77+
78+
runtime_ptr->global().setProperty(*runtime_ptr, "saveImageToGallery",
79+
std::move(saveImageToGallery));
80+
}
81+
};
82+
83+
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) {
84+
return jni::initialize(vm, [] { ImgBufferSaveModuleBridge::registerNatives(); });
85+
}

0 commit comments

Comments
 (0)