public bool CreateImagePreview(string mediaId) { bool result = false; string photoNamePreview = ImagePathManager.GetMediaFilename(mediaId, MediaObjectTypeEnum.Image, true); string pathToOriginal = ImagePathManager.GetImagePath(mediaId, MediaObjectTypeEnum.Image, false); string pathToPreview = ImagePathManager.GetImagePath(mediaId, MediaObjectTypeEnum.Image, true); try { byte[] originalByteArray = readFileToByteArray(pathToOriginal); if (originalByteArray.Length > 0) { ImagePreviewManager previewManager = new ImagePreviewManager(); var mediaService = DependencyService.Get <IMediaService>(); byte[] imgPreviewByteArray = previewManager.GetPreviewImage(mediaService, originalByteArray, _width, _height, _quality); File.WriteAllBytes(pathToPreview, imgPreviewByteArray); result = true; } } catch (Exception e) { Crashes.TrackError(e, new Dictionary <string, string> { { "MediaId", "ImgPreview" }, { mediaId, photoNamePreview } }); } return(result); }
public (bool getMetadataPhotoResult, Model.GpsCoordinates imageGpsCoordinates) GetPhoto(string mediaId, string photoFullPath, bool IsPreview = true) { bool getMetadataPhotoResult = false; Model.GpsCoordinates imageInfo = new Model.GpsCoordinates(); string imgPathDirectory = ImagePathManager.GetPicturesDirectory(); FileInfo originalFileInfo = new FileInfo(photoFullPath); ImagePreviewManager preview = new ImagePreviewManager(); preview.PreviewQualityType = _previewImageQuality; if (preview.CreateImagePreview(originalFileInfo.DirectoryName, originalFileInfo.Name, imgPathDirectory, ImagePathManager.GetMediaFilename(mediaId, MediaObjectTypeEnum.Image, IsPreview))) { ExifManager exif = new ExifManager(); imageInfo = exif.GetCoordinates(photoFullPath); getMetadataPhotoResult = true; } else { Analytics.TrackEvent("ImageManager: error create image for auto route", new Dictionary <string, string> { { "mediaId", mediaId }, { "quality", preview.PreviewQualityType.ToString() } }); } return(getMetadataPhotoResult, imageInfo); }
public void Delete(string mediaId, MediaObjectTypeEnum mediaType) { string mediaPath = ImagePathManager.GetMediaFilename(mediaId, mediaType, false); string mediaPreviewPath = ImagePathManager.GetMediaFilename(mediaId, mediaType, true); try { string fileToDelete = Path.Combine(_pictureDir, mediaPath); if (File.Exists(fileToDelete)) { File.Delete(fileToDelete); } } catch (Exception) { } try { string fileToDelete = Path.Combine(_pictureDir, mediaPreviewPath); if (File.Exists(fileToDelete)) { File.Delete(fileToDelete); } } catch (Exception) { } }
public void TryDeleteFile(string routePointMediaObjectId, MediaObjectTypeEnum mediaType, bool isPreview = false) { string filename = ImagePathManager.GetImagePath(routePointMediaObjectId, mediaType, isPreview); if (File.Exists(filename)) { try { File.Delete(filename); } catch (Exception e) { HandleError.Process("RoutePointMediaObjectManager", "TryDeleteFile", e, false); } } }
public IEnumerable <FileInfo> GetTrackFilesFromDirectory() { IEnumerable <FileInfo> files = new List <FileInfo>(); DirectoryInfo directory = new DirectoryInfo(ImagePathManager.GetTracksDirectory()); try { files = directory.GetFiles("*") .Where(f => f.Name.EndsWith(".kml") || f.Name.EndsWith(".gpx")); } catch (Exception e) { HandleError.Process("TrackFileManager", "GetMediaFilesFromDirectory", e, false, directory.FullName); } return(files); }
internal void UpdateMetadata(string pathToImageDirectory) { var startDate = DateTime.Now; int countFiles = 0; var files = _cacheManager.LocalFilesByDays(_dateBegin, _dateEnd, pathToImageDirectory); foreach (var currentFile in files) { if (!currentFile.Processed) { _imageManager.SetPreviewImageQuality(ImageQualityType.MiddleSizeHiQuality); var currentMetadata = _imageManager.GetPhoto(currentFile.Id, Path.Combine(currentFile.SourcePath, currentFile.SourceFileName)); if (currentMetadata.getMetadataPhotoResult) { currentFile.Latitude = currentMetadata.imageGpsCoordinates.Latitude; currentFile.Longitude = currentMetadata.imageGpsCoordinates.Longitude; currentFile.ImagePreviewFileName = ImagePathManager.GetImagePath(currentFile.Id, LocalDB.Model.MediaObjectTypeEnum.Image, true); } currentFile.Processed = true; _cacheManager.Save(currentFile); if (countFiles % 10 == 0) { Xamarin.Forms.MessagingCenter.Send <CurrentProgressIndexMessage>(new CurrentProgressIndexMessage() { Index = countFiles }, string.Empty); } countFiles++; } } var delay = DateTime.Now - startDate; Analytics.TrackEvent("ImagesCacheDb:Update metadata", new Dictionary <string, string> { { "delay", delay.ToString() }, { "dateBegin", _dateBegin.ToString(DateTimeFormatInfo.InvariantInfo) }, { "dateEnd", _dateEnd.ToString(DateTimeFormatInfo.InvariantInfo) }, { "countFiles", files.Count().ToString() } }); }
public async Task <(bool result, string newMediaId)> TakePhotoAsync(double latitude, double longitude) { bool takePhotoResult = false; string mediaId = Guid.NewGuid().ToString(); string photoName = ImagePathManager.GetMediaFilename(mediaId, MediaObjectTypeEnum.Image); MediaFile file; PermissionManager permissions = new PermissionManager(); if (await permissions.PermissionGrantedAsync(Plugin.Permissions.Abstractions.Permission.Photos, CommonResource.RoutePoint_RightNeedToTakePhoto)) { try { file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions { Location = new Plugin.Media.Abstractions.Location() { Latitude = latitude, Longitude = longitude, Timestamp = DateTime.Now }, Directory = string.Empty, Name = photoName, SaveToAlbum = true, PhotoSize = PhotoSize.Full, CompressionQuality = 40//так же соответствует параметрам качества при выборе фото }); takePhotoResult = file != null; } catch (Exception e) { HandleError.Process("ImageManager", "TakePhoto", e, false); } } return(takePhotoResult, mediaId); }
public async Task <(bool pickPhotoResult, string newMediaId, Model.GpsCoordinates imageGpsCoordinates)> PickPhotoAsync() { bool pickPhotoResult = false; Model.GpsCoordinates imageGpsCoordinates = new Model.GpsCoordinates(); string mediaId = Guid.NewGuid().ToString(); await CrossMedia.Current.Initialize(); if (CrossMedia.Current.IsPickPhotoSupported) { MediaFile photoPicked = null; PermissionManager permissions = new PermissionManager(); if (await permissions.PermissionGrantedAsync(Plugin.Permissions.Abstractions.Permission.Photos, CommonResource.RoutePoint_RightNeedToPickPhoto)) { try { photoPicked = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions() { SaveMetaData = true, }); } catch (Exception e) { photoPicked = null; HandleError.Process("ImageManager", "PickPhoto", e, false); } } if (photoPicked != null) { string imgPathDirectory = ImagePathManager.GetPicturesDirectory(); //используем метод создания превью для того, чтобы сделать основное фото из оригинального, но с уменьшенным качеством ImagePreviewManager resizedOriginal = new ImagePreviewManager(); resizedOriginal.PreviewQualityType = ImageQualityType.OriginalSizeLowQuality; FileInfo originalFileInfo = new FileInfo(photoPicked.Path); if (resizedOriginal.CreateImagePreview(originalFileInfo.DirectoryName, originalFileInfo.Name, imgPathDirectory, ImagePathManager.GetMediaFilename(mediaId, MediaObjectTypeEnum.Image, false))) { ImagePreviewManager preview = new ImagePreviewManager(); preview.PreviewQualityType = _previewImageQuality; if (preview.CreateImagePreview(originalFileInfo.DirectoryName, originalFileInfo.Name, imgPathDirectory, ImagePathManager.GetMediaFilename(mediaId, MediaObjectTypeEnum.Image, true))) { ExifManager exif = new ExifManager(); imageGpsCoordinates = exif.GetCoordinates(photoPicked.Path); if ((Double.IsNaN(imageGpsCoordinates.Latitude)) || (Double.IsNaN(imageGpsCoordinates.Longitude))) { //ToDo:Затычка, по другому не назвать. //По какой то причине перестало работать получение координат при выборе фото, связываю это либо с апдейтом компонента CrossMedia либо Huawei галереи //storage/emulated/0/DCIM/Camera/IMG_20210503_230047.jpg - координаты читаются //storage/emulated/0/Android/data/com.sd.goshdebug/files/Pictures/temp/IMG_20210503_230047.jpg - уже нет imageGpsCoordinates = exif.GetCoordinates(Path.Combine("/storage/emulated/0/DCIM/Camera", originalFileInfo.Name)); } pickPhotoResult = true; } else { Analytics.TrackEvent("ImageManager: add photo error create preview ", new Dictionary <string, string> { { "mediaId", mediaId } }); } } else { Analytics.TrackEvent("ImageManager: error resize photo ", new Dictionary <string, string> { { "mediaId", mediaId } }); } } } return(pickPhotoResult, mediaId, imageGpsCoordinates); }