惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks Blog

博客园 - 胖胖安

Xamarin.Android 的 Google 登入 Xamarin.iOS 的 Google 登入 Xamarin.iOS 照相機功能的使用 (1) :最簡單的做法 Xamarin.iOS 的鍵盤控制 (AutoLayout 與 新的 Keyboard 事件 ) Xamarin.Android 控制鍵盤縮放 Xamarin 環境建置–Xamarin Agent install rvm iOS : TableView MultiSelect 兩個 System.IO.FileInfo 的 Extension Method SQL Server 2008 Resize LDF IIS 7 站點下,ASP.NET 3.5 與 4.0 的並存問題 jQuery AutoComplete Parameters Tips : SQL Server 2008 "saving changes is not permitted." 的解決辦法 - 胖胖安 Section 3 : 程式進入點 Section 2 : 專案結構 Section 1 : 使用 XCode 建立一個新專案 iPhone 開發前的準備工作 IIS 7 上 设定ASP.NET 时应注意事项 DBML 在Beta2與正式版間差異 - 胖胖安 - 博客园
Xamarin.Android 的照相機使用
胖胖安 · 2016-04-18 · via 博客园 - 胖胖安

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