-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTakePicturePage.tsx
More file actions
192 lines (180 loc) · 5.21 KB
/
TakePicturePage.tsx
File metadata and controls
192 lines (180 loc) · 5.21 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import {
IonButton,
IonButtons,
IonContent,
IonHeader,
IonPage,
IonMenuButton,
IonTitle,
IonToolbar,
IonCardContent,
IonCard,
IonAccordion,
IonAccordionGroup,
IonItem,
IonLabel,
} from "@ionic/react";
import React from "react";
import { Camera, CameraSource, MediaResult, MediaMetadata } from "@capacitor/camera";
import PhotoWithMetadata from "../components/camera/PhotoWithMetadata";
import TakePictureConfigurable from "../components/camera/TakePictureConfigurable";
import { GetPhotoConfigurable } from "../components/camera/old-methods";
import { MediaHistoryService } from "../services/MediaHistoryService";
interface ITakePicturePageState {
filePath: string | null;
metadata: MediaMetadata | string | null;
editedPhoto: MediaResult | null;
}
class TakePicturePage extends React.Component<{}, ITakePicturePageState> {
constructor(props: {}) {
super(props);
this.state = {
filePath: null,
metadata: null,
editedPhoto: null,
};
}
handleTakePhotoResult = (result: MediaResult): void => {
this.setState({
filePath: result.uri ?? result.webPath ?? '',
metadata: result.metadata ?? null,
});
MediaHistoryService.addMedia({
mediaType: "photo",
method: "takePhoto",
uri: result.uri,
webPath: result.webPath,
format: result.metadata?.format,
size: result.metadata?.size,
saved: result.saved,
metadata: result.metadata,
});
};
handlePhotoResult = (result: {
path?: string;
webPath?: string;
base64String?: string;
dataUrl?: string;
exif?: any;
}): void => {
const filePath =
result.path ??
result.webPath ??
result.dataUrl ??
(result.base64String ? `data:image/jpeg;base64,${result.base64String}` : null);
this.setState({
filePath,
metadata: JSON.stringify(result.exif, null, 2),
});
if (filePath) {
MediaHistoryService.addMedia({
mediaType: "photo",
method: "getPhoto",
path: result.path || "",
webPath: result.webPath || "",
});
}
};
clearPhoto = (): void => {
this.setState({
filePath: null,
metadata: null,
editedPhoto: null,
});
};
handleEditPhoto = async (filePath: string): Promise<void> => {
try {
const result = await Camera.editURIPhoto({
uri: filePath,
saveToGallery: false,
includeMetadata: true,
});
this.setState({ editedPhoto: result });
MediaHistoryService.addMedia({
mediaType: "photo",
method: "editURIPhoto",
uri: result.uri,
webPath: result.webPath,
format: result.metadata?.format,
size: result.metadata?.size,
saved: result.saved,
metadata: result.metadata,
});
} catch (e) {
const error = e as any;
const errorMessage = error.code ? `[${error.code}] ${error.message}` : error.message;
alert(`Failed to edit photo with error:\n${errorMessage}`);
}
};
render() {
return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonButtons slot="start">
<IonMenuButton />
</IonButtons>
<IonTitle>Take Picture</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<IonCard>
<IonCardContent>
<TakePictureConfigurable
onPhotoResult={this.handleTakePhotoResult}
/>
</IonCardContent>
</IonCard>
<IonCard>
<IonCardContent>
<IonAccordionGroup>
<IonAccordion value="old-methods">
<IonItem slot="header">
<IonLabel><b>Old methods</b></IonLabel>
</IonItem>
<div slot="content" style={{ padding: "16px 0" }}>
<GetPhotoConfigurable
defaultSource={CameraSource.Prompt}
onPhotoResult={this.handlePhotoResult}
/>
</div>
</IonAccordion>
</IonAccordionGroup>
</IonCardContent>
</IonCard>
{this.state.filePath !== null && (
<>
<IonButton
expand="block"
color="danger"
fill="outline"
onClick={this.clearPhoto}
style={{ margin: "0 16px 16px 16px" }}
>
Clear Photo
</IonButton>
<PhotoWithMetadata
filePath={this.state.filePath}
metadata={this.state.metadata}
onEdit={this.handleEditPhoto}
/>
</>
)}
{this.state.editedPhoto !== null && (
<>
<div style={{ padding: "0 16px", marginTop: "16px" }}>
<h3>Edited Photo</h3>
</div>
<PhotoWithMetadata
filePath={this.state.editedPhoto.uri ?? this.state.editedPhoto.webPath ?? ''}
metadata={this.state.editedPhoto.metadata ?? null}
/>
</>
)}
<div style={{ height: '80px' }} />
</IonContent>
</IonPage>
);
}
}
export default TakePicturePage;