mono for android 获取手机照片或拍照并裁剪保存
2014-03-01 15:14
519 查看
axml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btn0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="从相册中选择照片" /> <Button android:id="@+id/btn1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="拍照" /> <ImageView android:id="@+id/imageID" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
C#
using System; using Android.App; using Android.Content; using Android.Widget; using Android.OS; using Android.Graphics; using System.IO; namespace Phoot { [Activity(Label = "Phoot", MainLauncher = true, Icon = "@drawable/icon")] public class Activity1 : Activity { public static int NONE = 0; public static int PHOTOHRAPH = 1;//拍照 public static int PHOTOXZOOM = 2;//缩放 public static int PHOTORESOULT = 3;//结果 public static string IMAGE_UNSPECTFIED = "image/*"; ImageView imageview = null; Button button0 = null; Button button1 = null; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); button0 = FindViewById<Button>(Resource.Id.btn0); button1 = FindViewById<Button>(Resource.Id.btn1); imageview = FindViewById<ImageView>(Resource.Id.imageID); button0.Click += new EventHandler(button0_Click); button1.Click += new EventHandler(button1_Click); } //选择照片 void button0_Click(object sender, EventArgs e) { Intent intent = new Intent(Intent.ActionPick, null); //选择照片意图 intent.SetDataAndType( Android.Provider.MediaStore.Images.Media.ExternalContentUri, IMAGE_UNSPECTFIED); StartActivityForResult(intent, PHOTOXZOOM); } //拍照 void button1_Click(object sender, EventArgs e) { Intent intent = new Intent(Android.Provider.MediaStore.ActionImageCapture); string file = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(),Android.OS.Environment.DirectoryDcim.ToString()+ "/test.jpg"); var outputFileUri = Android.Net.Uri.Parse(file); intent.PutExtra(Android.Provider.MediaStore.ExtraOutput,file); StartActivityForResult(intent, PHOTOHRAPH); } protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { if (requestCode == NONE) { return; } if (requestCode == PHOTOHRAPH) { //获取照片 Bitmap bitmap = (Bitmap)data.Extras.Get("data"); Java.IO.File picture = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.ToString()+"/"+Android.OS.Environment.DirectoryDcim.ToString() + "/test.jpg"); //Java.IO.FileOutputStream b = new Java.IO.FileOutputStream(picture); FileStream MyFileStream1 = new FileStream(picture.ToString(), FileMode.Create); //保存照片 bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, MyFileStream1); if (System.IO.File.Exists(picture.ToString())) { startPhotoZoom(Android.Net.Uri.FromFile(picture)); } //startPhotoZoom } if (data == null) { return; } if (requestCode == PHOTOXZOOM) { startPhotoZoom(data.Data); } //处理结果 if (requestCode == PHOTORESOULT) { Bundle extras = data.Extras; if (extras != null) { Bitmap photo = (Bitmap)extras.GetParcelable("data"); //Stream stream = null; //压缩文件 //photo.Compress(Bitmap.CompressFormat.Jpeg, 75, stream); imageview.SetImageBitmap(photo); } } base.OnActivityResult(requestCode, resultCode, data); } //缩放 public void startPhotoZoom(Android.Net.Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.SetDataAndType(uri, IMAGE_UNSPECTFIED); intent.PutExtra("crop", true); //// aspectX aspectY 是宽高的比例 intent.PutExtra("aspectX", 1); intent.PutExtra("aspectY", 1); //// outputX outputY 是裁剪图片宽? intent.PutExtra("outputX", 100); intent.PutExtra("outputY", 100); intent.PutExtra("return-data", true); StartActivityForResult(intent, PHOTORESOULT); } } }
相关文章推荐
- mono for android 获取手机照片或拍照并裁剪保存
- Android调用系统相机拍照、从相册中取照片,裁剪,并保存到手机SD卡中,展示到界面
- Android笔记之获取手机图库照片和拍照获取照片
- TakePhoto是一款用于在Android设备上获取照片(拍照或从相册、文件中选择)、裁剪图片、压缩图片的开源工具库,目前最新版本3.0.0。
- android 为了兼容不同手机 拍照不允许保存 和 获取图片;获取圆角图片 正方形图片 跳转到相册 ; 和返回值
- Android手机调用系统相机拍照、裁剪以及获取Url上传图片
- Android从相册以及拍照选取照片,然后裁剪保存遇到的问题
- 一款用于在Android设备上获取照片(拍照或从相册、文件中选择)、裁剪图片、压缩图片的开源工具库
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- Android调用系统相机拍照、裁剪,并保存到手机SD卡中,展示到界面
- Android--使用手机拍照获取图片路径,小米手机选择照片问题处理
- Android从本地获取照片以及调用手机拍照功能的方法
- Android调用自身相机拍照并保存拍摄的照片到手机的sd卡
- android 适配8.0。6.0调起手机拍照获取照片路径
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- Path 部分Android手机拍照后照片被旋转的解决方案
- Android 开发调用相机拍照如何获取清晰照片
- android 部分(三星)手机拍照后照片旋转问题解决方案
- android调用照相机拍照获取照片并做简单剪裁