-
Notifications
You must be signed in to change notification settings - Fork 2
/
Camera.js
131 lines (120 loc) · 3.19 KB
/
Camera.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
'use strict';
import React, { Component } from 'react';
import { Platform, View, Text, StyleSheet, TouchableOpacity } from 'react-native';
import { Icon } from 'native-base';
import { RNCamera } from 'react-native-camera';
import { dirPicutures } from './dirStorage';
const moment = require('moment');
const RNFS = require('react-native-fs');
const PendingView = () => (
<View
style={{
flex: 1,
backgroundColor: 'lightgreen',
justifyContent: 'center',
alignItems: 'center',
}}
>
<Text>Waiting</Text>
</View>
);
//move the attachment to app folder
const moveAttachment = async (filePath, newFilepath) => {
return new Promise((resolve, reject) => {
console.log("dirPicutures=" + dirPicutures);
console.log("filePath=" + filePath);
console.log("newFilepath=" + newFilepath);
RNFS.mkdir(dirPicutures)
.then(() => {
RNFS.moveFile(filePath, newFilepath)
.then(() => {
console.log('FILE MOVED', filePath, newFilepath);
resolve(true);
})
.catch(error => {
console.log('moveFile error', error);
reject(error);
});
})
.catch(err => {
console.log('mkdir error', err);
reject(err);
});
});
};
export default class Camera extends Component {
render() {
return (
<View style={styles.container}>
<RNCamera
ref={cam => {
this.camera = cam;
}}
style={styles.preview}
type={RNCamera.Constants.Type.back}
flashMode={RNCamera.Constants.FlashMode.on}
onGoogleVisionBarcodesDetected={({ barcodes }) => {
console.log(barcodes)
}}>
<View style={{ flex: 0, flexDirection: 'row', justifyContent: 'center' }}>
<TouchableOpacity
onPress={this.takePicture.bind(this)}
style={styles.capture}>
<Icon name="camera" style={{ fontSize: 40, color: 'white' }} />
</TouchableOpacity>
</View>
</RNCamera>
</View>
);
}
takePicture = async function () {
if (this.camera) {
const options = { quality: 0.5, base64: true }
const data = await this.camera.takePictureAsync(options)
this.saveImage(data.uri);
console.log(data.uri);
}
};
saveImage = async filePath => {
try {
// set new image name and filepath
const newImageName = `${moment().format('DDMMYY_HHmmSSS')}.jpg`;
const newFilepath = `${dirPicutures}/${newImageName}`
// move and save image to new filepath
const imageMoved = await moveAttachment(filePath, newFilepath);
console.log('image moved', imageMoved);
} catch (error) {
console.log(error);
}
};
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
preview: {
flex: 1,
justifyContent: 'flex-end',
alignItems: 'center'
},
capture: {
flex: 0,
borderRadius: 5,
padding: 15,
paddingHorizontal: 20,
alignSelf: 'center',
margin: 20
}
});