您的位置:首页 > 移动开发 > Android开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: