import { CameraView, useCameraPermissions } from 'expo-camera'; import { useEffect, useRef, useState } from 'react'; import { Dimensions, Pressable, ScrollView, StyleSheet, Text, View } from 'react-native'; import ChooseCarForChargingRow from '../../../../component/global/chooseCarForChargingRow'; import { CrossLogoWhiteSvg, QuestionSvg } from '../../../../component/global/SVG'; import { router } from 'expo-router'; const ScanQrPage = () => { const [permission, requestPermission] = useCameraPermissions(); const [scanned, setScanned] = useState(false); const viewRef = useRef(null); useEffect(() => { (async () => { const { status } = await requestPermission(); if (status !== 'granted') { alert('Please grant camera permission to use the QR scanner'); } })(); }, []); if (!permission) { return ; } if (!permission.granted) { return ( We need your permission to access the camera QR code function. Please go to your device settings and enable camera permissions for this application. ); } const { width: screenWidth, height: screenHeight } = Dimensions.get('window'); const handleBarCodeScanned = ({ bounds, data, type }) => { const { origin, size } = bounds; // Calculate the size of the square transparent area const transparentAreaSize = Math.min( screenWidth * 0.6, screenHeight * 0.3 ); const transparentAreaX = (screenWidth - transparentAreaSize) / 2; const transparentAreaY = (screenHeight - transparentAreaSize) / 2; // Check if the barcode is within the transparent area if ( origin.x >= transparentAreaX && origin.y >= transparentAreaY && origin.x + size.width <= transparentAreaX + transparentAreaSize && origin.y + size.height <= transparentAreaY + transparentAreaSize ) { setScanned(true); console.log(` type: ${type} data: ${data} `); if (type === 'qr') { console.log('QR Code link:', data); } setTimeout(() => { setScanned(false); }, 5000); } }; const dummyDataChooseCarForCharging = [ { imageUrl: require('../../../../assets/car1.png'), VehicleName: 'TESLA - Model 3', isDefault: true }, { VehicleName: 'TESLA - Model Y', isDefault: false }, { imageUrl: require('../../../../assets/car1.png'), VehicleName: 'TESLA - Model X', isDefault: false }, { VehicleName: 'TESLA - Model 3', isDefault: false } ]; const ChooseCar = () => { const isLargeScreen = screenHeight >= 800; return ( { if (router.canGoBack()) { router.back(); } else { router.replace('mainPage'); } }} > 選擇充電車輛 {dummyDataChooseCarForCharging.map((car, index) => ( ))} ); }; return ( 請掃瞄充電座上的二維碼 console.log('需要協助?')}> 需要協助? ); }; const styles = StyleSheet.create({ container: { flex: 1 }, camera: { flex: 1 }, overlay: { flex: 1 }, topOverlay: { flex: 35, alignItems: 'center', backgroundColor: 'rgba(0,0,0,0.5)' }, centerRow: { flex: 30, flexDirection: 'row' }, leftOverlay: { flex: 20, backgroundColor: 'rgba(0,0,0,0.5)' }, transparentArea: { flex: 60, aspectRatio: 1, position: 'relative' }, rightOverlay: { flex: 20, backgroundColor: 'rgba(0,0,0,0.5)' }, bottomOverlay: { flex: 35, backgroundColor: 'rgba(0,0,0,0.5)' } }); export default ScanQrPage;