最近开发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. jQuery开发工具

    开发工具:MyEclipse2014 + aptana插件 下载apada 放到MyEclipse的路径   https://segmentfault.com/a/1190000005711923   ...

  2. 最小生成树求最大比率 UVALive - 5713

    题目链接:https://vjudge.net/problem/UVALive-5713 题意:给出t组数据,每组数据第一行给出一个n,表示点的数量,接下来n行,每行有三个数字,分别是点的坐标x,y和 ...

  3. HDU-1260.Tickets(简单线性DP)

    本题大意:排队排票,每个人只能自己单独购买或者和后面的人一起购买,给出k个人单独购买和合买所花费的时间,让你计算出k个人总共花费的时间,然后再稍作处理就可得到答案,具体格式看题意. 本题思路:简单dp ...

  4. 微信小程序之 -----事件

    事件分类      1. 冒泡事件:     当一个组件上的事件被触发后,该事件会向父节点传递.      2. 非冒泡事件:   当一个组件上的事件被触发后,该事件不会向父节点传递.   常见的冒泡 ...

  5. Scanner 随机数

    import java.util.Scanner;                                               import java.util.Scanner; Sc ...

  6. 20172325 2018-2019-2 《Java程序设计》第七周学习总结

    20172325 2018-2019-2 <Java程序设计>第七周学习总结 教材学习内容总结 二叉查找树 二叉查找树:是含附加属性的二叉树,即其左孩子小于父节点,而父节点又小于或等于右孩 ...

  7. ubuntu,day1基础命令,shutdown,man,touch,rm,mv,cp,stat,locale,apt,date,tzselect,cal,快捷方式,echo,查看文件

    基本设置命令 1,shutdown 命令, shutdown -r now # 现在立即重启 shutdown -r + # 三分钟后重启 shutdown -r : #在12:12时将重启计算机 s ...

  8. Arrays工具类和Collections工具类

    集合知识点总结 Arrays工具类 .binarySearch() .sort() .fill() //填充 int[] array = new int[10]; Arrays.fill(array, ...

  9. pyhton 核心编程 正则表达式习题

    方案一 import re #1. 识别下列字符串:“bat,” “bit,” “but,” “hat,” “hit,” 或 “hut” import re def test1(self): bt = ...

  10. linux 基础 文件系统 用户权限

    描述Linux系统的启动过程? 1.开机自检 BIOS 2.MBR引导 3.GRUB菜单 4.加载内核 5.运行init进程 6.从/etc/inittab读取运行级别 7.根据/etc/rc.sys ...