win8 metro 調用攝像頭拍攝照片并將照片保存在對應的位置

剛剛做過這類開發,所以就先獻丑了,當然所貼上的源代碼都是經過驗證過的,已經執行成功了,希望能夠給大家一些借鑒:

以下是metro UI代碼:

<Pagex:Class="Camera.MainPage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="using:Camera"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"><Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"><Button x:Name="btnCamera" Content="打開攝像頭" HorizontalAlignment="Left" Margin="48,259,0,0" VerticalAlignment="Top" Click="btnCamera_Click" Height="45"/><Image x:Name="img1" HorizontalAlignment="Left" Height="609" Margin="240,78,0,0" VerticalAlignment="Top" Width="718" Stretch="Fill"/><Button x:Name="btnSave" Content="保存圖片" HorizontalAlignment="Left" Margin="48,369,0,0" VerticalAlignment="Top" Height="44" Click="btnSave_Click"/></Grid>
</Page>

顯示的界面事實上非常easy,可是這不重要,功能才是基本的,以下貼上基本的開發代碼:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Media.Capture;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage.Streams;// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238/** * 作者:李天鵬* 功能:調用PC上自帶的camera實現拍照的功能,并保存在對應的目錄下* */
namespace Camera
{/// <summary>/// An empty page that can be used on its own or navigated to within a Frame./// </summary>public sealed partial class MainPage : Page{private StorageFile file = null;public MainPage(){this.InitializeComponent();}private async void btnCamera_Click(object sender, RoutedEventArgs e){CameraCaptureUI dialog = new CameraCaptureUI();dialog.PhotoSettings.CroppedAspectRatio = new Size(16, 9);file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);if (file != null){BitmapImage bitmapImage = new BitmapImage();using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read)){bitmapImage.SetSource(fileStream);}img1.Source = bitmapImage;}}private async void btnSave_Click(object sender, RoutedEventArgs e){if (img1.Source == null)return;else{FileSavePicker picker = new FileSavePicker();picker.CommitButtonText = "保存";picker.SuggestedFileName = "hello";picker.FileTypeChoices.Add("圖片",new string[]{".jpg",".jpeg",".bmp",".png"});picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;StorageFile filePath = await picker.PickSaveFileAsync();if (filePath != null){//打開通過攝像頭拍攝的照片,并返回流,以流的形式讀取文件var streamRandom = await file.OpenAsync(FileAccessMode.Read);//將拍攝的照片以流的形式讀取到緩沖區IBuffer buffer = RandomAccessStreamToBuffer(streamRandom);//將緩沖區內容寫入對應的目錄中await FileIO.WriteBufferAsync(filePath, buffer);}}}//將圖片寫入到緩沖區private IBuffer RandomAccessStreamToBuffer(IRandomAccessStream randomstream){Stream stream = WindowsRuntimeStreamExtensions.AsStreamForRead(randomstream.GetInputStreamAt(0));MemoryStream memoryStream = new MemoryStream();if (stream != null){byte[] bytes = ConvertStreamTobyte(stream);  //將流轉化為字節型數組if (bytes != null){var binaryWriter = new BinaryWriter(memoryStream);binaryWriter.Write(bytes);}}IBuffer buffer = WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, (int)memoryStream.Length);return buffer;}//將流轉換成二進制public static byte[] ConvertStreamTobyte(Stream input){byte[] buffer = new byte[16 * 1024];using (MemoryStream ms = new MemoryStream()){int read;while ((read = input.Read(buffer, 0, buffer.Length)) > 0){ms.Write(buffer, 0, read);}return ms.ToArray();}}}
}

可是這還不夠,假設須要調用camera,還須要一些權限,應該在Package.appxmanifest里面改動權限,


改動例如以下:僅僅要勾上webcam即可了。



源代碼下載:

http://download.csdn.net/detail/litianpeng1991/7548065

轉載于:https://www.cnblogs.com/mengfanrong/p/4069015.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/375524.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/375524.shtml
英文地址,請注明出處:http://en.pswp.cn/news/375524.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

poj 3678 Katu Puzzle(2-sat)

Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a boolean operator op (one of AND, OR, XOR) and an integer c (0 ≤ c ≤ 1). One Katu is solvable if one can find each vertex Vi a value Xi (0 ≤ Xi ≤ 1) suc…

go 語言 first argument to append must be slice

錯誤代碼 func TestSliceGrowing(t *testing.T) {s : [4]int{1, 2, 3, 4}for i :0; i<10; i {s append(s, i)t.Log(len(s), cap(s))} }報錯代碼 s append(s, i)原因&#xff1a;append的第一個參數必須是切片 更正 func TestSliceGrowing(t *testing.T) {s : []int{1,…

豆瓣網靜態頁面

divcss網站登錄注冊豆瓣讀書視頻 音樂同城小組閱讀 豆瓣FM東西更多豆瓣視頻 影訊&購票電視劇排行榜 分類影評預告片 向后向前3/5正在熱映全部正在熱映>>即將上映 烈日灼心 4.7終結者&#xff1a;創世紀... 4.7百團大戰 4.7刺客&#xff1a;聶隱娘 4.7近期熱門更多影視…

C++并發編程實戰(豆瓣評分5.4)

評分已說明一切&#xff0c;切勿踩坑&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01; 推薦的翻譯 C并發編程實戰 關注公眾號回復【C并發編程實…

Please use boost/bind/bind.hpp + using namespace boost::placeholders

