Modified handleImagePicker with correct error handling. Able to scan QR code from images and give alert for non QR images
This commit is contained in:
@@ -149,30 +149,42 @@ 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,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result && result.assets[0].uri) {
|
|
||||||
try {
|
//https://medium.com/@funti009/create-a-mobile-qr-scanner-that-scans-via-camera-and-image-in-the-gallery-react-native-expo-ee7098a265d7
|
||||||
const scannedResults = await scanFromURLAsync(result.assets[0].uri);
|
// Refactored to use Camera.scanFromURLAsync instead
|
||||||
if (scannedResults.length > 0) {
|
// Function to handle QR code scanning from the image picker
|
||||||
const dataNeeded = scannedResults[0].data;
|
|
||||||
handleQRCodeScanned({ type: 'QR_CODE', data: dataNeeded });
|
|
||||||
} else {
|
const handleImagePicker = async () => {
|
||||||
setScannedData("No QR Code Found");
|
clearScanDataInternal();
|
||||||
setTimeout(() => setScannedData(""), 4000);
|
const result = await ImagePicker.launchImageLibraryAsync({
|
||||||
}
|
mediaTypes: ImagePicker.MediaTypeOptions.Images,
|
||||||
} catch (error) {
|
allowsEditing: false, // Disabling the crop functionality
|
||||||
console.error('Error scanning QR code from image:', error);
|
quality: 1,
|
||||||
Alert.alert('Failed to scan QR code from image.');
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//if (result && result.assets[0].uri) { // KIV this....
|
||||||
|
if (result && result.assets && result.assets.length > 0 && result.assets[0].uri) { // this is to unsure the uri is not empty
|
||||||
|
try {
|
||||||
|
const scannedResult = await scanFromURLAsync(result.assets[0].uri);
|
||||||
|
if (!scannedResult.data) { // This will check if no QR was scanned
|
||||||
|
// Not sure why by passing the scannedResults.data is not working , only works when I use scannedResults[0].data..... KIV >.<
|
||||||
|
const dataNeeded = scannedResult[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...
|
// 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
|
||||||
|
|||||||
Reference in New Issue
Block a user