Ejemplo n.º 1
0
        public async Task <string> SaveTaggedImageAsync(string fileName, FileTaggingInformation taggingInformation,
                                                        byte[] image)
        {
            var imagesFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            var taggedFolder = Path.Combine(imagesFolder, "Tagged");
            var filePath     = Path.Combine(taggedFolder, fileName);

            try
            {
                var imageData = await GetTaggedImageFromUriAsync(taggingInformation, image);

                if (!Directory.Exists(taggedFolder))
                {
                    Directory.CreateDirectory(taggedFolder);
                }

                var data    = NSData.FromArray(imageData);
                var uiImage = UIImage.LoadFromData(data);

                NSData  imgData = uiImage.AsJPEG();
                NSError err     = null;
                imgData.Save(filePath, false, out err);
            }
            catch (Exception ex)
            {
            }

            return(string.Empty);
        }
Ejemplo n.º 2
0
        private async Task <byte[]> GetTaggedImageFromUriAsync(FileTaggingInformation taggingInformation, byte[] image)
        {
            var client = new HttpClient();

            var url = $"{CoreConstants.TaggingServiceUrl}";

            var content = new MultipartContent();

            var payload = JsonConvert.SerializeObject(taggingInformation);

            var stringContent = new StringContent(payload);

            stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var byteContent = new ByteArrayContent(image);

            byteContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");

            content.Add(stringContent);
            content.Add(byteContent);

            var response = await client.PostAsync(url, content);

            var result = await response.Content.ReadAsByteArrayAsync();

            return(result);
        }
Ejemplo n.º 3
0
        public async Task <string> SaveTaggedImageAsync(string fileName, FileTaggingInformation taggingInformation, byte[] image)
        {
            var filePath = string.Empty;

            try
            {
                var imageData = await GetTaggedImageFromUriAsync(taggingInformation, image);

                var imagesFolder = KnownFolders.PicturesLibrary;

                var taggedFolder = await imagesFolder.CreateFolderAsync("Tagged", CreationCollisionOption.OpenIfExists);

                var file = await taggedFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

                await FileIO.WriteBytesAsync(file, imageData);

                filePath = file.Path;
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"ImageService.SaveTaggedImageAsync Exception: {ex}");
            }

            return(filePath);
        }
        public async Task <string> SaveTaggedImageAsync(string fileName, FileTaggingInformation taggingInformation,
                                                        byte[] image)
        {
            var imagesFolder = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDcim);
            var imagesPath   = imagesFolder.AbsolutePath;
            var taggedFolder = Path.Combine(imagesPath, "Tagged");

            var filePath = Path.Combine(taggedFolder, fileName);

            try
            {
                var imageData = await GetTaggedImageFromUriAsync(taggingInformation, image);

                if (!Directory.Exists(taggedFolder))
                {
                    Directory.CreateDirectory(taggedFolder);
                }

                File.WriteAllBytes(filePath, imageData);

                var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
                mediaScanIntent.SetData(Uri.FromFile(new Java.IO.File(filePath)));
                Forms.Context.SendBroadcast(mediaScanIntent);
            }
            catch (Exception ex)
            {
            }

            return(string.Empty);
        }
        // POST: api/Tagit
        public async Task <HttpResponseMessage> Post()
        {
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

            try
            {
                Stream stream = null;

                FileTaggingInformation tagInformation = null;

                var contents = await Request.Content.ReadAsMultipartAsync();

                foreach (var contentPart in contents.Contents)
                {
                    if (contentPart.Headers.ContentType.MediaType == "application/json")
                    {
                        var payload = await contentPart.ReadAsStringAsync();

                        tagInformation = Newtonsoft.Json.JsonConvert.DeserializeObject <FileTaggingInformation>(payload);
                    }
                    else if (contentPart.Headers.ContentType.MediaType == "image/jpeg")
                    {
                        stream = await contentPart.ReadAsStreamAsync();
                    }
                }

                if (tagInformation != null)
                {
                    byte[] taggedFile = Helpers.ImageHelper.UpdateImageProperties(stream, tagInformation?.Caption, tagInformation?.Tags, tagInformation.Rating);

                    MemoryStream dataStream = new MemoryStream(taggedFile);

                    response.Content = new StreamContent(dataStream);
                    response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
                }
                else
                {
                    response = new HttpResponseMessage(HttpStatusCode.BadRequest)
                    {
                        Content = new StringContent("The image contents could not be determined or read. Confirm image is valid or try a different image.")
                    };
                }
            }
            catch (Exception ex)
            {
                response = new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)
                {
                    Content = new StringContent(ex.Message)
                };
            }

            return(response);
        }
Ejemplo n.º 6
0
        public async Task <string> SaveTaggedImageAsync(string fileName, FileTaggingInformation taggingInformation, byte[] image)
        {
            if (!await tagit.Droid.Permissions.PermissionsHelper.RequestStorageAccess())
            {
                return(string.Empty);
            }

            var imagesFolder = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim);

            var imagesPath = imagesFolder.AbsolutePath;

            var taggedFolder = Path.Combine(imagesPath, "Tagged");

            var filePath = Path.Combine(taggedFolder, fileName);

            try
            {
                var imageData = await GetTaggedImageFromUriAsync(taggingInformation, image);

                if (!Directory.Exists(taggedFolder))
                {
                    Directory.CreateDirectory(taggedFolder);
                }

                File.WriteAllBytes(filePath, imageData);

                var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);

                mediaScanIntent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(filePath)));

                Forms.Context.SendBroadcast(mediaScanIntent);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"ImageService.SaveTaggedImageAsync Exception: {ex}");
            }

            return(string.Empty);
        }