The practice of declaring the Bind placeholders (_1, _2, …) in the global namespace is deprecated. Please use <boost/bind/bind.hpp> using namespace boost::placeholders, or define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior. 提示w…

奔跑吧,兄弟

10月底的時候&#xff0c;不能忍受老婆的奚落&#xff0c;開始了我的跑步計劃。 說說&#xff0c;跑步需要注意的事項&#xff0c;首先你得有雙跑步鞋&#xff0c;我有一次是穿了薄底鞋跑的&#xff0c;結果&#xff0c;打滿了水泡。跑步前控制飲水&#xff0c;最好在飲食后2個…

2299 Ultra-QuickSort(歸并)

合并排序第一次。連環畫看著合并看著別人的博客的想法。http://poj.org/problem?id2299 #include <stdio.h> #include <stdlib.h>#define MAX 500001int n,a[MAX], t[MAX]; long long int sum;//歸并 void Merge(int l, int m, int r) {int p0;int il, jm1;while…

由openSession、getCurrentSession和HibernateDaoSupport淺談Spring對事物的支持

由openSession、getCurrentSession和HibernateDaoSupport淺談Spring對事物的支持 Spring和Hibernate的集成的一個要點就是對事務的支持&#xff0c;openSession、getCurrentSession都是編程式事務&#xff08;手動設置事務的提交、回滾&#xff09;中重要的對象&#xff0c;Hi…

【tool】沒有需求文檔的時候如何來設計測試用例

沒有需求文檔的時候如何來設計測試用例 1.根據客戶的功能點整理測試需求追朔表&#xff1a; 一般的客戶都要把要開發軟件的功能點寫成一個表格交給市場部&#xff0c;讓市場部門轉交研發部。所以客戶的功能點是編寫測試用例一個最最重要的依據。 2.根據開發人員的Software Spec…

go返回多個值和python返回多個值對比

go package mulVals_test import "testing" func returnMultiValues(n int)(int, int){return n1, n2 }func TestReturnMultiValues(t *testing.T) {// a : returnMultiValues(5)// 這里嘗試用一個值接受多個返回值&#xff0c;將編譯錯誤a, _ : returnMultiValues(…

努力學習 HTML5 (3)—— 改造傳統的 HTML 頁面

要了解和熟悉 HTML5 中的新的語義元素&#xff0c;最好的方式就是拿一經典的 HTML 文檔作例子&#xff0c;然后把 HTML5 的一些新鮮營養充實進入。如下就是我們要改造的頁面&#xff0c;該頁面很簡單&#xff0c;只包含一篇文章。 ApocalypsePage_Original.html&#xff0c;這是…

判斷系統是大端還是小段

大端&#xff1a;高位內存存儲低序字節小端&#xff1a;高位內存存儲高序字節short a 0x0102&#xff0c;其中 01 高序字節&#xff0c; 02 低序字節 #include<stdio.h>int main() {union {short s;char c[sizeof(short)];} un;un.s 0x0102;if (sizeof(short) 2) {if…

手機頁面head中的meta元素

<meta http-equiv"Pragma" content"no-cache"> <meta http-equiv"expires" content"0"> <meta http-equiv"cache-control" content"no-cache"> 清除瀏覽器中的緩存&#xff0c;它和其它幾句合起…

Delphi 關鍵 重啟 注銷

//在初始化的時候獲取權限 varhToken: THandle;Tkp: TTokenPrivileges;Zero: DWORD;beginOpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES orTOKEN_QUERY, hToken);LookupPrivilegeValue(nil, SeShutdownPrivilege, Tkp.Privileges[0].Luid);Tkp.PrivilegeCou…

C語言判斷系統是32位還是64位

long 在 32 位系統中是 4 字節&#xff0c;與 int 表示范圍相同&#xff0c;在 64 位系統中是 8 字節。 #include <stdio.h> #include <stdlib.h> #include <limits.h>int main() {long a INT_MAX;if (a 1 < 0) {printf("32: %ld\n", a);} e…

使用Eclipse搭建Struts2框架

本文轉自http://blog.csdn.net/liaisuo/article/details/9064527 今天在Eclipse搭建了Struts2 框架&#xff0c;并完成了一個很簡單的例子程序。 搭建好的全局圖如下: 第一步:在http://struts.apache.org/download.cgi下載Struts2的最新版即下載Full Distribution&#xff0c;這…

打印到類陣列的給定序列的所有排列的n皇后問題

題目例如以下&#xff1a;Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 分析&#xff1a;假設僅僅是求排列數非常好算&#xff0c;可是…

asp網絡編程:ASP如何獲取客戶端真實IP地址

要想透過代理服務器取得客戶端的真實IP地址&#xff0c;就要使用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 來讀取。不過要注意的事&#xff0c;并不是每個代理服務器都能用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 來讀取客戶端的真實…

autoLayout自動布局

autoLayout 有兩個核心概念&#xff1a; 約束&#xff1a;就是對控件進行高度&#xff0c;寬度&#xff0c;相對位置的控制 參照&#xff1a;多個控件時&#xff0c;一個或多個控件以其中的一個為基準進行高度&#xff0c;寬度&#xff0c;位置的設置 當選擇了 use auto layout…

C++11實現自旋鎖

參見 《深入理解C11》 #include <thread> #include <atoimic> #include <iostream> #include <unistd.h> using namespace std;std::atomic_flag lock ATOMIC_FLAG_INIT; void f(int n) {while (lock.test_and_set(std::memory_order_acquire)) { //…