2

シンプルなウェブカメラアプリを作ろうとしています:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Media.MediaProperties;
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.Media.Imaging;
using Windows.UI.Xaml.Navigation;

namespace App1
{
public sealed partial class MainPage : Page
{
    private Windows.Media.Capture.MediaCapture m_mediaCaptureMgr;
    private Windows.Storage.StorageFile m_photoStorageFile;
    private readonly String PHOTO_FILE_NAME = "photo.jpg";

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


    internal async void initializeCamera()
    {
        m_mediaCaptureMgr = new Windows.Media.Capture.MediaCapture();
        await m_mediaCaptureMgr.InitializeAsync();
        statusBox.Text = "initialized";
    }
    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.  The Parameter
    /// property is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    internal async void takePicture(object sender, RoutedEventArgs e)
    {

        m_photoStorageFile = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync(PHOTO_FILE_NAME, Windows.Storage.CreationCollisionOption.GenerateUniqueName);
        ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
        await m_mediaCaptureMgr.CapturePhotoToStorageFileAsync(imageProperties, m_photoStorageFile);
    }

    private void initializeButton(object sender, RoutedEventArgs e)
    {
        initializeCamera();
    }
}

}

ただし、initializeButton をクリックすると、例外が発生しました。

UnauthorizedAccessException (Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)))

ここで何が問題になる可能性がありますか?

編集:バグを見つけました。基本的に、ウェブカメラが既に初期化されている場合、再度初期化しようとすると例外がトリガーされます。そのため、フラグを立てて、try/catch を実行する必要がありました。

4

2 に答える 2

1

マニフェスト ファイルでマイクと Web カメラの機能を設定しましたか?

于 2012-11-13T07:57:41.540 に答える
0

Package.Appmnifestファイル内のマイクとウェブカメラ機能を参照してください

ここに画像の説明を入力

于 2012-11-13T09:29:11.147 に答える