标签:
1 forms 接口定义 2 3 using System; 4 using System.Collections.Generic; 5 using System.IO; 6 7 namespace 8 { 9 public interface IUtilPicture 10 { 11 List<string> ResizeBitmap(string imagePath); //图片采样缩小 12 //删除图片 13 void DeletePicture(string imagePath); 14 } 15 } 16 17 18 平台实现 19 using System; 20 using System.Collections.Generic; 21 using System.IO; 22 using System.Diagnostics; 23 24 using Xamarin.Forms; 25 26 using.TDroid; 27 28 using Android.Graphics; 29 30 [assembly:Dependency (typeof(UtilPicture))] 31 namespace.TDroid 32 { 33 public class UtilPicture : Java.Lang.Object,IUtilPicture 34 { 35 public UtilPicture () 36 { 37 38 } 39 40 public List<string> ResizeBitmap (string imagePath) 41 { 42 List<string> listImagePath = new List<string> (); 43 BitmapFactory.Options newOptions = new BitmapFactory.Options (); 44 //开始du图片 45 newOptions.InJustDecodeBounds = true; 46 Bitmap bitmap = BitmapFactory.DecodeFile (imagePath, newOptions); 47 newOptions.InJustDecodeBounds = false; 48 int be = calculateInSampleSize (newOptions,700,700); 49 newOptions.InSampleSize = be; 50 bitmap = BitmapFactory.DecodeFile (imagePath, newOptions); 51 Bitmap tempBitOne = compressImage (bitmap,60); 52 bitmap.Recycle (); 53 string shinkOne = SaveCompressImage (tempBitOne, imagePath,""); 54 listImagePath.Add (shinkOne); 55 string shinkTwo = SaveShinkImage (shinkOne); 56 listImagePath.Add (shinkTwo); 57 return listImagePath; 58 } 59 60 61 public void DeletePicture (string imagePath) 62 { 63 if (File.Exists (imagePath)) { 64 try { 65 File.Delete (imagePath); 66 } catch (IOException ex) { 67 Debug.WriteLine ("DeleteFile:" + ex.Message); 68 return; 69 } 70 } 71 } 72 73 private Bitmap compressImage (Bitmap bitmap,int byteCount) 74 { 75 MemoryStream mStrean = new MemoryStream (); 76 bitmap.Compress (Bitmap.CompressFormat.Jpeg, 100, mStrean); 77 int options = 100; 78 while (mStrean.ToArray ().Length / 1024 > byteCount) { 79 mStrean.SetLength (0); 80 mStrean.Position = 0; 81 bitmap.Compress (Bitmap.CompressFormat.Jpeg, options, mStrean); 82 options -= 10; 83 } 84 bitmap.Recycle (); 85 Bitmap tempBit = BitmapFactory.DecodeByteArray (mStrean.ToArray (), 0, mStrean.ToArray ().Length); 86 mStrean.SetLength (0); 87 mStrean.Position = 0; 88 return tempBit; 89 } 90 91 private string SaveCompressImage (Bitmap mBitmap, string path,string fileShink) //上传图片 92 { 93 MemoryStream mStrean = new MemoryStream (); 94 mBitmap.Compress (Bitmap.CompressFormat.Jpeg, 100, mStrean); 95 var image1Path = Environment.GetFolderPath (Environment.SpecialFolder.Personal); 96 ///data/data/PocketDoctor.TDroid/files 97 string mPath = Android.OS.Environment.ExternalStorageDirectory + "/FDEKYYYG/MemoImages"; 98 mPath += fileShink; 99 if (!Directory.Exists (mPath)) { 100 Directory.CreateDirectory (mPath); 101 } 102 string fileName = path.Substring (path.LastIndexOf (‘/‘) + 1); 103 string imageFilePath = mPath + "/" + fileName; 104 FileInfo fileInfo; 105 fileInfo = new FileInfo (imageFilePath); 106 try { 107 FileStream fStream = fileInfo.Create (); 108 fStream.Write (mStrean.ToArray (), 0, mStrean.ToArray ().Length); 109 mStrean.SetLength (0); 110 mStrean.Position = 0; 111 fStream.Close (); 112 mBitmap.Recycle (); 113 } catch (Exception ex) { 114 Debug.WriteLine ("DeleteFile:" + ex.Message); 115 } 116 return imageFilePath; 117 } 118 119 private string SaveShinkImage (string imagePath) //缩略图 120 { 121 BitmapFactory.Options newOptions = new BitmapFactory.Options (); 122 //开始du图片 123 newOptions.InJustDecodeBounds = true; 124 Bitmap bitmap = BitmapFactory.DecodeFile (imagePath, newOptions); 125 newOptions.InJustDecodeBounds = false; 126 int beTwo = calculateInSampleSize (newOptions,70,70); 127 newOptions.InSampleSize = beTwo; 128 bitmap = BitmapFactory.DecodeFile (imagePath, newOptions); 129 Bitmap tempBitTwo = compressImage (bitmap,5); 130 bitmap.Recycle (); 131 string shinkOne = SaveCompressImage (tempBitTwo, imagePath,"Thumbnail"); 132 return shinkOne; 133 } 134 135 private int calculateInSampleSize ( 136 BitmapFactory.Options options, int reqWidth, int reqHeight) 137 { 138 // Raw height and width of image 139 int height = options.OutHeight; 140 int width = options.OutWidth; 141 int inSampleSize = 1; 142 143 if (height > reqHeight || width > reqWidth) { 144 145 int halfHeight = height / 2; 146 int halfWidth = width / 2; 147 148 // Calculate the largest inSampleSize value that is a power of 2 and keeps both 149 // height and width larger than the requested height and width. 150 while ((halfHeight / inSampleSize) > reqHeight 151 && (halfWidth / inSampleSize) > reqWidth) { 152 inSampleSize *= 2; 153 } 154 } 155 return inSampleSize; 156 } 157 158 } 159 }
标签:
原文地址:http://www.cnblogs.com/techidea/p/4693383.html