-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTakePictureButton.js
163 lines (153 loc) · 5.14 KB
/
TakePictureButton.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { LEVEL_INFO, showSnackbarMessage } from 'components/SnackbarMessages'
import { withStyles } from '@material-ui/core/styles'
import AddAPhotoIcon from '@material-ui/icons/AddAPhoto'
import Button from '@material-ui/core/Button'
import Divider from '@material-ui/core/Divider'
import Drawer from '@material-ui/core/Drawer'
import getErrorMessage from 'utils/getErrorMessage'
import isAndroid from 'utils/isAndroid'
import isCordova from 'utils/isCordova'
import PropTypes from 'prop-types'
import React from 'react'
const TakePictureButton = class extends React.Component {
static propTypes = {
onPictureTaken: PropTypes.func.isRequired,
}
state = {
hasInput: false,
isDrawerOpen: false,
}
constructor() {
super()
this.inputRef = React.createRef()
}
componentDidMount() {
if (isCordova() && isAndroid()) {
window.document.addEventListener('resume', this.handleCordovaResume)
}
}
componentWillUnmount() {
if (isCordova() && isAndroid()) {
window.document.removeEventListener('resume', this.handleCordovaResume)
}
}
takePhoto = () => {
if (isCordova() && isAndroid()) {
// Android is special-cased here because the user experience with the `<input>` element does
// not allow the user to take a photo with the camera, therefore we manually give the user the
// choice between camera (using the cordova-plugin-android-photo) or photo library (using the
// `<input>` element).
this.setState({ isDrawerOpen: true })
} else {
this.fetchPhotoWithInputElement()
}
}
fetchPhotoFromCameraWithCordova = () => {
this.closeDrawer()
navigator.photo.takePicture(this.handleImageFromCordova, (error) => {
console.error('navigator.photo.takePicture error:', error)
})
}
handleCordovaResume = (event) => {
if (event && event.action === 'resume' && event.pendingResult && event.pendingResult.pluginServiceName === 'Photo') {
if (event.pendingResult.pluginStatus === 'OK') {
this.handleImageFromCordova(event.pendingResult.result)
} else {
console.log('Resume event from cordova-plugin-android-photo:', event)
showSnackbarMessage('No photo selected', LEVEL_INFO)
}
} else {
console.log('Unrelated cordova resume event:', event)
}
}
handleImageFromCordova = (imageFileUri) => {
window.resolveLocalFileSystemURL(imageFileUri, (fileEntry) => {
fileEntry.file((file) => {
const reader = new FileReader()
reader.onloadend = () => {
fetch(reader.result)
.then(res => res.blob())
.then(blob => {
// add attributes to Blob to make it look like a File to caller
blob.lastModified = file.lastModified
blob.name = file.name
this.props.onPictureTaken(blob)
})
.catch((error) => {
console.error('fetch(reader.result)', error)
showSnackbarMessage(`Error reading photo: ${getErrorMessage(error)}`)
})
}
reader.readAsDataURL(file)
})
}, (error) => {
console.error('window.resolveLocalFileSystemURL', imageFileUri, error)
showSnackbarMessage(`Error resolving photo: ${getErrorMessage(error)}`)
})
}
fetchPhotoWithInputElement = () => {
this.inputRef.current && this.inputRef.current.click()
this.closeDrawer()
}
handleInputChange = (event) => {
this.props.onPictureTaken(event.target.files[0])
// dismiss the input to reset the FileList, allowing a change event to occur when
// selecting the same file consecutively
this.setState({ hasInput: false })
}
closeDrawer = () => {
this.setState({ isDrawerOpen: false })
}
render() {
const { classes } = this.props
const { hasInput, isDrawerOpen } = this.state
if (!hasInput) {
// asynchronously re-add the input field on next tick
setTimeout(() => {
this.setState({ hasInput: true })
}, 0)
}
return (
<React.Fragment>
<Button
variant='extendedFab'
aria-label='Take Photo'
className={classes.button}
onClick={this.takePhoto}
>
<AddAPhotoIcon className={classes.buttonIcon} />
Take Photo
</Button>
{hasInput && <input
ref={this.inputRef}
style={{display: 'none'}}
accept='image/*'
type='file'
onChange={this.handleInputChange}
/>}
<Drawer
anchor='bottom'
open={isDrawerOpen}
onClose={this.closeDrawer}
>
<Button className={classes.drawerButton} onClick={this.fetchPhotoFromCameraWithCordova}>
Take Photo
</Button>
<Button className={classes.drawerButton} onClick={this.fetchPhotoWithInputElement}>
Photo Library
</Button>
<Divider/>
<Button className={classes.drawerButton} onClick={this.closeDrawer}>
Cancel
</Button>
</Drawer>
</React.Fragment>
)
}
}
const styles = (theme) => ({
buttonIcon: {
marginRight: theme.spacing.unit,
},
})
export default withStyles(styles)(TakePictureButton)