using System; using Android.Content; using Android.App; using Android.OS; using Android.Graphics; using Android.Widget; using Android.Provider; using Java.IO; using Android.Graphics.Drawables; namespace Shared { [Activity(MainLauncher = false, Theme = "@android:style/Theme.NoTitleBar.Fullscreen")] internal class CameraActivity : Android.App.Activity { bool isZoom; protected override void OnCreate(Android.OS.Bundle savedInstanceState) { base.OnCreate(savedInstanceState); isZoom = Intent.Extras.GetBoolean("IsZoom", true); if (Intent.Extras.GetInt("Type") == Camera.TypePicture) { var intent = new Intent(Intent.ActionPick); intent.SetType("image/*");//相片类型 StartActivityForResult(intent, Camera.TypePicture); } else { if (Android.OS.Environment.ExternalStorageState == Android.OS.Environment.MediaMounted) { var getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE"); StartActivityForResult(getImageByCamera, Camera.TypeCamera); } else { Toast.MakeText(Shared.Application.Activity, "Makue Sure SD Card", ToastLength.Long).Show(); } } } /// /// 处理选择或者拍照的图片 /// /// Request code. /// Result code. /// Data. protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { try { base.OnActivityResult(requestCode, resultCode, data); if (Camera._callback == null) { return; } if (resultCode != Result.Ok) { Camera._callback(null); return; } var path = System.IO.Path.Combine(Shared.Application.RootPath, Camera._fileName); var uri = data.Data; if (uri == null) { var bundle = data.Extras; if (bundle == null) { Toast.MakeText(Shared.Application.Activity, "Error", ToastLength.Long).Show(); path = null; } else { compressAndGenImage((Bitmap)bundle.Get("data"), path, 200); } } else { var photoPath = uri.Path; if (!System.IO.File.Exists(photoPath)) { //获取照片路径 string[] filePathColumn = { MediaStore.Audio.AudioColumns.Data }; var cursor = Shared.Application.Activity.ContentResolver.Query(uri, filePathColumn, null, null, null); cursor.MoveToFirst(); photoPath = cursor.GetString(cursor.GetColumnIndex(filePathColumn[0])); cursor.Close(); } compressAndGenImage(BitmapFactory.DecodeFile(photoPath), path, 200); } Camera._callback(path); } finally { Finish(); } } void compressAndGenImage(Bitmap bitmap, string outPath, int maxSize) { if (isZoom) { var width = bitmap.Width; var height = bitmap.Height; //Shared.HDLUtils.WriteLine("原宽度=" + bitmap.Width + "高度=" + bitmap.Height); var noScale = width * 1.0f / height; var scale = 16.0f / 9.0f; float x = 0, y = 0, w = width, h = height; if (scale < noScale) { w = height * scale; x = (width - w) / 2; } else { h = width / scale; y = (height - h) / 2; } var matrix = new Matrix(); matrix.PostScale(Shared.Application.GetRealWidth(640) * 1.0f / w, Shared.Application.GetRealHeight(360) * 1.0f / h); bitmap = Bitmap.CreateBitmap(bitmap, (int)x, (int)y, (int)w, (int)h, matrix, true); //Shared.HDLUtils.WriteLine("现在宽度=" + bitmap.Width + "高度=" + bitmap.Height); var fs = new System.IO.FileStream(outPath, System.IO.FileMode.Create, System.IO.FileAccess.Write); // scale var options = 100; // Store the bitmap into output stream(no compress) bitmap.Compress(Bitmap.CompressFormat.Jpeg, options, fs); // Compress by loop while (fs.Length / 1024 > maxSize) { // Clean up os fs.Close(); // interval 10 options -= 10; fs = new System.IO.FileStream(outPath, System.IO.FileMode.Create, System.IO.FileAccess.Write); bitmap.Compress(Bitmap.CompressFormat.Jpeg, options, fs); } fs.Flush(); fs.Close(); } else { var fs = new System.IO.FileStream(outPath, System.IO.FileMode.Create, System.IO.FileAccess.Write); // scale var options = 100; // Store the bitmap into output stream(no compress) bitmap.Compress(Bitmap.CompressFormat.Jpeg, options, fs); fs.Flush(); fs.Close(); } } } /// /// 拍照或者选择图片 /// public static class Camera { /// /// 拍照或者读取图片完成时调用的动作,string==null表示用户取消了拍照或者取消选择图片,string!=null,表示图片的路径 /// internal static Action _callback; /// /// 文件名 /// internal static string _fileName; /// /// 选择类型是拍照 /// internal static readonly int TypeCamera = 0xFF0001; /// /// 选择图片 /// internal static readonly int TypePicture = 0xFF0002; /// /// 拍照 /// /// 回调函数 /// 文件名 public static void TakePicture(Action callback, string fileName,bool isZoom=true) { (Application.Activity as BaseActivity)?.SetCamera((obj) => { if(obj){ _callback = callback; var intent = new Intent(Application.Activity, typeof(CameraActivity)); intent.PutExtra("Type", TypeCamera); intent.PutExtra("IsZoom", isZoom); Application.Activity?.StartActivity(intent); } else{ callback?.Invoke(null); } }); _fileName = fileName; } /// /// 选择图片 /// /// 回调函数 /// 文件名 public static void SelectPicture(Action callback, string fileName, bool isZoom = true) { (Application.Activity as BaseActivity)?.SetImagePermission((obj) => { if (obj) { _fileName = fileName; _callback = callback; var intent = new Intent(Application.Activity, typeof(CameraActivity)); intent.PutExtra("Type", TypePicture); intent.PutExtra("IsZoom", isZoom); Application.Activity?.StartActivity(intent); }else{ callback?.Invoke(null); } }); } } }