import 'dart:io';
import 'package:exif/exif.dart';
import 'package:fileupload/presentation/view/file_upload_screen.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:image_picker/image_picker.dart';
class ImageViewScreen extends StatefulWidget {
const ImageViewScreen({super.key});
@override
State<ImageViewScreen> createState() => _ImageViewScreenState();
}
class _ImageViewScreenState extends State<ImageViewScreen> {
XFile? _image; //이미지 담을 변수 선언
double latitude = 0;
double longitude = 0;
bool isLocation = false;
@override
void initState() {
super.initState();
getLocationPermission();
}
Future<void> getLocationPermission() async {
LocationPermission permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
}
}
Future<void> getImage(ImageSource imageSource) async {
var picker = ImagePicker();
var pickerImage = await picker.pickImage(source: imageSource);
// 이미지를 읽을 때마다 위치정보 좌표값 초기화 처리
latitude = 0;
longitude = 0;
if (pickerImage != null) {
var metadata = await readExifFromBytes(File(pickerImage.path).readAsBytesSync());
if (metadata.containsKey('GPS GPSLatitude')) {
print("location enabled");
isLocation = true;
getCurrentLocationFromexif(metadata);
} else {
isLocation = false;
print("location disabled");
try {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
latitude = position.latitude;
longitude = position.longitude;
} catch (e) {
print(e);
}
}
// 이미지를 출력하기 위해 상태 변경
setState(() {
_image = XFile(pickerImage.path);
});
}
}
Future<void> getCurrentLocationFromexif(var data) async {
if (data.containsKey('GPS GPSLongitude')) {
final gpsLatitude = data['GPS GPSLatitude'];
final latitudeSignal = data['GPS GPSLatitudeRef']!.printable;
List latitudeRation = gpsLatitude!.values.toList();
List latitudeValue = latitudeRation.map((item) {
return (item.numerator.toDouble() / item.denominator.toDouble());
}).toList();
latitude = latitudeValue[0] + (latitudeValue[1] / 60) +
(latitudeValue[2] / 3600);
if (latitudeSignal == 'S') latitude = -latitude;
print('latitude ::: ${latitude}');
final gpsLongitude = data['GPS GPSLongitude'];
final longitudeSignal = data['GPS GPSLongitude']!.printable;
List longitudeRation = gpsLongitude!.values.toList();
List longitudeValue = longitudeRation.map((item) {
return (item.numerator.toDouble() / item.denominator.toDouble());
}).toList();
longitude = longitudeValue[0] + (longitudeValue[1] / 60) +
(longitudeValue[2] / 3600);
if (longitudeSignal == 'W') longitude = -longitude;
print('longitude ::: ${longitude}');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
_buildPhotoArea(),
if(latitude > 0) _buildLocation(),
],
),
floatingActionButton: Stack(
children: [
Align(
alignment: Alignment(
Alignment.bottomRight.x, Alignment.bottomRight.y - 0.4),
child: FloatingActionButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => const FileUploadScreen(),
));
},
tooltip: 'back',
heroTag: UniqueKey(),
child: const Icon(Icons.arrow_back),
),
),
Align(
alignment: Alignment(
Alignment.bottomRight.x, Alignment.bottomRight.y - 0.2),
child: FloatingActionButton(
onPressed: () async {
getImage(ImageSource.camera);
},
tooltip: 'image',
heroTag: UniqueKey(),
child: const Icon(Icons.camera_alt),
),
),
Align(
alignment: Alignment.bottomRight,
child: FloatingActionButton(
onPressed: () async {
getImage(ImageSource.gallery);
},
tooltip: 'image',
heroTag: UniqueKey(),
child: const Icon(Icons.image),
),
),
],
),
);
}
Widget _buildPhotoArea() {
return _image != null
? Container(
width: 400,
height: 400,
child: Image.file(File(_image!.path)), //가져온 이미지 화면에 띄워주는 코드
)
: const Center(
child: Text("불러온 이미지가 없습니다."),
);
}
Widget _buildLocation() {
return Column(
children: [
Container(
child: Text('GPS Location ::: $isLocation'),
),
Container(
child: Text('위도: ${latitude} ,경도: ${longitude}'),
),
],
);
}
}