Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ local.properties
buck-out/
\.buckd/
*.keystore


react-native-opencv3*.tgz
35 changes: 24 additions & 11 deletions android/src/main/java/com/adamfreeman/rnocv3/CvInvoke.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ private static int getNumKeys(ReadableMap RM) {
}

private Object[] getObjectArr(String func, ReadableMap RM, Class[] params) {

int i = 1;
ArrayList retObjs = new ArrayList<Object>();

Expand Down Expand Up @@ -150,15 +149,29 @@ else if (param == String.class) {
}
else if (itsType == ReadableType.Map) {
ReadableMap dMap = RM.getMap(paramNum);
if (param == Mat.class || param == List.class) {
int matIndex = dMap.getInt("matIndex");
Mat dMat = (Mat)MatManager.getInstance().matAtIndex(matIndex);
if (param == Mat.class) {
retObjs.add(dMat);
}
else {
retObjs.add(Arrays.asList(dMat));
}
if (param == Mat.class || param == List.class) {
int matIndex;

if (dMap.hasKey("listIndex")) {
matIndex = dMap.getInt("listIndex");
List<MatOfPoint> dList = (List<MatOfPoint>)MatManager.getInstance().matAtIndex(matIndex);

if (func.equals("findContours")) {
// Special case, we need to clear the list
dList.clear();
}

retObjs.add(dList);
} else {
matIndex = dMap.getInt("matIndex");
Mat dMat = (Mat)MatManager.getInstance().matAtIndex(matIndex);
if (param == Mat.class) {
retObjs.add(dMat);
}
else {
retObjs.add(Arrays.asList(dMat));
}
}

// have to update the dst mat after op ...
// should be last mat in function parameters unless exception function
Expand Down Expand Up @@ -486,7 +499,7 @@ else if (in != null && matParams.containsKey(in)) {
}

if (dstMatIndex >= 0) {
Mat dstMat = (Mat)objects[arrMatIndex];
Object dstMat = objects[arrMatIndex];
MatManager.getInstance().setMat(dstMatIndex, dstMat);
result = dstMatIndex;
dstMatIndex = -1;
Expand Down
55 changes: 46 additions & 9 deletions android/src/main/java/com/adamfreeman/rnocv3/MatManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;

import java.util.ArrayList;
import java.util.List;

import org.opencv.core.Scalar;
import org.opencv.core.Mat;
import org.opencv.core.MatOfInt;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;

import android.util.Log;

Expand Down Expand Up @@ -90,6 +94,20 @@ public static int createMatOfFloat(float lomatval, float himatval) {
return matIndex;
}

public static int createMatOfPoint() {
int matIndex = mats.size();
MatOfPoint matToAdd = new MatOfPoint();
mats.add(matToAdd);
return matIndex;
}

public static int createListOfMapOfPoint() {
int listIndex = mats.size();
List<MatOfPoint> listToAdd = new ArrayList<>();
mats.add(listToAdd);
return listIndex;
}

public static int addMat(Object matToAdd) {
int matIndex = mats.size();
mats.add(matToAdd);
Expand All @@ -112,15 +130,34 @@ public static void setMat(int matIndex, Object matToSet) {
// TODO: get this to work for different data types checking CvType
public static WritableArray getMatData(int matIndex, int rownum, int colnum) {
WritableArray retArr = new WritableNativeArray();
Mat mat = (Mat)matAtIndex(matIndex);

if (mat.rows() > 0 && mat.cols() > 0) {
float[] retFloats = new float[mat.rows() * mat.cols() * mat.channels()];
mat.get(rownum, colnum, retFloats);
for (float retFloat : retFloats) {
retArr.pushDouble(retFloat);
}
}

Object matObject = matAtIndex(matIndex);

if (List.class.isAssignableFrom(matObject.getClass())) {
List<MatOfPoint> listOfMat = (List<MatOfPoint>)matObject;

for (MatOfPoint mat : listOfMat) {
WritableNativeArray points = new WritableNativeArray();

for (Point point : mat.toArray()) {
points.pushDouble(point.x);
points.pushDouble(point.y);
}

retArr.pushArray(points);
}
} else {
Mat mat = (Mat)matObject;

if (mat.rows() > 0 && mat.cols() > 0) {
float[] retFloats = new float[mat.rows() * mat.cols() * mat.channels()];
mat.get(rownum, colnum, retFloats);
for (float retFloat : retFloats) {
retArr.pushDouble(retFloat);
}
}
}

return retArr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,5 +225,19 @@ public void MatOfFloat(float lomatfloat, float himatfloat, final Promise promise
promise.resolve(result);
}


@ReactMethod
public void MatOfPoint(final Promise promise) {
int matIndex = MatManager.getInstance().createMatOfPoint();
WritableNativeMap result = new WritableNativeMap();
result.putInt("matIndex", matIndex);
promise.resolve(result);
}

@ReactMethod
public void ListOfMatOfPoint(final Promise promise) {
int listIndex = MatManager.getInstance().createListOfMapOfPoint();
WritableNativeMap result = new WritableNativeMap();
result.putInt("listIndex", listIndex);
promise.resolve(result);
}
}
48 changes: 24 additions & 24 deletions coretypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,28 @@ export class CvSize {
export class CvRect {
constructor(top, left, width, height) {
if (top) {
this.top = top
}
else {
this.top = 0.0
}
if (left) {
this.left = left
}
else {
this.left = 0.0
}
if (width) {
this.width = width
}
else {
this.width = 0.0
}
if (height) {
this.height = height
}
else {
this.height = 0.0
}
this.top = top
}
else {
this.top = 0.0
}
if (left) {
this.left = left
}
else {
this.left = 0.0
}
if (width) {
this.width = width
}
else {
this.width = 0.0
}
if (height) {
this.height = height
}
else {
this.height = 0.0
}
}
}
}
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { RNOpencv3 } = NativeModules;

import { ColorConv, CvType, Imgproc, Core } from './constants';
import { CvScalar, CvPoint, CvSize, CvRect } from './coretypes';
import { Mat, MatOfInt, MatOfFloat, setTo, get } from './mats';
import { Mat, MatOfInt, MatOfFloat, MatOfPoint, ListOfMatOfPoint, setTo, get } from './mats';
import { CvImage } from './cvimage';
import { findNodeHandle } from 'react-native';

Expand Down Expand Up @@ -255,8 +255,10 @@ export {
Mat,
MatOfInt,
MatOfFloat,
MatOfPoint,
ListOfMatOfPoint,
CvScalar,
CvPoint,
CvSize,
CvRect
CvRect,
};
12 changes: 12 additions & 0 deletions mats.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,15 @@ export class MatOfFloat {
}
}
}

export class MatOfPoint {
init = async() => {
return await RNOpencv3.MatOfPoint()
}
}

export class ListOfMatOfPoint {
init = async() => {
return await RNOpencv3.ListOfMatOfPoint()
}
}