最近开发Android遇到了调用本地拍照功能,于是在网上搜了一些方法,加上自己理解的注释,在这儿记录下来省的下次用时候找不到,同事也给正在寻找调用本地拍照功能的小伙伴一些帮助~

  实现思路:首先加载-->判断是否具备拍照功能-->创建图片目录(文件夹)-->点击拍照事件-->返回图片并绑定在控件上显示。

引用命名空间:

    using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.OS;
using Android.Provider;
using Android.Widget;
using Java.IO;
using Environment = Android.OS.Environment;
using Uri = Android.Net.Uri;

加载:

     protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main); if (IsThereAnAppToTakePictures()) //判断本设备是否存在拍照功能
{
CreateDirectoryForPictures(); Button button = FindViewById<Button>(Resource.Id.myButton);
_imageView = FindViewById<ImageView>(Resource.Id.imageView1);
button.Click += TakeAPicture;
}
}

判断是否具备拍照功能:

        /// <summary>
/// 判断是否具备拍照功能
/// </summary>
/// <returns></returns>
private bool IsThereAnAppToTakePictures()
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
IList<ResolveInfo> availableActivities =
PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
return availableActivities != null && availableActivities.Count > ;
}

创建图片目录(文件夹):

        /// <summary>
/// 创建目录图片
/// </summary>
private void CreateDirectoryForPictures()
{
App._dir = new File(
Environment.GetExternalStoragePublicDirectory(
Environment.DirectoryPictures), "CameraAppDemo"); //CameraAppDemo
if (!App._dir.Exists())
{
App._dir.Mkdirs();
}
}

点击拍照事件:

        /// <summary>
/// 拍照
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private void TakeAPicture(object sender, EventArgs eventArgs)
{
Intent intent = new Intent(MediaStore.ActionImageCapture); App._file = new File(App._dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid())); //保存路径 intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(App._file)); StartActivityForResult(intent, );
}

返回图片并绑定在控件上显示:

        /// <summary>
/// 拍照结束执行
/// </summary>
/// <param name="requestCode"></param>
/// <param name="resultCode"></param>
/// <param name="data"></param>
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data); Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
Uri contentUri = Uri.FromFile(App._file);
mediaScanIntent.SetData(contentUri);
SendBroadcast(mediaScanIntent); // Display in ImageView. We will resize the bitmap to fit the display
// Loading the full sized image will consume to much memory
// and cause the application to crash. int height = Resources.DisplayMetrics.HeightPixels;
int width = _imageView.Height; //获取拍照的位图
App.bitmap = App._file.Path.LoadAndResizeBitmap(width, height);
if (App.bitmap != null)
{
//将图片绑定到控件上
_imageView.SetImageBitmap(App.bitmap); //清空bitmap 否则会出现oom问题
App.bitmap = null;
} // Dispose of the Java side bitmap.
GC.Collect();
}

LoadAndResizeBitmap方法:

        public static Bitmap LoadAndResizeBitmap(this 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 = ; 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;
}

App类:

    public static class App
{
public static File _file;
public static File _dir;
public static Bitmap bitmap;
}

最后再附上下载地址:

  链接: https://pan.baidu.com/s/1h0Zg1jkCyKrZKN6N5eIB8Q

  密码: wgdm

