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

Xamarin.Android 照相机的使用

2015-11-23 17:56 721 查看
这个板块呢,我想展示如何调用照相机,如何保存其路径,然后在我们imageView里面展示

出来

先上最终效果图(用的是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();
}

好了,整个程序就到这里了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: