Added handleImagePicker for scaning Qr Code from library.incomplete; need to handle error before passing to setScannedData
This commit is contained in:
12
package-lock.json
generated
12
package-lock.json
generated
@@ -14,6 +14,7 @@
|
|||||||
"axios": "^1.7.2",
|
"axios": "^1.7.2",
|
||||||
"expo": "~51.0.11",
|
"expo": "~51.0.11",
|
||||||
"expo-camera": "~15.0.10",
|
"expo-camera": "~15.0.10",
|
||||||
|
"expo-image-manipulator": "^12.0.5",
|
||||||
"expo-image-picker": "~15.0.5",
|
"expo-image-picker": "~15.0.5",
|
||||||
"expo-status-bar": "~1.12.1",
|
"expo-status-bar": "~1.12.1",
|
||||||
"react": "18.2.0",
|
"react": "18.2.0",
|
||||||
@@ -7565,6 +7566,17 @@
|
|||||||
"expo": "*"
|
"expo": "*"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/expo-image-manipulator": {
|
||||||
|
"version": "12.0.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/expo-image-manipulator/-/expo-image-manipulator-12.0.5.tgz",
|
||||||
|
"integrity": "sha512-zJ8yINjckYw/yfoSuICt4yJ9xr112+W9e5QVXwK3nCAHr7sv45RQ5sxte0qppf594TPl+UoV6Tjim7WpoKipRQ==",
|
||||||
|
"dependencies": {
|
||||||
|
"expo-image-loader": "~4.7.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"expo": "*"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/expo-image-picker": {
|
"node_modules/expo-image-picker": {
|
||||||
"version": "15.0.5",
|
"version": "15.0.5",
|
||||||
"resolved": "https://registry.npmjs.org/expo-image-picker/-/expo-image-picker-15.0.5.tgz",
|
"resolved": "https://registry.npmjs.org/expo-image-picker/-/expo-image-picker-15.0.5.tgz",
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
"axios": "^1.7.2",
|
"axios": "^1.7.2",
|
||||||
"expo": "~51.0.11",
|
"expo": "~51.0.11",
|
||||||
"expo-camera": "~15.0.10",
|
"expo-camera": "~15.0.10",
|
||||||
|
"expo-image-manipulator": "^12.0.5",
|
||||||
"expo-image-picker": "~15.0.5",
|
"expo-image-picker": "~15.0.5",
|
||||||
"expo-status-bar": "~1.12.1",
|
"expo-status-bar": "~1.12.1",
|
||||||
"react": "18.2.0",
|
"react": "18.2.0",
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
import React, { useState, useEffect, useContext } from 'react';
|
import React, { useState, useEffect, useContext } from 'react';
|
||||||
import { View, Text, StyleSheet, ActivityIndicator, TouchableOpacity, Button } from 'react-native';
|
import { View, Text, StyleSheet, ActivityIndicator, TouchableOpacity, Button, Alert } from 'react-native';
|
||||||
import { Camera, CameraView } from 'expo-camera';
|
import { Camera, CameraView, scanFromURLAsync } from 'expo-camera';
|
||||||
import { QRCodeContext } from '../types';
|
import { QRCodeContext } from '../types';
|
||||||
import axios from 'axios'; // For URL calls
|
import axios from 'axios'; // For URL calls
|
||||||
import { Ionicons } from '@expo/vector-icons'; // For icons
|
import { Ionicons } from '@expo/vector-icons'; // For icons
|
||||||
import { useNavigation } from '@react-navigation/native';
|
import { useNavigation } from '@react-navigation/native';
|
||||||
|
import * as ImagePicker from 'expo-image-picker';
|
||||||
|
|
||||||
|
|
||||||
//-----------------FUNCTIONS DECLARED HERE------------------//
|
//-----------------FUNCTIONS DECLARED HERE------------------//
|
||||||
// Function to determine the type of data
|
// Function to determine the type of data
|
||||||
@@ -67,10 +66,9 @@ interface QRScannerScreenProps {
|
|||||||
clearScanData: () => void;
|
clearScanData: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------Main------------------//
|
//-----------------Main------------------//
|
||||||
const QRScannerScreen: React.FC<QRScannerScreenProps> = ({ clearScanData }) => {
|
const QRScannerScreen: React.FC<QRScannerScreenProps> = ({ clearScanData }) => {
|
||||||
|
|
||||||
const navigation = useNavigation(); // call Navigation bar
|
const navigation = useNavigation(); // call Navigation bar
|
||||||
const [showSplash, setShowSplash] = useState<boolean>(true); // call splash screen
|
const [showSplash, setShowSplash] = useState<boolean>(true); // call splash screen
|
||||||
|
|
||||||
@@ -103,8 +101,6 @@ const QRScannerScreen: React.FC<QRScannerScreenProps> = ({ clearScanData }) => {
|
|||||||
initializeApp();
|
initializeApp();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// If loading this screen , reset the scanning data
|
// If loading this screen , reset the scanning data
|
||||||
const clearScanDataInternal = () => {
|
const clearScanDataInternal = () => {
|
||||||
setScannedData('');
|
setScannedData('');
|
||||||
@@ -113,8 +109,6 @@ const QRScannerScreen: React.FC<QRScannerScreenProps> = ({ clearScanData }) => {
|
|||||||
setDataType('');
|
setDataType('');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// The function that takes data from <Cameraview onBarcodeScanned
|
// The function that takes data from <Cameraview onBarcodeScanned
|
||||||
const handleQRCodeScanned = async ({ data }: { type: string; data: string }) => {
|
const handleQRCodeScanned = async ({ data }: { type: string; data: string }) => {
|
||||||
setScanned(true); //Flag is QR code already scanned
|
setScanned(true); //Flag is QR code already scanned
|
||||||
@@ -137,8 +131,6 @@ const QRScannerScreen: React.FC<QRScannerScreenProps> = ({ clearScanData }) => {
|
|||||||
setQrCodes([...qrCodes, newScannedData]);
|
setQrCodes([...qrCodes, newScannedData]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// If the focus is lost focus on this screen , when come reset the scan data
|
// If the focus is lost focus on this screen , when come reset the scan data
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const unsubscribe = navigation.addListener('focus', () => {
|
const unsubscribe = navigation.addListener('focus', () => {
|
||||||
@@ -147,7 +139,6 @@ const QRScannerScreen: React.FC<QRScannerScreenProps> = ({ clearScanData }) => {
|
|||||||
return unsubscribe;
|
return unsubscribe;
|
||||||
}, [navigation]);
|
}, [navigation]);
|
||||||
|
|
||||||
|
|
||||||
// This function is for toggling torch
|
// This function is for toggling torch
|
||||||
const toggleTorch = () => {
|
const toggleTorch = () => {
|
||||||
setEnableTorch((prev) => !prev);
|
setEnableTorch((prev) => !prev);
|
||||||
@@ -158,8 +149,32 @@ const QRScannerScreen: React.FC<QRScannerScreenProps> = ({ clearScanData }) => {
|
|||||||
handleQRCodeScanned({ type: 'TEST', data: 'TEST123' });
|
handleQRCodeScanned({ type: 'TEST', data: 'TEST123' });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Function to handle QR code scanning from the image picker
|
||||||
|
const handleImagePicker = async () => {
|
||||||
|
const result = await ImagePicker.launchImageLibraryAsync({
|
||||||
|
mediaTypes: ImagePicker.MediaTypeOptions.Images,
|
||||||
|
allowsEditing: false, // Disabling the crop functionality
|
||||||
|
quality: 1,
|
||||||
|
});
|
||||||
|
|
||||||
// For Splash, for some reason nedd to be near the end of the function...
|
if (result && result.assets[0].uri) {
|
||||||
|
try {
|
||||||
|
const scannedResults = await scanFromURLAsync(result.assets[0].uri);
|
||||||
|
if (scannedResults.length > 0) {
|
||||||
|
const dataNeeded = scannedResults[0].data;
|
||||||
|
handleQRCodeScanned({ type: 'QR_CODE', data: dataNeeded });
|
||||||
|
} else {
|
||||||
|
setScannedData("No QR Code Found");
|
||||||
|
setTimeout(() => setScannedData(""), 4000);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error scanning QR code from image:', error);
|
||||||
|
Alert.alert('Failed to scan QR code from image.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// For Splash, for some reason need to be near the end of the function...
|
||||||
// or else permission for camera is not asked
|
// or else permission for camera is not asked
|
||||||
if (showSplash) {
|
if (showSplash) {
|
||||||
return (
|
return (
|
||||||
@@ -169,31 +184,27 @@ const QRScannerScreen: React.FC<QRScannerScreenProps> = ({ clearScanData }) => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// While asking for permision the page behind will render this only
|
// While asking for permission the page behind will render this only
|
||||||
if (hasPermission === null) {
|
if (hasPermission === null) {
|
||||||
return <Text>Requesting for camera permission</Text>;
|
return <Text>Requesting for camera permission</Text>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// this will thrown on the screen and nothing else will load if no permission
|
// this will thrown on the screen and nothing else will load if no permission
|
||||||
if (hasPermission === false) {
|
if (hasPermission === false) {
|
||||||
return <Text>No access to camera</Text>;
|
return <Text>No access to camera</Text>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//---------------The UI part-----------------//
|
//---------------The UI part-----------------//
|
||||||
return (
|
return (
|
||||||
|
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
{/* Banner section */}
|
{/* Banner section */}
|
||||||
<View style={styles.banner}>
|
<View style={styles.banner}>
|
||||||
|
<Text style={styles.headerText}>SafeQR v0.70</Text>
|
||||||
<Text style={styles.headerText}>SafeQR v0.66.t</Text>
|
|
||||||
</View>
|
</View>
|
||||||
{/* Welcome Text */}
|
{/* Welcome Text */}
|
||||||
<Text style={styles.welcomeText}>Welcome to SafeQR code Scanner</Text>
|
<Text style={styles.welcomeText}>Welcome to SafeQR code Scanner</Text>
|
||||||
|
|
||||||
{/* The cutour for the camera */}
|
{/* The cutout for the camera */}
|
||||||
<View style={styles.cameraContainer}>
|
<View style={styles.cameraContainer}>
|
||||||
<CameraView
|
<CameraView
|
||||||
onBarcodeScanned={scanned ? undefined : handleQRCodeScanned}
|
onBarcodeScanned={scanned ? undefined : handleQRCodeScanned}
|
||||||
@@ -202,18 +213,21 @@ const QRScannerScreen: React.FC<QRScannerScreenProps> = ({ clearScanData }) => {
|
|||||||
enableTorch={enableTorch}
|
enableTorch={enableTorch}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* the torch icon floating above the camerView */}
|
{/* the torch icon floating above the CameraView */}
|
||||||
<TouchableOpacity onPress={toggleTorch} style={styles.flashButton}>
|
<TouchableOpacity onPress={toggleTorch} style={styles.flashButton}>
|
||||||
<Ionicons name="flashlight" size={24} color="#fff" />
|
<Ionicons name="flashlight" size={24} color="#fff" />
|
||||||
{enableTorch}
|
{enableTorch}
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
<TouchableOpacity onPress={handleTestScan} style={styles.testButton}>
|
<TouchableOpacity onPress={handleTestScan} style={styles.testButton}>
|
||||||
<Ionicons name="bug" size={24} color="#fff" />
|
<Ionicons name="bug" size={24} color="#fff" />
|
||||||
{enableTorch}
|
|
||||||
|
|
||||||
|
{/* the image icon for opening album/gallery */}
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
|
<TouchableOpacity onPress={handleImagePicker} style={styles.galleryButton}>
|
||||||
|
<Ionicons name="image" size={24} color="#fff" />
|
||||||
|
</TouchableOpacity>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
{/* The CONTENT , the popup for the scanned data */}
|
{/* The CONTENT , the popup for the scanned data */}
|
||||||
@@ -245,9 +259,6 @@ const QRScannerScreen: React.FC<QRScannerScreenProps> = ({ clearScanData }) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//--------------------The CSS----------------//
|
//--------------------The CSS----------------//
|
||||||
const styles = StyleSheet.create({
|
const styles = StyleSheet.create({
|
||||||
container: {
|
container: {
|
||||||
@@ -363,8 +374,20 @@ const styles = StyleSheet.create({
|
|||||||
backgroundColor: '#000',
|
backgroundColor: '#000',
|
||||||
padding: 10,
|
padding: 10,
|
||||||
borderRadius: 5,
|
borderRadius: 5,
|
||||||
// Ensure the button appears above other elements
|
// Ensure the button appears above other elements
|
||||||
}
|
},
|
||||||
|
galleryButton: {
|
||||||
|
position: 'absolute',
|
||||||
|
bottom: 20,
|
||||||
|
right: 100, // Adjust this value to move it more or less to the right
|
||||||
|
width: 50,
|
||||||
|
height: 50,
|
||||||
|
justifyContent: 'center',
|
||||||
|
alignItems: 'center',
|
||||||
|
backgroundColor: '#000',
|
||||||
|
borderRadius: 25, // Half of width and height to make it a circle
|
||||||
|
},
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default QRScannerScreen;
|
export default QRScannerScreen;
|
||||||
|
|||||||
Reference in New Issue
Block a user