示例#1
0
 public async Task CreateImageAsyncTest()
 {
     using (var image = await _editor.CreateImageAsync(_main.PngData)) {
         CreateCompare(image);
     }
     using (var image = await _editor.CreateImageAsync(_main.JpegData)) {
         CreateCompare(image, false);
     }
 }
        private async Task TakePhotograph()
        {
            try
            {
                await CrossMedia.Current.Initialize();


                /*
                 * if (Plugin.Media.CrossMedia.Current.IsTakePhotoSupported && CrossMedia.Current.IsCameraAvailable)
                 *  {
                 *          var photo = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions() { SaveToAlbum=false, SaveMetaData=false});
                 *
                 *          if (photo != null)
                 *              return ImageSource.FromStream(() => { return photo.GetStream(); });
                 *          else
                 *              return null;
                 *
                 *  }
                 *  else
                 *  {
                 *      return null;
                 *  }
                 *
                 */
                if (!CrossMedia.Current.IsTakePhotoSupported || !CrossMedia.Current.IsCameraAvailable)
                {
                    await _dialogService.ShowAlertAsync("抱歉,没有检测到相机...", "提示", "取消");

                    return;
                }

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

                if (file == null)
                {
                    return;
                }
                /**/

                //裁切图片481X480
                var imageEditor = await _imageEditor.CreateImageAsync(CommonHelper.StreamToBytes(file.GetStream()));

                imageEditor.Resize(480, 480);
                var convertStream = CommonHelper.BytesToStream(imageEditor.ToJpeg());
                imageEditor.Dispose();

                //上传图片
                using (UserDialogs.Instance.Loading("上传图片中..."))
                {
                    var content = new MultipartFormDataContent();
                    content.Add(new StreamContent(convertStream), "\"file\"", $"\"{file.Path}\"");
                    var httpClient = new HttpClient();
                    var uploadServiceBaseAddress = "http://www.jshcrb.com:9100/document/reomte/fileupload/HRXHJS";
                    //var uploadServiceBaseAddress = "http://192.168.1.42:9100/document/reomte/fileupload/HRXHJS";
                    var httpResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);

                    //RemotePathLabel.Text = await httpResponseMessage.Content.ReadAsStringAsync();
                    //new { Success = "上传失败,文件不存在!", MD5="" }
                    var result = await httpResponseMessage.Content.ReadAsStringAsync();

                    var uploadResult = new UploadResult();

                    if (!string.IsNullOrEmpty(result))
                    {
                        uploadResult = JsonConvert.DeserializeObject <UploadResult>(result);
                    }

                    if (uploadResult != null)
                    {
                        //ThumbnailPhotoUrl = "http://192.168.1.42:9100/HRXHJS/document/image/" + uploadResult.Id + "";
                        ThumbnailPhotoUrl = "http://www.jshcrb.com:9100/HRXHJS/document/image/" + uploadResult.Id + "";
                    }

                    _dialogService.LongAlert(uploadResult != null ? uploadResult.Success : "未知错误!");

                    ThumbnailPhoto = ImageSource.FromStream(() =>
                    {
                        var stream = file.GetStream();
                        //string base64Str = "";
                        //using (MemoryStream memStream = new MemoryStream())
                        //{
                        //    stream.CopyToAsync(memStream);
                        //    base64Str = Convert.ToBase64String(memStream.ToArray());
                        //}
                        //System.Diagnostics.Debug.Print("base64Str-------->" + base64Str);
                        if (file != null)
                        {
                            file.Dispose();
                        }
                        return(stream);
                    });
                };
            }
            catch (Exception ex)
            {
                //ex.Message = "Unable to get file location. This most likely means that the file provider information is not set in your Android Manifest file. Please check documentation on how to set this up in your project."
                //"Unable to get file location. This most likely means that the file provider information is not set in your Android Manifest file. Please check documentation on how to set this up in your project."
                await _dialogService.ShowAlertAsync(ex.Message, "提示", "取消");
            }
        }