|
| 1 | +--- |
| 2 | +title: Camera |
| 3 | +order: 200 |
| 4 | +--- |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +The Camera API provides access to the device's camera for taking photos and selecting images from the gallery. |
| 9 | + |
| 10 | +```php |
| 11 | +use Native\Mobile\Facades\Camera; |
| 12 | +``` |
| 13 | + |
| 14 | +## Methods |
| 15 | + |
| 16 | +### `getPhoto()` |
| 17 | + |
| 18 | +Opens the camera interface to take a photo. |
| 19 | + |
| 20 | +```php |
| 21 | +Camera::getPhoto(); |
| 22 | +``` |
| 23 | + |
| 24 | +### `pickImages()` |
| 25 | + |
| 26 | +Opens the gallery/photo picker to select existing images. |
| 27 | + |
| 28 | +**Parameters:** |
| 29 | +- `string $media_type` - Type of media to pick: `'all'`, `'images'`, `'videos'` (default: `'all'`) |
| 30 | +- `bool $multiple` - Allow multiple selection (default: `false`) |
| 31 | + |
| 32 | +**Returns:** `bool` - `true` if picker opened successfully |
| 33 | + |
| 34 | +```php |
| 35 | +// Pick a single image |
| 36 | +Camera::pickImages('images', false); |
| 37 | + |
| 38 | +// Pick multiple images |
| 39 | +Camera::pickImages('images', true); |
| 40 | + |
| 41 | +// Pick any media type |
| 42 | +Camera::pickImages('all', true); |
| 43 | +``` |
| 44 | + |
| 45 | +## Events |
| 46 | + |
| 47 | +### `PhotoTaken` |
| 48 | + |
| 49 | +Fired when a photo is taken with the camera. |
| 50 | + |
| 51 | +**Payload:** `string $path` - File path to the captured photo |
| 52 | + |
| 53 | +```php |
| 54 | +use Livewire\Attributes\On; |
| 55 | +use Native\Mobile\Events\Camera\PhotoTaken; |
| 56 | + |
| 57 | +#[On('native:'.PhotoTaken::class)] |
| 58 | +public function handlePhotoTaken(string $path) |
| 59 | +{ |
| 60 | + // Process the captured photo |
| 61 | + $this->processPhoto($path); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### `MediaSelected` |
| 66 | + |
| 67 | +Fired when media is selected from the gallery. |
| 68 | + |
| 69 | +**Payload:** `array $media` - Array of selected media items |
| 70 | + |
| 71 | +```php |
| 72 | +use Livewire\Attributes\On; |
| 73 | +use Native\Mobile\Events\Gallery\MediaSelected; |
| 74 | + |
| 75 | +#[On('native:'.MediaSelected::class)] |
| 76 | +public function handleMediaSelected($success, $files, $count) |
| 77 | +{ |
| 78 | + foreach ($files as $file) { |
| 79 | + // Process each selected media item |
| 80 | + $this->processMedia($file); |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## Notes |
| 86 | + |
| 87 | +- The first time your app requests camera access, users will be prompted for permission |
| 88 | +- If permission is denied, camera functions will fail silently |
| 89 | +- Captured photos are stored in the app's temporary directory |
| 90 | +- File formats are platform-dependent (typically JPEG) |
0 commit comments