-
Notifications
You must be signed in to change notification settings - Fork 66
/
cvimage.js
96 lines (88 loc) · 2.92 KB
/
cvimage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/** 01/28/2019
* OpenCV 3.4.5 minified + Face module ported to react-native
* https://github.com/adamgf/react-native-opencv3
*
* @format
* @flow
* @author Adam Freeman, [email protected]
*/
import { NativeModules } from 'react-native';
import React, {Component} from 'react';
import { Platform, Image } from 'react-native';
const { RNOpencv3 } = NativeModules;
import { ColorConv } from './constants';
var RNFS = require('react-native-fs')
export class CvImage extends Component {
constructor(props) {
super(props)
this.resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource')
this.state = { 'destFile' : '' }
}
componentDidMount = () => {
const assetSource = this.props.source
const uri = this.resolveAssetSource(assetSource).uri
const downloadAssetSource = require('./downloadAssetSource');
downloadAssetSource(uri)
.then((sourceFile) => {
let srcMat
let dstMat
RNOpencv3.Mat().then((res) => {
dstMat = res
RNOpencv3.imageToMat(sourceFile).then((res) => {
srcMat = res
RNFS.unlink(sourceFile).then(() => {
// replace srcMat and dstMat strings with actual srcMat and dstMat
const { cvinvoke } = this.props
if (cvinvoke) {
for (let i=0;i < cvinvoke.paramsArr.length;i++) {
let params = cvinvoke.paramsArr[i]
for (let j=0;j < Object.keys(params).length;j++) {
const pnum = 'p' + (j + 1).toString()
if (params[pnum] && params[pnum] === 'srcMat') {
params[pnum] = srcMat
}
if (params[pnum] && params[pnum] === 'dstMat') {
params[pnum] = dstMat
}
}
}
//alert('cvinvoke is: ' + JSON.stringify(this.props.cvinvoke))
RNOpencv3.invokeMethods(cvinvoke)
}
RNOpencv3.matToImage(dstMat, sourceFile)
.then((image) => {
RNOpencv3.deleteMat(srcMat)
RNOpencv3.deleteMat(dstMat)
const { width, height, uri } = image
if (uri && uri.length > 0) {
this.setState({ destFile : uri })
}
else {
console.error('Error getting image information.')
}
})
.catch((err) => {
console.error(err)
})
})
.catch((err) => {
console.error(err)
})
})
})
})
.catch((err) => {
console.error(err)
})
}
render() {
let imageFilePath = this.resolveAssetSource(this.props.source).uri
if (this.state.destFile.length > 0) {
const prependFilename = Platform.OS === 'ios' ? '' : 'file://'
imageFilePath = prependFilename + this.state.destFile
}
return(
<Image {...this.props} source={{uri:`${imageFilePath}`}} />
)
}
}