Xamarin.Forms<六> Android拍照
一、接口
1、在公共项目下的文件接口
1 using System; 2 using System.IO; 3 4 namespace Autoloan.Mobile.Common 5 { 6 public interface IMediaFile : IDisposable 7 { 8 /// <summary> 9 /// 文件路径 10 /// </summary> 11 string Path { get; } 12 13 /// <summary> 14 /// 文件流 15 /// </summary> 16 /// <returns></returns> 17 Stream GetStream(); 18 } 19 }
2、在公共项目下的拍照设备接口
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Autoloan.Mobile.Common 8 { 9 public enum CameraDevice 10 { 11 Front, 12 Rear 13 } 14 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Test.Mobile.Common 8 { 9 public class CameraOptions : AbstractOptions 10 { 11 /// <summary> 12 /// 拍照设备 13 /// </summary> 14 public CameraDevice Camera { get; set; } 15 16 /// <summary> 17 /// 所选的拍照摄像头-后 18 /// </summary> 19 public CameraOptions() 20 { 21 this.Camera = CameraDevice.Rear; 22 } 23 } 24 }
3、在公共项目下的多媒体接口
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Test.Mobile.Common 8 { 9 public interface IMediaPicker 10 { 11 /// <summary> 12 /// 拍照功能是否可用 13 /// </summary> 14 bool IsCameraAvailable { get; } 15 16 /// <summary> 17 /// 图库是否可用 18 /// </summary> 19 bool IsPhotoGalleryAvailable { get; } 20 21 /// <summary> 22 /// 拍照 23 /// </summary> 24 /// <param name="options"></param> 25 /// <returns></returns> 26 Task<IMediaFile> TakePhoto(CameraOptions options = null); 27 } 28 }
二、Android下的具体功能
1 using System; 2 using System.IO; 3 using Test.Mobile.Common; 4 using Xamarin.Media; 5 6 namespace Test.Mobile.Droid 7 { 8 public class MediaFile_Android : IMediaFile 9 { 10 private readonly Xamarin.Media.MediaFile file; 11 12 public MediaFile_Android(Xamarin.Media.MediaFile file) 13 { 14 this.file = file; 15 } 16 17 public string Path 18 { 19 get { return this.file.Path; } 20 } 21 22 public Stream GetStream() 23 { 24 return this.file.GetStream(); 25 } 26 27 public void Dispose() 28 { 29 this.file.Dispose(); 30 } 31 } 32 }
1 using System; 2 using Test.Mobile.Common; 3 using Xamarin.Media; 4 using Xamarin.Forms; 5 using System.Threading.Tasks; 6 using Test.Mobile.Droid; 7 using System.IO; 8 using System.IO.IsolatedStorage; 9 using ICSharpCode.SharpZipLib.Zip; 10 11 [assembly: Dependency(typeof(MediaPicker_Android))] 12 13 namespace Test.Mobile.Droid 14 { 15 public class MediaPicker_Android : IMediaPicker 16 { 17 private readonly Xamarin.Media.MediaPicker picker; 18 19 public MediaPicker_Android() 20 { 21 this.picker = new Xamarin.Media.MediaPicker(Forms.Context); 22 } 23 24 public bool IsPhotoGalleryAvailable { 25 get { return this.picker.PhotosSupported; } 26 } 27 28 public bool IsCameraAvailable 29 { 30 get { return this.picker.IsCameraAvailable; } 31 } 32 33 public async Task<IMediaFile> TakePhoto(CameraOptions options) 34 { 35 try 36 { 37 if (!this.IsCameraAvailable) 38 throw new ArgumentException("Camera is not available"); 39 40 options = options ?? new CameraOptions(); 41 IDeviceService service = new DeviceService(); 42 string fileNname = service.GetFileName(PublicBase.CurrentOperateTask.CustomName, PublicBase.CurrentOperateTask.ApplicantName,PublicBase.CurrentOperateTask.Id) + ".jpg"; 43 var file = await this.picker.TakePhotoAsync(new StoreCameraMediaOptions 44 { 45 Directory = "Test", 46 Name = fileNname, 47 DefaultCamera = (Xamarin.Media.CameraDevice)Enum.Parse(typeof(Xamarin.Media.CameraDevice), options.Camera.ToString()) 48 }); 49 return new MediaFile_Android(file); 50 } 51 catch (OperationCanceledException) 52 { 53 return null; 54 } 55 } 56 } 57 }
三、打开相机
1 /// <summary> 2 /// 拍照 3 /// </summary> 4 private async void TakePhoto() 5 { 6 try 7 { 8 var result = await DependencyService.Get<IMediaPicker>().TakePhoto(TakePhotoType); 9 this.OnPhotoReceived(result); 10 } 11 catch (Exception ex) 12 { 13 PublicBase.WriteLog(ex.ToString()); 14 PublicBase.dlg.Alert("打开拍照功能失败", "错误提示", "确定"); 15 } 16 }
保存数据
1 /// <summary> 2 /// 保存数据 3 /// </summary> 4 /// <param name="file"></param> 5 private async void OnPhotoReceived(IMediaFile file) 6 { 7 //判断是否按下返回按钮 8 if (file == null) 9 { 10 if (CurrentPhotoList.Count > 0) 11 { 12 PublicBase.dlg.ShowLoading("保存照片数据..."); 13 await Task.Factory.StartNew(() => 14 { 15 try 16 { 17 List<t_TaskPhoto> currentList = new List<t_TaskPhoto>(); 18 foreach (var item in CurrentPhotoList) 19 { 20 string filePath = DependencyService.Get<IFilePathService>().GetPhotoRootPath() + item.Path.Substring(item.Path.LastIndexOf("/") + 1); 21 22 var taskPhoto = new t_TaskPhoto 23 { 24 PhotoPath = filePath, 25 PhotoName = filePath.Substring(filePath.LastIndexOf("/") + 1), 26 AddTime = DateTime.Now 27 }; 28 currentList.Add(taskPhoto); 29 PhotoCount++; 30 } 31 //存储数据库 32 fileService.AddPhoto(currentList); 33 currentList.Clear(); 34 35 } 36 catch (Exception ex) 37 { 38 DependencyService.Get<IAndroidService>().RecordLog("\r\nOnPhotoReceived(....) error: " + ex.Message + " " + DateTime.Now.ToString()); 39 PublicBase.dlg.Alert("保存照片失败", "错误提示", "确定"); 40 PublicBase.dlg.HideLoading(); 41 } 42 }).ContinueWith(task => 43 { 44 PublicBase.dlg.HideLoading(); 45 }); ; 46 } 47 else 48 { 49 PublicBase.dlg.Toast("取消拍照", 1, null); 50 } 51 } 52 else 53 { 54 try 55 { 56 CurrentPhotoList.Add(file); 57 //没拍5次,清理一次垃圾 58 if (CurrentPhotoList.Count % 5 == 0) 59 { 60 GC.Collect(1); 61 } 62 //连续拍照 63 TakePhoto(); 64 } 65 catch (Exception ex) 66 { 67 DependencyService.Get<IAndroidService>().RecordLog("\r\nOnPhotoReceived(....) error: " + ex.Message + " " + DateTime.Now.ToString()); 68 PublicBase.dlg.Alert("拍照失败", "错误提示", "确定"); 69 } 70 } 71 }
浙公网安备 33010602011771号