From 7e4d59b02e04bf1cfb4df8c28d85e1ea1fea876a Mon Sep 17 00:00:00 2001 From: Randall Stillwell Date: Tue, 22 Jul 2025 09:16:19 -0500 Subject: [PATCH] Fix camera UI visibility issue with enhanced debugging --- src/components/CameraScanner.tsx | 64 ++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/src/components/CameraScanner.tsx b/src/components/CameraScanner.tsx index f626ca0..37ef9dd 100644 --- a/src/components/CameraScanner.tsx +++ b/src/components/CameraScanner.tsx @@ -18,6 +18,7 @@ const CameraScanner: React.FC = ({ onCardScanned, onError }) const [isProcessing, setIsProcessing] = useState(false); const [scanResult, setScanResult] = useState(null); const [capturedImage, setCapturedImage] = useState(null); + const [debugInfo, setDebugInfo] = useState('Camera not started'); const videoRef = useRef(null); const canvasRef = useRef(null); @@ -25,6 +26,8 @@ const CameraScanner: React.FC = ({ onCardScanned, onError }) // Start camera stream const startCamera = async () => { + setDebugInfo('🔄 Requesting camera access...'); + try { const stream = await navigator.mediaDevices.getUserMedia({ video: { @@ -34,14 +37,36 @@ const CameraScanner: React.FC = ({ onCardScanned, onError }) } }); + setDebugInfo('✅ Camera stream obtained'); + if (videoRef.current) { videoRef.current.srcObject = stream; streamRef.current = stream; - setIsStreaming(true); + + // Wait for video to be ready + videoRef.current.onloadedmetadata = () => { + setDebugInfo('🎥 Video metadata loaded, starting playback...'); + videoRef.current?.play().then(() => { + setIsStreaming(true); + setDebugInfo('✅ Camera streaming successfully'); + }).catch((err) => { + setDebugInfo(`❌ Video play failed: ${err.message}`); + onError(`Video playback failed: ${err.message}`); + }); + }; + + videoRef.current.onerror = (err) => { + setDebugInfo(`❌ Video error: ${err}`); + onError('Video element error occurred'); + }; + } else { + setDebugInfo('❌ Video ref is null'); + onError('Video element not available'); } - } catch (err) { + } catch (err: any) { console.error('Camera access error:', err); - onError('Unable to access camera. Please check permissions.'); + setDebugInfo(`❌ Camera error: ${err.message}`); + onError(`Unable to access camera: ${err.message}`); } }; @@ -54,6 +79,7 @@ const CameraScanner: React.FC = ({ onCardScanned, onError }) setIsStreaming(false); setCapturedImage(null); setScanResult(null); + setDebugInfo('Camera stopped'); }; // Capture image from video stream @@ -214,7 +240,7 @@ const CameraScanner: React.FC = ({ onCardScanned, onError }) {/* Camera Preview */} - {isStreaming && ( + {(isStreaming || streamRef.current) && (
)} + {/* Fallback: Always show video element for debugging */} + {!isStreaming && streamRef.current && ( +
+
🔍 Debug: Stream exists but not marked as streaming
+
+
+
+ )} + {/* Hidden canvas for image capture */} @@ -312,6 +354,20 @@ const CameraScanner: React.FC = ({ onCardScanned, onError })
  • • Works best with English cards
  • + + {/* Debug Info */} +
    +
    Camera Status:
    +

    {debugInfo}

    +
    +
    isStreaming: {isStreaming ? '✅ true' : '❌ false'}
    +
    videoRef.current: {videoRef.current ? '✅ exists' : '❌ null'}
    +
    streamRef.current: {streamRef.current ? '✅ exists' : '❌ null'}
    + {videoRef.current && ( +
    Video ready state: {videoRef.current.readyState}
    + )} +
    +
    ); };