• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > Xamarin.Android 的照相機使用,xamarin.android

Xamarin.Android 的照相機使用,xamarin.android

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了xamarin android,xamarin android 中文,xamarin android教程,xamarin for android,xamarin 极光推送等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

Xamarin.Android 的照相機使用,xamarin.android


Android 提供了不少硬體功能。其中照相機功能更是兵家必爭之地。
甚至爆發如「三星門」等事件。不過本篇文章的目的只呼叫Android系統定義的API,取得相片後顯示出來。

現在我們先引入幾個 Android 負責處理 Camera 的命名空間。
當然會多些,不過要是在 Visual Studio 內開發的同學,很快就可以用工具移除。

using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;

using Android.OS;
using Android.App;
using Android.Views;
using Android.Widget;
using Android.Content;
using Android.Runtime;
using Android.Graphics;
using Android.Provider;
using Android.Content.PM;

using Java.IO;

using Environment = Android.OS.Environment;
using Uri = Android.Net.Uri;


我們先檢查硬體配備是否已準備好?

private bool IsThereAnAppToTakePictures ()
{
  Intent intent = new Intent (MediaStore.ActionImageCapture);
   IList<ResolveInfo> availableActivities = PackageManager.QueryIntentActivities (intent, PackageInfoFlags.MatchDefaultOnly);
   return availableActivities != null && availableActivities.Count > 0;
}

特別請大家注意

Intent intent = new Intent (MediaStore.ActionImageCapture);

在這邊我們使用系統提供的 Intent 來取得照相機的功能。

然後我們準備存儲目錄

private void CreateDirectoryForPictures ()
{
    App._dir = new File (
    Environment.GetExternalStoragePublicDirectory ( Environment.DirectoryPictures), "AndroidTips");
    if (!App._dir.Exists ())
    {
        bool result =App._dir.Mkdirs( );
        Debug.WriteLine ("mkdir:" + result);
    }
}      


準備處理 Bitmap 的方法(改變大小確保能按比例顯示)

public static class BitmapHelpers
{
  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);

       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;
  }
}

 

準備回調的方法

protected override void OnActivityResult (int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult (requestCode, resultCode, data);

    // Make it available in the gallery

    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 = resultImageView.Height ;
    App.bitmap = App._file.Path.LoadAndResizeBitmap (width, height);
    if (App.bitmap != null) {
        resultImageView.SetImageBitmap (App.bitmap);

        Debug.WriteLine ("contentUri:" + contentUri);

        App.bitmap = null;
    }



    // Dispose of the Java side bitmap.
    GC.Collect();
}

 

如此即可快速調用照相機的功能。

參考資料:https://developer.xamarin.com/recipes/android/other_ux/camera_intent/take_a_picture_and_save_using_camera_app/

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Xamarin android 的WebClient Json下载并存储本地及sqlite数据库,xamarinsqlite
  • XAMARIN.ANDROID SIGNALR 实时消息接收发送示例,xamarinsignalr
  • Xamarin Android 应用程序内图标上数字提示,xamarinandroid
  • Xamarin.Android 的照相機使用,xamarin.android

相关文章

  • 2017-05-26【Android】不弹root请求框检测手机是否root,androidroot
  • 2017-05-26活动的生周期(四)活动结束前的数据保存,周期活动结束
  • 2017-05-26Android Studio安装,androidstudio安装
  • 2017-05-26Android 手机卫士13--进程设置,android13--
  • 2017-05-26Android动态加载Dex机制解析
  • 2017-05-26WEB服务器、应用程序服务器、HTTP服务器区别
  • 2017-05-26Android—Service与Activity的交互,androidactivity
  • 2017-05-26Android的基本世界观——系统简介,组件逻辑及其他
  • 2017-05-26did not call through to super.onCreate(),didsuper.oncreate
  • 2017-05-227.5.2 WebView和JavaScrip交互基础

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 自定义控件——开关按钮ToggleButton,控件togglebutton
    • Android 开源库和项目 3,android开源库项目
    • Android Studio NDK开发
    • 通知 listview刷新!一天没搞出来的血泪史,listview血泪史
    • Android如何http获取数据库数据
    • 《Android Studio实用指南》7.1 AndroidStudio代码检查工具概述
    • Android 工具包 xUtils 3.3.16,xutils3.3.16
    • android中常见的设计模式有哪些?,android设计模式
    • WEB服务器、应用程序服务器、HTTP服务器区别
    • APP级别处理未捕获异常,APP级别处理捕获

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有