Spaces:
Runtime error
Runtime error
Update app.js
Browse files
app.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
import React, { useRef, useEffect, useState } from "react";
|
| 2 |
import { StyleSheet, View, Button, Alert } from "react-native";
|
| 3 |
import { Camera, useCameraDevices } from "react-native-vision-camera";
|
| 4 |
-
|
| 5 |
const App = () => {
|
| 6 |
const [hasPermission, setHasPermission] = useState(false);
|
| 7 |
-
const [
|
| 8 |
const camera = useRef(null);
|
| 9 |
const devices = useCameraDevices();
|
| 10 |
-
const device = devices
|
| 11 |
|
| 12 |
useEffect(() => {
|
| 13 |
(async () => {
|
|
@@ -21,16 +20,24 @@ const App = () => {
|
|
| 21 |
|
| 22 |
try {
|
| 23 |
const photo = await camera.current.takePhoto({
|
| 24 |
-
quality: 0.8,
|
| 25 |
-
skipMetadata: true,
|
| 26 |
});
|
|
|
|
| 27 |
console.log("Captured Image Path:", photo.path);
|
| 28 |
-
setCapturedImage(photo.path);
|
| 29 |
|
| 30 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
const formData = new FormData();
|
| 32 |
formData.append("file", {
|
| 33 |
-
uri:
|
| 34 |
type: "image/jpeg",
|
| 35 |
name: "photo.jpg",
|
| 36 |
});
|
|
@@ -59,6 +66,12 @@ const App = () => {
|
|
| 59 |
photo={true}
|
| 60 |
/>
|
| 61 |
<View style={styles.buttonContainer}>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
<Button title="Capture Image" onPress={captureImage} />
|
| 63 |
</View>
|
| 64 |
</View>
|
|
@@ -73,7 +86,8 @@ const styles = StyleSheet.create({
|
|
| 73 |
position: "absolute",
|
| 74 |
bottom: 20,
|
| 75 |
width: "100%",
|
| 76 |
-
|
|
|
|
| 77 |
},
|
| 78 |
});
|
| 79 |
|
|
|
|
| 1 |
import React, { useRef, useEffect, useState } from "react";
|
| 2 |
import { StyleSheet, View, Button, Alert } from "react-native";
|
| 3 |
import { Camera, useCameraDevices } from "react-native-vision-camera";
|
|
|
|
| 4 |
const App = () => {
|
| 5 |
const [hasPermission, setHasPermission] = useState(false);
|
| 6 |
+
const [cameraType, setCameraType] = useState("rear"); // Default to rear camera
|
| 7 |
const camera = useRef(null);
|
| 8 |
const devices = useCameraDevices();
|
| 9 |
+
const device = devices[cameraType];
|
| 10 |
|
| 11 |
useEffect(() => {
|
| 12 |
(async () => {
|
|
|
|
| 20 |
|
| 21 |
try {
|
| 22 |
const photo = await camera.current.takePhoto({
|
| 23 |
+
quality: 0.8, // Adjust quality as needed
|
|
|
|
| 24 |
});
|
| 25 |
+
|
| 26 |
console.log("Captured Image Path:", photo.path);
|
|
|
|
| 27 |
|
| 28 |
+
// Correct image orientation for the rear camera
|
| 29 |
+
const fixedImage = await ImageManipulator.manipulateAsync(
|
| 30 |
+
photo.path,
|
| 31 |
+
[{ flip: cameraType === "rear" ? "horizontal" : undefined }], // Flip only for rear camera
|
| 32 |
+
{ compress: 1, format: ImageManipulator.SaveFormat.JPEG }
|
| 33 |
+
);
|
| 34 |
+
|
| 35 |
+
console.log("Fixed Image Path:", fixedImage.uri);
|
| 36 |
+
|
| 37 |
+
// Send the fixed image to the backend
|
| 38 |
const formData = new FormData();
|
| 39 |
formData.append("file", {
|
| 40 |
+
uri: fixedImage.uri,
|
| 41 |
type: "image/jpeg",
|
| 42 |
name: "photo.jpg",
|
| 43 |
});
|
|
|
|
| 66 |
photo={true}
|
| 67 |
/>
|
| 68 |
<View style={styles.buttonContainer}>
|
| 69 |
+
<Button
|
| 70 |
+
title={`Switch to ${cameraType === "rear" ? "Front" : "Rear"} Camera`}
|
| 71 |
+
onPress={() =>
|
| 72 |
+
setCameraType((prev) => (prev === "rear" ? "front" : "rear"))
|
| 73 |
+
}
|
| 74 |
+
/>
|
| 75 |
<Button title="Capture Image" onPress={captureImage} />
|
| 76 |
</View>
|
| 77 |
</View>
|
|
|
|
| 86 |
position: "absolute",
|
| 87 |
bottom: 20,
|
| 88 |
width: "100%",
|
| 89 |
+
flexDirection: "row",
|
| 90 |
+
justifyContent: "space-evenly",
|
| 91 |
},
|
| 92 |
});
|
| 93 |
|