標籤:returns 設定檔 pos 本地 判斷 double ges nbsp XA
隨著手機的拍照像素越來越高,導致圖片贊的容量越來越大,如果上傳多張圖片不進行壓縮、品質處理很容易出現OOM記憶體流失問題。
最近做了一個項目,向webservices上傳多張照片,但是項目部署出來就會出現閃退現象,後來經行調試發現圖片沒有進行壓縮,一張圖片大小為2M,然而webservices沒法接搜多個大圖片,所以需要改下設定檔,我這裡改為40M。
<system.web> <httpRuntime maxRequestLength = "40960" 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, 0, 0, (int)width, (int)height, matrix, true); return bitmap; } static string BitmapToString(Bitmap bitmap) { Bitmap bit = zoomImage(bitmap, 750, 1000);//小圖 //品質壓縮 //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, 80, 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");
Xamarin.Android 壓縮圖片並上傳到WebServices