Xamarin.Android 调用手机拍照功能的更多相关文章

  1. Android使得手机拍照功能的发展(源共享)

    Android系统调用手机拍照功能有两种方法来直接调用手机自带摄像头还有一个就是要当心自己的节拍. 例Camera360 强大的一个在每个操作系统都有一个手机摄影软件:您可以捕捉不同风格,不同特效的照 ...

  2. HTML5+Canvas+jQuery调用手机拍照功能实现图片上传(二)

    上一篇仅仅讲到前台操作,这篇专门涉及到Java后台处理.前台通过Ajax提交将Base64编码过的图片数据信息传到Java后台,然后Java这边进行接收处理.通过对图片数据信息进行Base64解码,之 ...

  3. Android实现手机拍照功能

    一.布局文件main.xml <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmln ...

  4. Java乔晓松-android中调用系统拍照功能并显示拍照的图片

    android中调用系统拍照功能并显示拍照的图片 如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图 代 ...

  5. Xamarin.Android 调用Web Api(通过ListView展示远程获取的数据)

    xamarin.android如何调用sqlserver 数据库呢(或者其他的),很多新手都会有这个疑问.xamarin.android调用远程数据主要有两种方式: 在Android中保存数据或调用数 ...

  6. [置顶] Xamarin android 调用Web Api(ListView使用远程数据)

    xamarin android如何调用sqlserver 数据库呢(或者其他的),很多新手都会有这个疑问.xamarin android调用远程数据主要有两种方式: 在Android中保存数据或调用数 ...

  7. C# - VS2019调用AForge库实现调用摄像头拍照功能

    前言 作为一名资深Delphi7程序员,想要实现摄像头扫描一维码/二维码功能,发现所有免费的第三方库都没有简便的实现办法,通用的OpenCV或者ZXing库基本上只支持XE以上的版本,而且一维码的识别 ...

  8. Android初级教程调用手机拍照与摄像功能

    这个小案例建议在手机上运行. package com.example.camera; import java.io.File; import android.net.Uri; import andro ...

  9. Android调用系统拍照裁剪和选图功能

    最近项目中用到修改用户头像的功能,基本上都是模板代码,现在简单记录一下. 调用系统拍照 private fun openCamera() { //调用相机拍照 // 创建File对象,用于存储拍照后的 ...

随机推荐

  1. python--第九天总结

    python 多进程和多线程 多线程可以共享全局变量,多进程不能.多线程中,所有子线程的进程号相同:多进程中,不同的子进程进程号不同. [多进程] Python在2.6引入了多进程的机制,并提供了丰富 ...

  2. PEiD中识别虚拟地址和物理地址

    可通过PEiD中的信息计算文件偏移地址,从而修改PE文件的关键内容,达到破解目的. 文件偏移地址=相对虚拟地址-节偏移. PEiD中有: 节偏移=虚拟地址VOffset-物理地址ROffset.  

  3. 在java项目中使用umeditor

    之前有介绍了ueditor的用法,可看这篇:https://www.cnblogs.com/roy-blog/p/7250668.html umeditor是ueditor的简化版,不仅在功能,容量上 ...

  4. [leetcode]38. Count and Say数数

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  5. ubuntu桌面环境安装中文环境

    apt install language-pack-zh-hans* language-pack-gnome-zh-hans* language-pack-kde-zh-hans* apt insta ...

  6. Python开发——解释器安装

    Python(解释器)安装 Windows 1.Python(解释器)下载链接 2.选择好安装路径,点击安装即可 3.环境变量配置 [右键计算机]-->[属性]-->[高级系统设置]--& ...

  7. Centos Firefox中文乱码

    解决CentOS Firefox 中文乱码问题,执行以下命令 Centos 6 # yum -y groupinstall chinese-support 重启电脑即可. Centos 7 yum - ...

  8. js原生的节点操作API

    // yi获取元素节点 //一 :过id的方式( 通过id查找元素,大小写敏感,如果有多个id只找到第一个) document.getElementById('div1'); // 通过类名查找元素, ...

  9. MySQL InnoDB特性:两次写(Double Write)

    http://www.ywnds.com/?p=8334 一.经典Partial page write问题? 介绍double write之前我们有必要了解partial page write(部分页 ...

  10. jquery如何在元素后面添加一个元素

    jQuery添加插入元素技巧: jquery添加分为在指定元素的里面添加和外面添加两种: 里面添加使用(append 和prepend) 里面添加又分为在里面的前面添加和后面添加 里面的前面添加使用 ...