출처 : https://developer.microsoft.com/en-us/windows/iot/samples/webcamapp
Initialize a webcam, take pictures, and record video and audio.
This is a headed sample. To better understand what headed mode is and how to configure your device to be headed, follow the instructions here.
Load the project in Visual Studio
You can find the source code for this sample by downloading a zip of all of our samples here. Make a copy of the folder on your disk and open the project from Visual Studio.
This is a Universal Windows application
Connecting your webcam
You’ll need:
- A USB web cam (Example: Microsoft Life Cam)
Connect the web cam to one of USB ports on the IoT Device
Deploy your app
If you’re building for Minnowboard Max, select x86
as the architecture. If you’re building for Raspberry Pi 2 or 3 or DragonBoard , select ARM
.
DragonBoard only: Before deploying this app, disable the on-board mic and audio drivers. This is required on every reboot when running this app
Select Remote Machine to point to IoT device and hit F5 to deploy to your device. Go back to the basic ‘Hello World’ sample. if you need guidance
Test your app
The sample app when deployed displays 2 buttons Initialize Audio and Video
and Initialize Audio Only
. Below is a description of the actions available when the buttons are clicked.
Initialize Audio and Video:
- Preview will appear in the left most canvas
Take Photo
when clicked would store the photo in the default location and show the last captured photo in the canvas in the middleStart Video Record
when clicked would begin recording. When done, pressStop Video Record
to save the video. Video is automatically played back in the right most canvas
Initialize Audio Only:
Start Audio Record
when clicked will begin recording. When done pressStop Audio Record
to save the video. Audio is automtically played back.
NOTE: In order to hear the audio, an audio device (ex: earphones) must be connected to the analog audio output on Raspberry Pi2. On MinnowBoard Max, audio output is available via HDMI
Congratulations! You created your first web cam app.
Let’s look at the code
The code for this sample uses the Windows.Media.Capture namespace.
MediaCapture class will be used to enumerate, connect to, and perform actions using the web camera connected to the device.
For accessing the web cam, the microphone, and the default storage folders, you must add the following capabilities to the Package.appxmanifest file in your project:
NOTE: You can also add capabilities directly by opening the Package.appxmanifest file in an XML editor (Right Click on the file -> Open with -> XML (Text) Editor) and adding the capabilities below:
Initialize MediaCapture object
MediaCapture object can be initialized to capture video or audio only. In the sample,
- initVideo_Click initializes the device for both audio and video when
Initialize Audio and Video
is clicked. - initAudioOnly_Click initializes the device for audio only when
Initialize Audio Only
is clicked.
Audio and Video Mode: Start Preview, Take a photo
This section describes some XAML components we create to display the preview and photo output from the web cam. First we create a CaptureElement for preview and a and Image for the captured photo.
When the camera is initialized successfully, we start the preview in initVideo_Click.
When the Take Photo
button is clicked, we capture the image, store it in the default storage, and display it in the XAML canvas element in takePhoto_Click
Audio and Video Mode: Record a video and playback
This section describes how we record a video and play it back. First we create a MediaElement in XAML to playback the video
When the Start Video Record
button is clicked, we change the content on the button to Stop Video Record
.
When this button is clicked recordVideo_Click is called and we read the content of the button to determine whether we want to start or stop the video recording.
The code below shows how we accomplish this. Once the recording is stopped, we store the video in the default storage location and play it back in the XAML canvas element.
NOTE: In order to hear the audio, an audio device (ex: earphones) must be connected to the analog audio output on Raspberry Pi2. On MinnowBoard Max, audio output is available via HDMI
Audio Only Mode: Record audio and playback
This section describes how we record an audio and play it back. First we create a MediaElement in XAML to playback the audio
When the Start Audio Record
button is clicked, we change the content on the button to Stop Audio Record
.
When this button is clicked recordAudio_Click is called and we read the content of the button to determine whether we want to start or stop the audio recording.
The code below shows how we accomplish this. Once the recording is stopped, we store the audio in the default storage location and play it back in the XAML canvas element.
NOTE: In order to hear the audio, an audio device (ex: earphones) must be connected to the analog audio output on Raspberry Pi2. On MinnowBoard Max, audio output is available via HDMI
MediaCapture callbacks
There are two callbacks illustrated in the sample: MediaCapture.Failed and MediaCapture.RecordLimitationExceeded.
We assign these in the initVideo_Click and initAudio_Click functions
To summarize:
-
First, we create a MediaCapture object to initialize the web cam with either both audio and video settings or only audio settings
-
Based on user input, we initialize camera preview, take a photo, record a video or record an audio
-
Media files are stored appropriately and can be played back.
-
MediaCapture callback delegates are used as needed
'RaspberryPi' 카테고리의 다른 글
(RaspberryPI) 라즈비안(Raspbian) 설치하기 (0) | 2017.05.29 |
---|---|
(Windows 10 IoT) 라즈베리파이3에 USB 웹캠 연결 하고 브라우저로 보기 (0) | 2017.05.26 |
(RaspberryPI) 라즈베리파이3 발열문제 (0) | 2017.05.22 |
(Windows IoT) 샘플코드 - 링크 (0) | 2017.05.22 |
(RaspberryPI) 라즈베리파이3 로 웹서버 구축하기 (Windows 10 IoT Core) - 코딩 (0) | 2017.05.17 |