Xamarin.Android 压缩图片并上传到WebServices

时间:2023-03-09 07:01:03
Xamarin.Android 压缩图片并上传到WebServices

  随着手机的拍照像素越来越高,导致图片赞的容量越来越大,如果上传多张图片不进行压缩、质量处理很容易出现OOM内存泄漏问题。

  最近做了一个项目,向webservices上传多张照片,但是项目部署出来就会出现闪退现象,后来经行调试发现图片没有进行压缩,一张图片大小为2M,然而webservices没法接搜多个大图片,所以需要改下配置文件,我这里改为40M。

  <system.web>
<httpRuntime maxRequestLength = "" useFullyQualifiedRedirectUrl="true"/>
</system.web>

   这里改好后发现上传图片还是有问题,后来经过一步步调试发现将本地图片转换成Bitmap后没有清空,然后一直存放在内存中,导致内存泄漏。只要把转换完的Bitmap清空一下就好了。

        /// <summary>
/// 图片转换成String流
/// </summary>
/// <param name="file_path">文件名(不带file://)</param>
/// <returns></returns>
public static string ImageToString(string file_path)
{
//待上传图片路径
//string uploadFile = file_path; //转化成文件 //System.IO.FileInfo imgFile = new System.IO.FileInfo(uploadFile); ////文件转化成字节
//byte[] imgByte = new byte[imgFile.Length]; //////读文件
//System.IO.FileStream imgStream = imgFile.OpenRead(); //////文件写入到字节数组
//imgStream.Read(imgByte, 0, Convert.ToInt32(imgFile.Length)); //////字节数组转换成String类型
//string by = Convert.ToBase64String(imgByte); ////上传到服务器 后面是文件名
////fileUp.UpdateFile(imgByte, Guid.NewGuid() + ".png"); //return imgByte; Bitmap bitmap = BitmapFactory.DecodeFile(file_path); //将图片文件转换成bitmap 格式:/storage/emulated/0/DCIM/Camera/IMG_20180425_105725.jpg string bitstring = BitmapToString(bitmap);
bitmap = null; //一定要清空,否则会导致OOM问题
GC.Collect();
return bitstring;
} /// <summary>
/// 图片缩放处理
/// </summary>
/// <param name="bgimage">Bitmap文件</param>
/// <param name="newWidth">新图片宽度</param>
/// <param name="newHeight">新图片高度</param>
/// <returns></returns>
public static Bitmap zoomImage(Bitmap bgimage, double newWidth, double newHeight)
{
// 获取这个图片的宽和高
float width = bgimage.Width;
float height = bgimage.Height;
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 计算宽高缩放率
float scaleWidth = ((float)newWidth) / width;
float scaleHeight = ((float)newHeight) / height;
// 缩放图片动作
matrix.PostScale(scaleWidth, scaleHeight);
Bitmap bitmap = Bitmap.CreateBitmap(bgimage, , , (int)width,
(int)height, matrix, true);
return bitmap;
} static string BitmapToString(Bitmap bitmap)
{
Bitmap bit = zoomImage(bitmap, , );//小图
//质量压缩
//MemoryStream stream = new MemoryStream();
//bit.Compress(Bitmap.CompressFormat.Jpeg, 50, stream);
//byte[] bitmapData = stream.ToArray();
//Bitmap map = BitmapFactory.DecodeByteArray(bitmapData, 0, bitmapData.Length);
//btn_imagetwo.SetImageBitmap(map);
//Bitmap im = zoomImage(bitmap, 800, 900);//大图
MemoryStream big_stream = new MemoryStream();
bit.Compress(Bitmap.CompressFormat.Jpeg, , big_stream);
byte[] big_bitmapData = big_stream.ToArray();
return Convert.ToBase64String(big_bitmapData);
}

  webservices接受进行保存图片:

     private String ImagePath = "/HandlerImages/";

        /// <summary>
/// 上传图片
/// </summary>
/// <param name="content">图片字符流</param>
/// <param name="pathandname">图片名称</param>
/// <returns></returns> [WebMethod]
public bool UpdateFile(string content, string pathandname)
{
//保存图片路径
string FilePath = Server.MapPath(ImagePath);
//判断路径是否存在
if (!Directory.Exists(FilePath))
{
//创建路径
Directory.CreateDirectory(FilePath);
} string SaveFilePath = Path.Combine(FilePath, pathandname);
byte[] fileBytes;
try
{
fileBytes = Convert.FromBase64String(content);
MemoryStream memoryStream = new MemoryStream(fileBytes); //1.定义并实例化一个内存流,以存放提交上来的字节数组。
FileStream fileUpload = new FileStream(SaveFilePath, FileMode.Create); ///2.定义实际文件对象,保存上载的文件。
memoryStream.WriteTo(fileUpload); ///3.把内存流里的数据写入物理文件
memoryStream.Close();
fileUpload.Close();
fileUpload = null;
memoryStream = null;
return true;
}
catch
{
return false;
}
}

调用webservices上传图片:

MyWebService service = new MyWebService(); 
service.UpdateFile(ImageToByte("/storage/emulated/0/DCIM/Camera/IMG_20180425_105725.jpg"),Guid.NewGuid().ToString() + ".jpg");