camera_with_gps 1.0.2
camera_with_gps: ^1.0.2 copied to clipboard
A Flutter plugin for capturing photos with embedded GPS metadata.
Camera With GPS Flutter Plugin #
A Flutter plugin for capturing photos with embedded GPS metadata. It provides a full-screen camera experience and automatically tags photos with the device's location (latitude and longitude).
Overview #
CameraWithGps offers:
- Full-screen camera UI: For high-resolution photo capture.
- Automatic GPS metadata: Embeds latitude and longitude into photos.
- Graceful GPS handling: Works even when GPS is disabled, showing a warning message.
- Cross-platform support: Works on both iOS and Android.
- Lightweight & efficient: Easy integration into your Flutter projects.
Installation #
-
Add the dependency to your
pubspec.yamlfile:dependencies: camera_with_gps: ^1.0.0 -
Install the package by running:
flutter pub get
Platform Setup #
Android #
-
Permissions: Add the following to your
AndroidManifest.xml:<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.location.gps" /> -
Minimum SDK: Ensure your
android/app/build.gradleincludes:android { defaultConfig { minSdkVersion 21 // Ensure minSdkVersion is 21 or higher ... } } -
Runtime Permissions: Make sure your app requests location permissions during runtime as needed.
iOS #
-
Info.plist: Add these keys for camera and location usage:
<key>NSCameraUsageDescription</key> <string>This app requires camera access to take photos.</string> <key>NSLocationWhenInUseUsageDescription</key> <string>This app requires location access to add GPS data to photos.</string> -
Image I/O Framework: Ensure it is included in your project.
Usage #
Importing the Plugin #
Include the package in your Dart file:
import 'package:camera_with_gps/camera_with_gps.dart';
Initializing the Plugin #
Assign the plugin’s navigatorKey to your app’s Navigator:
void main() {
runApp(MaterialApp(
navigatorKey: CameraWithGps.navigatorKey,
home: const MyApp(),
));
}
Opening the Camera #
Launch the camera UI and capture a photo:
Future<void> openCamera() async {
try {
final photoPath = await CameraWithGps.openCamera(context);
if (photoPath != null) {
print('Photo saved at: $photoPath');
}
} catch (e) {
print('Failed to open camera: $e');
}
}
Note: The plugin now allows taking photos even when GPS is disabled. In this case, a warning message will be displayed to the user, but they can still capture photos (without GPS metadata).
Embedding GPS Metadata #
To manually add GPS metadata, call:
final success = await CameraWithGps.addGps(
path: '/path/to/photo.jpg',
latitude: 37.4219983,
longitude: -122.084,
);
print(success ? "GPS metadata added!" : "Failed to add GPS metadata.");
Structure #
The plugin consists of the following components:
- CameraWithGps: Main class handling camera launching and EXIF editing.
- CameraPreviewPage: Provides the full-screen UI for capturing photos.
- CameraWithGpsPlugin (Kotlin): Manages Android EXIF metadata and native communication.
- CameraWithGpsPlugin (Swift): Manages iOS EXIF metadata and native communication.
API Reference #
| Method | Description |
|---|---|
openCamera() |
Opens the camera interface and returns the file path of the captured photo. |
addGps(String, double, double) |
Embeds GPS data (latitude and longitude) into an image file. Returns success/failure. |
Example #
Below is a complete example of using the plugin in a Flutter application:
import 'package:camera_with_gps/camera_with_gps.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
navigatorKey: CameraWithGps.navigatorKey,
home: const MyApp(),
));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
Future<void> _capturePhoto() async {
try {
final path = await CameraWithGps.openCamera();
if (path != null) {
print('Photo captured at: $path');
} else {
print('Capture canceled or failed.');
}
} catch (e) {
print('Error: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Camera With GPS Example')),
body: Center(
child: ElevatedButton(
onPressed: _capturePhoto,
child: const Text('Capture Photo'),
),
),
);
}
}
Cross-platform Implementation #
Android #
- GPS Metadata Handling: Uses Android's
ExifInterfacefrom Jetpack. - Key File:
CameraWithGpsPlugin.kthandles native communication and metadata embedding.
iOS #
- GPS Metadata Handling: Uses the
Image I/Oframework. - Key File:
CameraWithGpsPlugin.swifthandles native communication and metadata embedding.
Contributing #
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your enhancements or corrections.
- Submit a pull request.
License #
Released under the MIT License. Feel free to use, modify, and distribute.
Developed and maintained by Ruslan Madzhara.
Changelog #
For detailed version history, see CHANGELOG.md.