Forms Android Bitmap 处理
2015-07-31 23:01
351 查看
forms 接口定义 using System; using System.Collections.Generic; using System.IO; namespace { public interface IUtilPicture { List<string> ResizeBitmap(string imagePath); //图片采样缩小 //删除图片 void DeletePicture(string imagePath); } } 平台实现 using System; using System.Collections.Generic; using System.IO; using System.Diagnostics; using Xamarin.Forms; using.TDroid; using Android.Graphics; [assembly:Dependency (typeof(UtilPicture))] namespace.TDroid { public class UtilPicture : Java.Lang.Object,IUtilPicture { public UtilPicture () { } public List<string> ResizeBitmap (string imagePath) { List<string> listImagePath = new List<string> (); BitmapFactory.Options newOptions = new BitmapFactory.Options (); //开始du图片 newOptions.InJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.DecodeFile (imagePath, newOptions); newOptions.InJustDecodeBounds = false; int be = calculateInSampleSize (newOptions,700,700); newOptions.InSampleSize = be; bitmap = BitmapFactory.DecodeFile (imagePath, newOptions); Bitmap tempBitOne = compressImage (bitmap,60); bitmap.Recycle (); string shinkOne = SaveCompressImage (tempBitOne, imagePath,""); listImagePath.Add (shinkOne); string shinkTwo = SaveShinkImage (shinkOne); listImagePath.Add (shinkTwo); return listImagePath; } public void DeletePicture (string imagePath) { if (File.Exists (imagePath)) { try { File.Delete (imagePath); } catch (IOException ex) { Debug.WriteLine ("DeleteFile:" + ex.Message); return; } } } private Bitmap compressImage (Bitmap bitmap,int byteCount) { MemoryStream mStrean = new MemoryStream (); bitmap.Compress (Bitmap.CompressFormat.Jpeg, 100, mStrean); int options = 100; while (mStrean.ToArray ().Length / 1024 > byteCount) { mStrean.SetLength (0); mStrean.Position = 0; bitmap.Compress (Bitmap.CompressFormat.Jpeg, options, mStrean); options -= 10; } bitmap.Recycle (); Bitmap tempBit = BitmapFactory.DecodeByteArray (mStrean.ToArray (), 0, mStrean.ToArray ().Length); mStrean.SetLength (0); mStrean.Position = 0; return tempBit; } private string SaveCompressImage (Bitmap mBitmap, string path,string fileShink) //上传图片 { MemoryStream mStrean = new MemoryStream (); mBitmap.Compress (Bitmap.CompressFormat.Jpeg, 100, mStrean); var image1Path = Environment.GetFolderPath (Environment.SpecialFolder.Personal); ///data/data/PocketDoctor.TDroid/files string mPath = Android.OS.Environment.ExternalStorageDirectory + "/FDEKYYYG/MemoImages"; mPath += fileShink; if (!Directory.Exists (mPath)) { Directory.CreateDirectory (mPath); } string fileName = path.Substring (path.LastIndexOf ('/') + 1); string imageFilePath = mPath + "/" + fileName; FileInfo fileInfo; fileInfo = new FileInfo (imageFilePath); try { FileStream fStream = fileInfo.Create (); fStream.Write (mStrean.ToArray (), 0, mStrean.ToArray ().Length); mStrean.SetLength (0); mStrean.Position = 0; fStream.Close (); mBitmap.Recycle (); } catch (Exception ex) { Debug.WriteLine ("DeleteFile:" + ex.Message); } return imageFilePath; } private string SaveShinkImage (string imagePath) //缩略图 { BitmapFactory.Options newOptions = new BitmapFactory.Options (); //开始du图片 newOptions.InJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.DecodeFile (imagePath, newOptions); newOptions.InJustDecodeBounds = false; int beTwo = calculateInSampleSize (newOptions,70,70); newOptions.InSampleSize = beTwo; bitmap = BitmapFactory.DecodeFile (imagePath, newOptions); Bitmap tempBitTwo = compressImage (bitmap,5); bitmap.Recycle (); string shinkOne = SaveCompressImage (tempBitTwo, imagePath,"Thumbnail"); return shinkOne; } private int calculateInSampleSize ( BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image int height = options.OutHeight; int width = options.OutWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { int halfHeight = height / 2; int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } } }
View Code
相关文章推荐
- 如何使用Android MediaStore裁剪大图片
- J哥---------Android 多线程下载 仿下载助手(改进版)
- Android大图片裁剪终极解决方案(下:拍照截图)
- Android Service与Activity之间通信的几种方式(1)---通过broadcast(广播)的形式
- Android大图片裁剪终极解决方案(中:从相册截图)
- Android 动画系列
- Android大图片裁剪终极解决方案(上:原理分析)
- Android固定标题栏/底栏方法
- android的Spinner控件的自定义样式设置以及ArrayAdapter的使用
- Android Shape画圆,矩形
- android用dp还是sp还是px等
- Android 图片平铺效果实现的3种方法
- [android]多次点击的控制
- Android 通过Uri获取Bitmap对象
- Android--基于Message的进程间通信
- Android 笔记之Cursor 类
- <Android>自定义竖向seekbar
- Android -- 绑定服务
- android developers training 文档学习笔记(目录)
- 使用O-LLVM和NDK对Android应用进行混淆