Lesson 2 of 10
Camera - Photo, Video & QR Scanning
The camera is used in three main patterns: taking a photo (profile photo, document scan), recording video (stories, content creation), and scanning (QR codes, barcodes). expo-camera and expo-image-picker cover all three.
Installation
npx expo install expo-camera expo-image-pickerFull Camera Screen with Capture & QR Scan
import { CameraView, CameraType, useCameraPermissions, BarcodeScanningResult } from 'expo-camera';
import { useState, useRef } from 'react';
import { View, Text, Pressable, StyleSheet } from 'react-native';
type Mode = 'photo' | 'qr';
export function CameraScreen({ onCapture, onQRScan }: { onCapture: (uri: string) => void; onQRScan: (data: string) => void }) {
const [permission, requestPermission] = useCameraPermissions();
const [facing, setFacing] = useState<CameraType>('back');
const [mode, setMode] = useState<Mode>('photo');
const [isCapturing, setIsCapturing] = useState(false);
const [scanned, setScanned] = useState(false);
const cameraRef = useRef<CameraView>(null);
// ---- Permission gate ----
if (!permission) return <View style={s.screen}><Text style={s.text}>Loading...</Text></View>;
if (!permission.granted) {
return (
<View style={s.screen}>
<Text style={s.text}>Camera access is required</Text>
<Pressable onPress={requestPermission} style={s.btn}>
<Text style={s.btnText}>Grant Permission</Text>
</Pressable>
</View>
);
}
// ---- Capture photo ----
async function takePhoto() {
if (!cameraRef.current || isCapturing) return;
setIsCapturing(true);
try {
const photo = await cameraRef.current.takePictureAsync({
quality: 0.8, // balance quality vs file size
skipProcessing: true, // faster on Android (skips EXIF rotation fix - handle manually)
});
if (photo) onCapture(photo.uri);
} finally {
setIsCapturing(false);
}
}
// ---- QR scan handler ----
function handleBarcode({ type, data }: BarcodeScanningResult) {
if (scanned) return;
setScanned(true);
onQRScan(data);
// Allow scanning again after 3 seconds
setTimeout(() => setScanned(false), 3000);
}
return (
<View style={s.screen}>
<CameraView
ref={cameraRef}
style={StyleSheet.absoluteFillObject}
facing={facing}
// QR scanning - only active in qr mode
barcodeScannerSettings={mode === 'qr' ? { barcodeTypes: ['qr', 'ean13', 'code128'] } : undefined}
onBarcodeScanned={mode === 'qr' ? handleBarcode : undefined}
/>
{/* QR overlay */}
{mode === 'qr' && (
<View style={s.qrOverlay}>
<View style={s.qrFrame} />
<Text style={s.qrHint}>{scanned ? 'β Scanned!' : 'Point at a QR code'}</Text>
</View>
)}
{/* Controls */}
<View style={s.controls}>
{/* Mode toggle */}
<View style={s.modeRow}>
{(['photo', 'qr'] as Mode[]).map(m => (
<Pressable key={m} onPress={() => setMode(m)} style={[s.modeBtn, mode === m && s.modeBtnActive]}>
<Text style={[s.modeBtnText, mode === m && s.modeBtnTextActive]}>
{m === 'photo' ? 'π· Photo' : 'π² Scan'}
</Text>
</Pressable>
))}
</View>
{/* Shutter / Flip */}
<View style={s.shutterRow}>
<Pressable onPress={() => setFacing(f => f === 'back' ? 'front' : 'back')} style={s.flipBtn}>
<Text style={{ fontSize: 24 }}>π</Text>
</Pressable>
{mode === 'photo' && (
<Pressable onPress={takePhoto} disabled={isCapturing} style={[s.shutter, isCapturing && { opacity: 0.6 }]}>
<View style={s.shutterInner} />
</Pressable>
)}
</View>
</View>
</View>
);
}
const s = StyleSheet.create({
screen: { flex: 1, backgroundColor: '#000', alignItems: 'center', justifyContent: 'center' },
text: { color: 'white', fontSize: 16, marginBottom: 16 },
btn: { backgroundColor: '#229c6b', padding: 14, borderRadius: 10 },
btnText: { color: 'white', fontWeight: '700' },
qrOverlay: { ...StyleSheet.absoluteFillObject, alignItems: 'center', justifyContent: 'center' },
qrFrame: { width: 220, height: 220, borderWidth: 2, borderColor: '#229c6b', borderRadius: 12 },
qrHint: { color: 'white', marginTop: 20, fontSize: 16, backgroundColor: '#00000066', padding: 8, borderRadius: 8 },
controls: { position: 'absolute', bottom: 0, left: 0, right: 0, paddingBottom: 48 },
modeRow: { flexDirection: 'row', justifyContent: 'center', gap: 12, marginBottom: 24 },
modeBtn: { paddingHorizontal: 20, paddingVertical: 8, borderRadius: 20, backgroundColor: '#ffffff33' },
modeBtnActive: { backgroundColor: '#229c6b' },
modeBtnText: { color: '#ffffff99', fontWeight: '600' },
modeBtnTextActive: { color: 'white' },
shutterRow: { flexDirection: 'row', justifyContent: 'center', alignItems: 'center', gap: 40 },
flipBtn: { width: 48, height: 48, alignItems: 'center', justifyContent: 'center' },
shutter: { width: 72, height: 72, borderRadius: 36, borderWidth: 4, borderColor: 'white', alignItems: 'center', justifyContent: 'center' },
shutterInner: { width: 58, height: 58, borderRadius: 29, backgroundColor: 'white' },
});Tip
Tip
Practice Camera Photo Video QR Scanning in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Integrating expo-camera with real-time barcode scanning and worklets
Practice Task
Note
Practice Task - (1) Write a working example of Camera Photo Video QR Scanning from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Camera Photo Video QR Scanning is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?