BNA

BNA UI
26

Camera Preview

A comprehensive camera component with capture, preview, and media management capabilities.

Installation

pnpm dlx bna-ui add camera-preview

Usage

import { CameraPreview } from '@/components/ui/camera-preview';
<CameraPreview />

Examples

Default

Features

Camera Capabilities

  • Photo Capture: High-quality photo capture with multiple resolution options
  • Video Recording: Full video recording with audio support
  • Flash Control: Built-in torch/flash toggle functionality
  • Camera Switching: Front and back camera switching
  • Focus Control: Tap-to-focus functionality

Media Management

  • Preview Mode: Full-screen preview of captured media
  • Save to Gallery: Direct save to device photo library
  • Custom Upload: Configurable upload handling
  • Media Processing: Ready for custom processing workflows

User Experience

  • Responsive Design: Adapts to different screen sizes
  • Theme Support: Automatic light/dark theme support
  • Permission Handling: Graceful permission request flows
  • Error Handling: Comprehensive error handling and user feedback

API Reference

CameraPreview

The main camera preview component that provides a complete camera experience.

PropTypeDefaultDescription
onUpload(media: MediaDetails) => void-Custom upload handler for captured media
onSave(media: MediaDetails) => void-Custom save handler for captured media
facing'front' | 'back''back'Default camera facing direction
enableTorchbooleantrueEnable flash/torch functionality
enableVideobooleantrueEnable video recording
showControlsbooleantrueShow camera control buttons
maxDurationnumber60Maximum video recording duration in seconds
quality'low' | 'medium' | 'high' | 'max''high'Photo/video quality setting
styleViewStyle-Additional styles for the container
cameraStyleViewStyle-Additional styles for the camera view
buttonStyleViewStyle-Additional styles for control buttons
autoSavebooleanfalseAutomatically save to gallery after capture
showPreviewbooleantrueShow preview screen after capture

MediaDetails

The media object structure returned by capture events.

interface MediaDetails {
  uri: string;
  type: 'picture' | 'video';
  timestamp: string;
  width?: number;
  height?: number;
  duration?: number;
  size?: number;
}

Events

EventTypeDescription
onCapture(media: MediaDetails) => voidFired when media is captured
onError(error: Error) => voidFired when an error occurs
onPermission(granted: boolean) => voidFired when permissions are requested

Permissions

The Camera Preview component requires the following permissions:

iOS

  • Camera: Required for photo and video capture
  • Microphone: Required for video recording with audio
  • Photo Library: Required for saving media to gallery

Android

  • CAMERA: Required for photo and video capture
  • RECORD_AUDIO: Required for video recording with audio
  • WRITE_EXTERNAL_STORAGE: Required for saving media
  • READ_EXTERNAL_STORAGE: Required for accessing saved media

Best Practices

Performance

  • Use appropriate quality settings for your use case
  • Implement proper cleanup when component unmounts
  • Handle memory management for large media files
  • Use compression for uploaded media when appropriate

User Experience

  • Always request permissions gracefully
  • Provide clear feedback during capture and processing
  • Implement proper loading states
  • Handle edge cases like low storage space

Security

  • Validate uploaded media on your backend
  • Implement proper file type checking
  • Consider implementing media scanning for inappropriate content
  • Use secure upload endpoints with proper authentication

Accessibility

The Camera Preview component is built with accessibility in mind:

  • Screen reader support for all interactive elements
  • High contrast mode support
  • Voice-over announcements for capture events
  • Keyboard navigation support where applicable
  • Proper focus management throughout the interface

Troubleshooting

Common Issues

Camera not working on iOS simulator

  • The iOS simulator doesn't support camera functionality
  • Test on a physical device for full functionality

Permission denied errors

  • Ensure permissions are properly configured in app.json
  • Check that users have granted necessary permissions
  • Implement graceful fallbacks for denied permissions

Media not saving to gallery

  • Verify Media Library permissions are granted
  • Check available storage space
  • Ensure proper error handling is implemented

Video recording issues

  • Verify microphone permissions for audio recording
  • Check maximum duration settings
  • Monitor memory usage during long recordings