techidea

技术,封闭制造壁垒,开源促进进步。 https://github.com/chaozaiai

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  17 随笔 :: 0 文章 :: 0 评论 :: 8995 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
复制代码
  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 }
View Code
复制代码

 

posted on   techidea  阅读(225)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
阅读排行:
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析
点击右上角即可分享
微信分享提示