Skip to content

Commit

Permalink
fix(image):
Browse files Browse the repository at this point in the history
  • Loading branch information
tranvantai2905 committed Dec 30, 2023
1 parent 8a13ab7 commit a76ffc9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
40 changes: 22 additions & 18 deletions src/Screens/Scan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { memo, useEffect, useMemo, useRef, useState } from 'react';
import { View, StyleSheet, Text, TouchableOpacity, ActivityIndicator } from 'react-native';
import { StackNavigationProp } from '@react-navigation/stack';
import { RootScreens } from '..';
import { Camera } from 'expo-camera';
import { Camera, CameraType } from 'expo-camera';
import SvgUri from 'react-native-svg-uri';
import { getListIngredients } from '@/api';
import * as FileSystem from 'expo-file-system';
Expand Down Expand Up @@ -43,7 +43,7 @@ const ScanScreen: React.FC<ScanScreenProps> = ({ navigation }) => {
const takePicture = async () => {
if (cameraRef) {
try {
const options = { quality: 1, base64: true }; // Set base64 option to true
const options = { quality: 1, base64: true, format: 'jpg' }; // Set base64 option to true
const photo = await cameraRef.takePictureAsync(options);

// Set the captured image base64 data
Expand All @@ -54,20 +54,22 @@ const ScanScreen: React.FC<ScanScreenProps> = ({ navigation }) => {
// Create FormData
const formData = new FormData();
const fileUri = photo.uri;
const fileName = fileUri.split('/').pop(); // Get the file name from the URI
const fileType = 'image/jpeg'; // Adjust the file type if needed

// Convert the image to Blob
const fileBlob = await FileSystem.readAsStringAsync(fileUri, {
encoding: FileSystem.EncodingType.Base64,
});

formData.append('image', {
uri: fileUri,
name: fileName,
type: fileType,
data: fileBlob, // Assign the image data (Base64 encoded) to the FormData
});
console.log({fileUri});
// const fileName = fileUri.split('/').pop(); // Get the file name from the URI
// const fileType = 'image/jpg'; // Adjust the file type if needed

// // Convert the image to Blob
// const fileBlob = await FileSystem.readAsStringAsync(fileUri, {
// encoding: FileSystem.EncodingType.Base64,
// });

const response = await fetch(fileUri);
const blob = await response.blob();
console.log({blob, fileUri});


// formData.append('image', blob, '412418347_1083552213095570_2864901517473978635_n.jpg');
formData.append('image', blob);

// Send the FormData with base64 image data to the API
await sendImageToAPI(formData);
Expand All @@ -84,8 +86,9 @@ const ScanScreen: React.FC<ScanScreenProps> = ({ navigation }) => {
const sendImageToAPI = async (formData: FormData) => {
try {
const data = await getListIngredients(formData);
console.log({data});

dispatch(setScanIngredients({listScanIngredients:data.ingredients}));
// dispatch(setScanIngredients({listScanIngredients:data.ingredients}));

//mock
const ingredients = [
Expand Down Expand Up @@ -120,11 +123,12 @@ const ScanScreen: React.FC<ScanScreenProps> = ({ navigation }) => {
<View style={styles.container}>
<Camera
style={styles.camera}
type={Camera.Constants.Type.back}
type={CameraType.back}
ref={(ref) => setCameraRef(ref)}
>
<TouchableOpacity style={styles.iconBack} onPress={() => navigation.navigate(RootScreens.HOME)}>
<SvgUri source={require('../../../assets/arrow-left.svg')} />

</TouchableOpacity>
<View style={styles.cameraContent}>
<ScanIcon />
Expand Down
2 changes: 1 addition & 1 deletion src/Screens/ScanResult/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const ScanResultScreen: React.FC<ScanResultScreenProps> = ({ navigation }) => {

try {
let listFood = await Promise.all(
listScanIngredients.map(async (item: any) => {const result = await getListFood(item); return result;})
listScanIngredients?.map(async (item: any) => {const result = await getListFood(item); return result;})
);

// Flatting the listFood array
Expand Down
11 changes: 6 additions & 5 deletions src/Screens/ScannedDetail/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { memo, useCallback, useState } from 'react';
import { View, StyleSheet, TouchableOpacity } from 'react-native';
import { View, StyleSheet, TouchableOpacity, SafeAreaView } from 'react-native';
import { StackNavigationProp } from '@react-navigation/stack';
import { RootScreens } from '..';
import SvgUri from 'react-native-svg-uri';
Expand Down Expand Up @@ -53,7 +53,7 @@ const ScannedDetailScreen: React.FC<ScannedDetailScreenProps> = ({ navigation })
}
}, [ingredientsList]);
return(
<View style={styles.container}>
<SafeAreaView style={styles.container}>
<ScrollView style={styles.scroll_container}>
<TouchableOpacity style={styles.iconBack} onPress={() => navigation.navigate(RootScreens.HOME)}>
<SvgUri source={require('../../../assets/arrow-left.svg')} />
Expand All @@ -69,7 +69,7 @@ const ScannedDetailScreen: React.FC<ScannedDetailScreenProps> = ({ navigation })

</ScrollView>
<View style={styles.scanButtonContainer}>
<TouchableOpacity style={styles.btn} onPress={()=>{}}>
<TouchableOpacity style={styles.btn} onPress={()=>{navigation.navigate(RootScreens.SCAN);}}>
<SvgUri source={require("../../../assets/Scan.svg")} />
<Text style={styles.text}>Scan</Text>
</TouchableOpacity>
Expand All @@ -79,14 +79,15 @@ const ScannedDetailScreen: React.FC<ScannedDetailScreenProps> = ({ navigation })
</TouchableOpacity>
</View>

</View>
</SafeAreaView>
);};

const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
position:"relative"
position:"relative",
marginTop:10,
},
scroll_container:{
width:"100%",
Expand Down
2 changes: 1 addition & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const getDishById = async (id: any) => {

export const getListIngredients = async (formData: any) => {
try {
console.log(formData);
console.log("getListIngredients");
const res = await axios.post('detect-ingredients', formData, {
headers: {
'Content-Type': 'multipart/form-data', // Cần set Content-Type là multipart/form-data khi gửi hình ảnh
Expand Down

0 comments on commit a76ffc9

Please sign in to comment.