Skip to content

Commit 81432da

Browse files
Merge pull request #43 from lukaszkurantdev/feat/image-conversion-format
feat: image conversion format
2 parents 7fceaa3 + cdbac0b commit 81432da

File tree

6 files changed

+83
-32
lines changed

6 files changed

+83
-32
lines changed

cpp/ConvertImage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ std::string ImageConverter::base64_decode(std::string const& encoded_string)
139139
return ret;
140140
}
141141

142-
string ImageConverter::mat2str(const Mat& m)
142+
string ImageConverter::mat2str(const Mat& m, std::string &format)
143143
{
144144
vector<uchar> buf;
145-
cv::imencode(".jpeg", m, buf);
145+
cv::imencode("." + format, m, buf);
146146
auto result = reinterpret_cast<const unsigned char*>(buf.data());
147147

148148
return base64_encode(result, buf.size());

cpp/ConvertImage.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using namespace cv;
2222
class ImageConverter {
2323
public:
2424
static cv::Mat str2mat(const string& imageBase64);
25-
static string mat2str(const Mat& img);
25+
static string mat2str(const Mat& img, std::string &format);
2626

2727
private:
2828
static std::string base64_encode(uchar const* bytesToEncode, unsigned int inLen);

cpp/FOCV_Object.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,15 @@ jsi::Object FOCV_Object::convertToJSI(jsi::Runtime& runtime, const jsi::Value* a
176176
switch(hashString(objectType.c_str(), objectType.size())) {
177177
case hashString("mat", 3): {
178178
auto mat = *FOCV_Storage::get<cv::Mat>(id);
179+
std::string format = "jpeg";
180+
181+
if(arguments[1].isString()) {
182+
format = arguments[1].asString(runtime).utf8(runtime);
183+
}
184+
179185
mat.convertTo(mat, CV_8U);
180186

181-
value.setProperty(runtime, "base64", jsi::String::createFromUtf8(runtime, ImageConverter::mat2str(mat)));
187+
value.setProperty(runtime, "base64", jsi::String::createFromUtf8(runtime, ImageConverter::mat2str(mat, format)));
182188
value.setProperty(runtime, "size", jsi::Value(mat.size));
183189
value.setProperty(runtime, "cols", jsi::Value(mat.cols));
184190
value.setProperty(runtime, "rows", jsi::Value(mat.rows));

docs/pages/apidetails.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ copyObjectFromVector(vector: RectVector, itemIndex: number): Rect;
6363
Converts an object to a JS-readable object.
6464

6565
```js
66-
toJSValue(mat: Mat): {
66+
toJSValue(
67+
mat: Mat,
68+
outImageFormat?: 'jpeg' | 'png'
69+
): {
6770
size: number;
6871
cols: number;
6972
rows: number;

example/ios/Podfile.lock

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ PODS:
936936
- React-Mapbuffer (0.74.4):
937937
- glog
938938
- React-debug
939-
- react-native-fast-opencv (0.3.1):
939+
- react-native-fast-opencv (0.3.3):
940940
- DoubleConversion
941941
- FastOpenCV-iOS (= 1.0.4)
942942
- glog
@@ -979,9 +979,9 @@ PODS:
979979
- ReactCommon/turbomodule/bridging
980980
- ReactCommon/turbomodule/core
981981
- Yoga
982-
- react-native-safe-area-context (4.11.0):
982+
- react-native-safe-area-context (4.14.0):
983983
- React-Core
984-
- react-native-skia (1.3.11):
984+
- react-native-skia (1.5.5):
985985
- DoubleConversion
986986
- glog
987987
- hermes-engine
@@ -1004,10 +1004,27 @@ PODS:
10041004
- ReactCommon/turbomodule/bridging
10051005
- ReactCommon/turbomodule/core
10061006
- Yoga
1007-
- react-native-worklets-core (1.3.3):
1008-
- React
1009-
- React-callinvoker
1007+
- react-native-worklets-core (1.5.0):
1008+
- DoubleConversion
1009+
- glog
1010+
- hermes-engine
1011+
- RCT-Folly (= 2024.01.01.00)
1012+
- RCTRequired
1013+
- RCTTypeSafety
1014+
- React-Codegen
10101015
- React-Core
1016+
- React-debug
1017+
- React-Fabric
1018+
- React-featureflags
1019+
- React-graphics
1020+
- React-ImageManager
1021+
- React-NativeModulesApple
1022+
- React-RCTFabric
1023+
- React-rendererdebug
1024+
- React-utils
1025+
- ReactCommon/turbomodule/bridging
1026+
- ReactCommon/turbomodule/core
1027+
- Yoga
10111028
- React-nativeconfig (0.74.4)
10121029
- React-NativeModulesApple (0.74.4):
10131030
- glog
@@ -1237,7 +1254,30 @@ PODS:
12371254
- React-logger (= 0.74.4)
12381255
- React-perflogger (= 0.74.4)
12391256
- React-utils (= 0.74.4)
1240-
- RNReanimated (3.15.0):
1257+
- RNReanimated (3.16.1):
1258+
- DoubleConversion
1259+
- glog
1260+
- hermes-engine
1261+
- RCT-Folly (= 2024.01.01.00)
1262+
- RCTRequired
1263+
- RCTTypeSafety
1264+
- React-Codegen
1265+
- React-Core
1266+
- React-debug
1267+
- React-Fabric
1268+
- React-featureflags
1269+
- React-graphics
1270+
- React-ImageManager
1271+
- React-NativeModulesApple
1272+
- React-RCTFabric
1273+
- React-rendererdebug
1274+
- React-utils
1275+
- ReactCommon/turbomodule/bridging
1276+
- ReactCommon/turbomodule/core
1277+
- RNReanimated/reanimated (= 3.16.1)
1278+
- RNReanimated/worklets (= 3.16.1)
1279+
- Yoga
1280+
- RNReanimated/reanimated (3.16.1):
12411281
- DoubleConversion
12421282
- glog
12431283
- hermes-engine
@@ -1257,10 +1297,9 @@ PODS:
12571297
- React-utils
12581298
- ReactCommon/turbomodule/bridging
12591299
- ReactCommon/turbomodule/core
1260-
- RNReanimated/reanimated (= 3.15.0)
1261-
- RNReanimated/worklets (= 3.15.0)
1300+
- RNReanimated/reanimated/apple (= 3.16.1)
12621301
- Yoga
1263-
- RNReanimated/reanimated (3.15.0):
1302+
- RNReanimated/reanimated/apple (3.16.1):
12641303
- DoubleConversion
12651304
- glog
12661305
- hermes-engine
@@ -1281,7 +1320,7 @@ PODS:
12811320
- ReactCommon/turbomodule/bridging
12821321
- ReactCommon/turbomodule/core
12831322
- Yoga
1284-
- RNReanimated/worklets (3.15.0):
1323+
- RNReanimated/worklets (3.16.1):
12851324
- DoubleConversion
12861325
- glog
12871326
- hermes-engine
@@ -1302,7 +1341,7 @@ PODS:
13021341
- ReactCommon/turbomodule/bridging
13031342
- ReactCommon/turbomodule/core
13041343
- Yoga
1305-
- RNScreens (3.34.0):
1344+
- RNScreens (3.35.0):
13061345
- DoubleConversion
13071346
- glog
13081347
- hermes-engine
@@ -1347,16 +1386,16 @@ PODS:
13471386
- ReactCommon/turbomodule/core
13481387
- VisionCamera
13491388
- Yoga
1350-
- VisionCamera (4.5.2):
1351-
- VisionCamera/Core (= 4.5.2)
1352-
- VisionCamera/FrameProcessors (= 4.5.2)
1353-
- VisionCamera/React (= 4.5.2)
1354-
- VisionCamera/Core (4.5.2)
1355-
- VisionCamera/FrameProcessors (4.5.2):
1389+
- VisionCamera (4.6.1):
1390+
- VisionCamera/Core (= 4.6.1)
1391+
- VisionCamera/FrameProcessors (= 4.6.1)
1392+
- VisionCamera/React (= 4.6.1)
1393+
- VisionCamera/Core (4.6.1)
1394+
- VisionCamera/FrameProcessors (4.6.1):
13561395
- React
13571396
- React-callinvoker
13581397
- react-native-worklets-core
1359-
- VisionCamera/React (4.5.2):
1398+
- VisionCamera/React (4.6.1):
13601399
- React-Core
13611400
- VisionCamera/FrameProcessors
13621401
- Yoga (0.0.0)
@@ -1594,11 +1633,11 @@ SPEC CHECKSUMS:
15941633
React-jsitracing: 4e9c99e73a6269b27b0d4cbab277dd90df3e5ac0
15951634
React-logger: fbfb50e2a2b1b46ee087f0a52739fadecc5e81a4
15961635
React-Mapbuffer: d39610dff659d8cf1fea485abae08bbf6f9c8279
1597-
react-native-fast-opencv: 7b63cf65c463b153286f5556ef51b0cad47fb128
1636+
react-native-fast-opencv: 973f3a99309db23b4ebded20de36757c790d1ec5
15981637
react-native-image-picker: c3afe5472ef870d98a4b28415fc0b928161ee5f7
1599-
react-native-safe-area-context: 851c62c48dce80ccaa5637b6aa5991a1bc36eca9
1600-
react-native-skia: 8da84ea9410504bf27f0db229539a43f6caabb6a
1601-
react-native-worklets-core: f51430dd07bf5343d4918d28a4bb00fe8f98b982
1638+
react-native-safe-area-context: 4532f1a0c5d34a46b9324ccaaedcb5582a302b7d
1639+
react-native-skia: e2b30a7cca22f5daef0ed243d18de58403e7a997
1640+
react-native-worklets-core: e0a05ed7887519277942efc866fd2785a24c86db
16021641
React-nativeconfig: 2be4363c2c4ac2b42419577774e83e4e4fd2af9f
16031642
React-NativeModulesApple: 453ada38f826a508e48872c7a7877c431af48bba
16041643
React-perflogger: 9745f800ab4d12ec4325bde7bd090eafb87c5570
@@ -1622,11 +1661,11 @@ SPEC CHECKSUMS:
16221661
React-runtimescheduler: 3f312d33f475467a59864e0c5ab8708461387d1c
16231662
React-utils: e8b0eac797c81c574b24f6515fec4015599b643c
16241663
ReactCommon: eebffb37a90138c6db6eb8b2d952e7e5c6bc083c
1625-
RNReanimated: 45553a3ae29a75a76269595f8554d07d4090e392
1626-
RNScreens: aa943ad421c3ced3ef5a47ede02b0cbfc43a012e
1664+
RNReanimated: af4e059a8fd0fb7a9cdf5ad35ead4699598a9447
1665+
RNScreens: 6b641f232990a9d505a6d139fd18c3c759c9d290
16271666
SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d
16281667
vision-camera-resize-plugin: 4306d5df9bce0e603bbe6ab04337f21a606f4ad1
1629-
VisionCamera: 057aff621f7801b7d99a00d157fa39244bbd4fd2
1668+
VisionCamera: ec141897a88c2e95e8b83cf97b8e4db801e02fd6
16301669
Yoga: 6259f968a4fdf516d76a4432dead624d71aa0fb1
16311670

16321671
PODFILE CHECKSUM: ded8a41f26047703e900afe99b8a72ca375b02ca

src/objects/Objects.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ export type Objects = {
6060
d: number
6161
): Scalar;
6262

63-
toJSValue(mat: Mat): {
63+
toJSValue(
64+
mat: Mat,
65+
outImageFormat?: 'jpeg' | 'png'
66+
): {
6467
size: number;
6568
cols: number;
6669
rows: number;

0 commit comments

Comments
 (0)