Xamarin.Android 照相机的使用
2015-11-23 17:56
721 查看
这个板块呢,我想展示如何调用照相机,如何保存其路径,然后在我们imageView里面展示
出来
先上最终效果图(用的是Genymotion模拟器)
新键一个Layout,命名为CameraLayout.axml
新建一个类,命名为CameraHelper,方法CreateDirectoryForPictures
是判断文件夹是否存在,如果不存在则新建文件夹;
方法LoadandResize则是为了整理图片的大小
在我们的Activity中,代码如下
为方便找到我们的界面元素,先定义元素
private ImageView _imageView;
private Button button;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.CameraLayout);
CameraHelper.CreateDirectoryForPictures();
button = FindViewById<Button>(Resource.Id.CameraButton);
_imageView = FindViewById<ImageView>(Resource.Id.imageView1);
button.Click += TakeAPicture;
}
其实呢,应该先判断我们的手机是否存在能照相的应用程序,但是我这里有点问题,到后面我再整理一下
叫起照相机,进行拍照
当程序有返回值时
好了,整个程序就到这里了。
出来
先上最终效果图(用的是Genymotion模拟器)
新键一个Layout,命名为CameraLayout.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/CameraButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/openCamera" /> <ImageView android:src="@android:drawable/ic_menu_gallery" android:layout_width="fill_parent" android:layout_height="300.0dp" android:id="@+id/imageView1" android:adjustViewBounds="true" /> </LinearLayout>
新建一个类,命名为CameraHelper,方法CreateDirectoryForPictures
是判断文件夹是否存在,如果不存在则新建文件夹;
方法LoadandResize则是为了整理图片的大小
class CameraHelper { public static File _file; public static File _dir; public static Bitmap bitmap; public static void CreateDirectoryForPictures() { _dir = new File( Android.OS.Environment.GetExternalStoragePublicDirectory( Android.OS.Environment.DirectoryPictures), "CameraAppDemo"); if (!_dir.Exists()) { _dir.Mkdirs(); } } public static Bitmap LoadAndResizeBitmap( string fileName, int width, int height) { // First we get the the dimensions of the file on disk BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true }; BitmapFactory.DecodeFile(fileName, options); // Next we calculate the ratio that we need to resize the image by // in order to fit the requested dimensions. int outHeight = options.OutHeight; int outWidth = options.OutWidth; int inSampleSize = 1; if (outHeight > height || outWidth > width) { inSampleSize = outWidth > outHeight ? outHeight / height : outWidth / width; } // Now we will load the image and have BitmapFactory resize it for us. options.InSampleSize = inSampleSize; options.InJustDecodeBounds = false; Bitmap resizedBitmap = BitmapFactory.DecodeFile(fileName, options); return resizedBitmap; }
在我们的Activity中,代码如下
为方便找到我们的界面元素,先定义元素
private ImageView _imageView;
private Button button;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.CameraLayout);
CameraHelper.CreateDirectoryForPictures();
button = FindViewById<Button>(Resource.Id.CameraButton);
_imageView = FindViewById<ImageView>(Resource.Id.imageView1);
button.Click += TakeAPicture;
}
其实呢,应该先判断我们的手机是否存在能照相的应用程序,但是我这里有点问题,到后面我再整理一下
叫起照相机,进行拍照
private void TakeAPicture(object sender, EventArgs eventArgs) { var intent = new Intent(MediaStore.ActionImageCapture); CameraHelper._file = new File(CameraHelper._dir, string.Format("myPhoto_{0}.jpg", Guid.NewGuid())); intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(CameraHelper._file)); StartActivityForResult(intent, 0); }
当程序有返回值时
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); //判断可用 var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile); var contentUri = Uri.FromFile(CameraHelper._file); mediaScanIntent.SetData(contentUri); SendBroadcast(mediaScanIntent); var height = Resources.DisplayMetrics.HeightPixels; var width = _imageView.Height; CameraHelper.bitmap = CameraHelper.LoadAndResizeBitmap(CameraHelper._file.Path, width, height); if (CameraHelper.bitmap != null) { _imageView.SetImageBitmap(CameraHelper.bitmap); CameraHelper.bitmap = null; } //释放资源 GC.Collect(); }
好了,整个程序就到这里了。
相关文章推荐
- Android HelloChart开源库图表(二)之饼图
- Androidstudio 真机device offline
- 解决 Android Studio 乱码问题(特别是关于IDE的不同窗口里显示乱码的情况)
- android多屏幕适配注意几点
- android WindowManager
- Android动画的fillAfter属性
- 搭建Android开发环境
- Android Action广播大全,开机自启动的服务
- android中onActivityResult不执行
- 简单谈谈android自定义相机的实现(上 android.hardware.Camera)
- Android屏幕适配经验谈
- Android开发屏幕适配全攻略
- Android根据坐标判断是否在控件内
- Android项目依赖与直接导入Jar包
- AndroidStudio单元测试——instrumentation
- android重要的依赖库与SDK收集
- 老罗android之旅补丁版之三HAL层
- Android软键盘的隐藏显示监听
- Android专用Log开源项目——KLog
- 安卓巴士Android开发神贴整理