Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Windows Store Apps > Windows Store and Media (C#) > Windows Store Apps and Image Capture from Camera (C#)



Clound SSD Virtual Server

Windows Store Apps and Image Capture from Camera (C#)

Windows Store Apps and Image Capture from Camera (C#) สำหรับการถ่ายรูป หรือ ถ่ายภาพบน Camera (กล้องถ่ายรูป) ถือว่าเป็น Feature หลักที่จะมาพร้อมกับอุปกรณ์ประเภท Smart Phone และ Tablets และค่อนข้างจะเป็น Function พื้นฐานของการใช้งานอุปกรณ์ เพราะรูปแบบการทำงานนั้นไม่มีอะไรซับซ้อน แค่เพียงเรียกกล้องขึ้นมาให้ทำงาน จากนั้นก็ทำการ Capture ภาพ นำภาพที่ได้จัดเก็บลงใน SD Card หรือ Storage

Windows Store Apps and Image Capture from Camera (C#)

Windows Store Apps and Image Capture from Camera (C#)


และบน Windows Store Apps ก็สามารถเรียกใช้งานกล้องได้ด้วยวิธีง่าย ๆ เช่นเดียวกัน โดยจะมีชุดคำสั่ง CameraCaptureUI อยู่ภายใต้ Class ของ Windows.Media.Capture เข้ามาจัดการ ซึ่งจะมีรูปแบบและการนำไปใช้งานง่ายมาก สามารถเรียกเปิดกล้อง และ Capture รูป ถ่ายภาพได้ออกมาอย่างง่ายดาย

ขั้นตอนการเรียกใช้ CameraCaptureUI

เรียกใช้งาน Class ของ Windows.Media.Capture
using Windows.Media.Capture;

ทำการเปิดกล้องโดยเรียก CameraCaptureUI
var ui = new CameraCaptureUI();
ui.PhotoSettings.CroppedAspectRatio = new Size(4, 3);
[cs]สิ่งที่ได้กลับมาคือ BitmapSource
[cs]var file = await ui.CaptureFileAsync(CameraCaptureUIMode.Photo);

สามารถแปลงเป็น BitmapImage เพื่อแสดงผลบน Control ของ Image
var bitmap = new BitmapImage();
bitmap.SetSource(await file.OpenAsync(FileAccessMode.Read));
this.Photo.Source = bitmap;









Example 1 ตัวอย่างการเขียน Windows Store Apps เพื่อทำการ Capture ภาพจากกล้อง Camera

Windows Store Apps and Image Capture from Camera (C#)

ออกแบบหน้าจอประกอบด้วย Control ของ Image และ Button

Windows Store Apps and Image Capture from Camera (C#)

กรณีที่มีการใช้กล้องจะต้อง Allow ในส่วนของ Webcam

MainPage.xaml
<Page
    x:Class="WindowsStoreApps.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WindowsStoreApps"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:bm="using:Bing.Maps">


    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">



        <Image x:Name="Photo" Margin="136,48,830,391" />

        <Button x:Name="btnCamera" Content="Open Camera" HorizontalAlignment="Left" Margin="274,436,0,0" VerticalAlignment="Top" Click="btnCamera_Click"/>



    </Grid>

</Page>

MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
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.UI.Xaml.Media.Imaging; // for BitmapImage
using Windows.Storage; // for FileAccessMode

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace WindowsStoreApps
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    /// 
    public sealed partial class MainPage : Page
    {

        public MainPage()
        {
            this.InitializeComponent();
        }


        private async void btnCamera_Click(object sender, RoutedEventArgs e)
        {
            var ui = new CameraCaptureUI();
            ui.PhotoSettings.CroppedAspectRatio = new Size(4, 3);
            var file = await ui.CaptureFileAsync(CameraCaptureUIMode.Photo);

            if (file != null)
            {
                var bitmap = new BitmapImage();
                bitmap.SetSource(await file.OpenAsync(FileAccessMode.Read));
                this.Photo.Source = bitmap;
            }
        }

    }
}

Result

Windows Store Apps and Image Capture from Camera (C#)

ทดสอบการทำงาน ให้คลิก Open Camera

Windows Store Apps and Image Capture from Camera (C#)

ในการเปิดครั้งแรกโปรแกรมจะถามว่าเราจะมีการอนุญาติ Allow การใช้กล้องหรือไม่

Windows Store Apps and Image Capture from Camera (C#)

ในขั้นตอนนี้ก็เป็นความสามารถของกล้องแล้ว ว่าสามารถทำอะไรได้บ้าง

Windows Store Apps and Image Capture from Camera (C#)

โดยให้เราคลิกที่หน้าจอ เพื่อทำการ Capture ถ่ายรูปภาพ หลังจากนั้นเลือก OK

Windows Store Apps and Image Capture from Camera (C#)

จากนั้นโปรแกรมจะนำรูปภาพที่ได้มาแสดงในส่วนของ Image ที่เราออกแบบไว้

ในกรณีที่ต้องการให้จัดเก็บลงใน Storage ให้เพิ่มคำสั่งนี้เข้าไป

                var targetFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("image.jpg");
                await file.MoveAndReplaceAsync(targetFile);


Windows Store Apps and Image Capture from Camera (C#)

โปรแกรมก็จะนำไฟล์ที่ได้จัดเก็บลงใน Storage

Code เต็ม ๆ
        private async void btnCamera_Click(object sender, RoutedEventArgs e)
        {
            var ui = new CameraCaptureUI();
            ui.PhotoSettings.CroppedAspectRatio = new Size(4, 3);
            var file = await ui.CaptureFileAsync(CameraCaptureUIMode.Photo);

            if (file != null)
            {
                var bitmap = new BitmapImage();
                bitmap.SetSource(await file.OpenAsync(FileAccessMode.Read));
                this.Photo.Source = bitmap;

                var targetFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("image.jpg");
                await file.MoveAndReplaceAsync(targetFile);
            }
        }








.

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2014-06-23 13:18:13 / 2017-03-19 15:02:36
  Download : No files
 Sponsored Links / Related

 
Windows Store Apps and Video Capture from Camera (C#)
Rating :

 
Windows Store Apps and Play Media / Sound / Slider Control (C# )
Rating :

 
Windows Store Apps and Generate QR Codes (C#)
Rating :

 
Windows Store Apps and Youtube API (C#)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่