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