Exemplo n.º 1
0
        private async void Button_UploadPhoto(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();

            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                await DisplayAlert("No camera", ":(No camera available.", "ok");

                return;
            }

            var file = await CrossMedia.Current.TakePhotoAsync(
                new StoreCameraMediaOptions
            {
                SaveToAlbum = true,
            }
                );

            if (file == null)
            {
                return;
            }
            int s = await face.searchFirst(file);

            if (s != 3)
            {
                Toast.MakeText(Android.App.Application.Context, s.ToString(), ToastLength.Long).Show();
                return;
            }
            var id = user.UploaderID;

            string s2 = await face.register(file, id);

            if (s2 != "Success")
            {
                Toast.MakeText(Android.App.Application.Context, s2, ToastLength.Long).Show();
                return;
            }

            DateTime dTime = DateTime.Now;
            string   time  = dTime.ToString();

            time = time.Replace(" ", "_") + ".jpg";

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=faceitphotos;AccountKey=67nq3VNJlZ0KJArJZU62vjri4pNzqd1MERWFQytw7w7B6cfTv7Gw75iJq4LJgUN7E05Y0+3ixmkOWDyKk4yhtw==");
            CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer  container      = blobClient.GetContainerReference("userimages");

            CloudBlockBlob blockBlob = container.GetBlockBlobReference(id + "_" + time);

            using (Stream photo = file.GetStream())
            {
                await blockBlob.UploadFromStreamAsync(photo);
            }

            MainImage.Source = ImageSource.FromStream(() =>
            {
                var Stream = file.GetStream();
                file.Dispose();
                return(Stream);
            });


            if (id != 0)
            {
                UserPhotoBindingModel photoModel = new UserPhotoBindingModel();

                photoModel.Link       = "https://faceitphotos.blob.core.windows.net/userimages/" + id + "_" + time;
                photoModel.UploaderID = id;

                try
                {
                    var         json    = JsonConvert.SerializeObject(photoModel);
                    HttpContent content = new StringContent(json);

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

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

                    if (response.IsSuccessStatusCode)
                    {
                        string Message = "Registration Successfull";
                        Toast.MakeText(Android.App.Application.Context, Message, ToastLength.Long).Show();

                        var get1 = response.Content.ReadAsStringAsync().Result;

                        var json2 = JsonConvert.DeserializeObject <UserViewModel>(get1);
                        user = json2;

                        Toast.MakeText(Android.App.Application.Context, "Uploading Image... ", ToastLength.Long).Show();
                    }
                }
                catch (Exception)
                {
                    string Message = "Registration Failed";
                    Toast.MakeText(Android.App.Application.Context, Message, ToastLength.Long).Show();
                }
            }
        }
        public async Task <ActionResult> NewCelebrity(CreateCelebrityViewModel celebrity)
        {
            string             message;
            int                id;
            Face0              faceAPI = new Face0();
            HttpPostedFileBase file    = celebrity.imageBrowes;
            //
            Image        i  = Image.FromStream(file.InputStream, true, true);
            MemoryStream ms = new MemoryStream();

            i.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] imgData = ms.ToArray();

            using (var fileStream = new MemoryStream(imgData))
            {
                int s = await faceAPI.searchFirst(fileStream);

                if (s == 0)
                {
                    ViewBag.Status  = true;
                    ViewBag.Message = "no one detected";
                    return(View());
                }
                else if (s == 1)
                {
                    ViewBag.Status  = true;
                    ViewBag.Message = "more than one person detected";
                    return(View());
                }
                else if (s == 2)
                {
                    ViewBag.Status  = true;
                    ViewBag.Message = "there is an existing person";
                    return(View());
                }
            }
            //
            try
            {
                Celebrity celebrityModel = new Celebrity();

                celebrityModel.FirstName    = celebrity.FirstName;
                celebrityModel.LastName     = celebrity.LastName;
                celebrityModel.Gender       = celebrity.Gender;
                celebrityModel.Feild        = celebrity.Feild;
                celebrityModel.Description  = celebrity.Description;
                celebrityModel.ActiveStatus = true;

                var serializer     = new JavaScriptSerializer();
                var json1          = serializer.Serialize(celebrityModel);
                var stringContent1 = new StringContent(json1, Encoding.UTF8, "application/json");

                HttpResponseMessage response = await client.PostAsync("api/Celebrity/NewCelebrity", stringContent1);

                if (response.IsSuccessStatusCode)
                {
                    id = response.Content.ReadAsAsync <int>().Result;
                    //

                    using (var fileStream = new MemoryStream(imgData))
                    {
                        string s = await faceAPI.register(imgData, id);

                        if (s != "Success")
                        {
                            ViewBag.Status  = true;
                            ViewBag.Message = s;
                            return(View());
                        }
                    }
                    //
                    DateTime dTime = DateTime.Now;
                    string   time  = dTime.ToString();
                    time = time.Replace(" ", "_") + ".jpg";

                    if (file.ContentLength > 0)
                    {
                        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=faceitphotos;AccountKey=" + ConfigurationManager.AppSettings["StorageKey"].ToString());
                        CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
                        CloudBlobContainer  container      = blobClient.GetContainerReference("celebrityimages");
                        container.CreateIfNotExists();

                        CloudBlockBlob blockBlob = container.GetBlockBlobReference(id.ToString() + "_" + time);
                        //Image i = Image.FromStream(file.InputStream, true, true);
                        //MemoryStream ms = new MemoryStream();
                        //i.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                        //byte[] imgData = ms.ToArray();

                        using (var fileStream = new MemoryStream(imgData))
                        {
                            await blockBlob.UploadFromStreamAsync(fileStream);
                        }
                    }
                    if (id != 0)
                    {
                        CelebrityPhoto photoModel = new CelebrityPhoto();

                        photoModel.Link        = "https://faceitphotos.blob.core.windows.net/celebrityimages/" + id + "_" + time;
                        photoModel.ProfilePic  = true;
                        photoModel.CelibrityID = id;
                        var json2          = serializer.Serialize(photoModel);
                        var stringContent2 = new StringContent(json2, Encoding.UTF8, "application/json");
                        response = await client.PostAsync("api/Celebrity/CelebrityPhoto", stringContent2);

                        if (response.IsSuccessStatusCode)
                        {
                            message = response.Content.ReadAsAsync <string>().Result;
                            if (message == "Success")
                            {
                                return(RedirectToAction("CelebrityProfile", new { id = id }));
                            }
                            else
                            {
                                ViewBag.Status  = true;
                                ViewBag.Message = "Error occured while creating profile";
                                return(View(celebrity));
                            }
                        }
                        else
                        {
                            ViewBag.Status  = true;
                            ViewBag.Message = "Error occured while creating profile";
                            return(View(celebrity));
                        }
                    }
                    else
                    {
                        ViewBag.Status  = true;
                        ViewBag.Message = "Error occured while creating profile";
                        return(View(celebrity));
                    }
                }


                return(View());
            }
            catch
            {
                ViewBag.Status  = true;
                ViewBag.Message = "Error occured while creating profile";
                return(null);
            }
        }