Windows Phone7在程序中直接開啟攝像頭(非選擇器)

jopen 12年前發布 | 20K 次閱讀 Windows Phone開發 移動開發 Windows Phone

很久沒有做wp7開發了,最近看到微信在程序中打開攝像頭進行二維碼,掃描忽然性起,遍做了下研究,然后發現原來是用到了using Microsoft.Devices;命名空間下的

PhotoCamera 對象,把它賦值給VideoBrush 就可以了

但是真機測試發現圖像和真實圖像是顛倒90度的,后來才發現,原來PhotoCamera 會將圖像旋轉一個角度,我們只要獲取到這個角度做下處理就可以了

 double d= photocamera.Orientation;在這里獲取到圖像順時針旋轉的角度
            video.RelativeTransform =  在這個把他給前臺的VideoBrush 就可以了
                new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = d };

 

做了個小demo,在此分享給大家

下面的是前臺xaml頁面代碼

<phone:PhoneApplicationPage 
    x:Class="MyPocketBook.MainPage"
    xmlns="

<!--LayoutRoot 是包含所有頁面內容的根網格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>


    <!--TitlePanel 包含應用程序的名稱和頁標題-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock x:Name="ApplicationTitle" Text="" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock x:Name="PageTitle" Text="頁面名稱" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>


    <!--ContentPanel - 在此處放置其他內容-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="516"></RowDefinition>
            <RowDefinition Height="91*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid x:Name="butlist" Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Name="btnstart" Content="開啟閃光燈" Grid.Column="0" Width="200"  Click="btnok_Click"></Button>
            <Button Name="btnstop" Content="關閉閃光燈" Grid.Column="1" Width="200"  Click="btnstop_Click"></Button>
        </Grid>
        <Rectangle Grid.Row="0">
            <Rectangle.Fill>
                <VideoBrush x:Name="video"></VideoBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
</Grid>

<!--演示 ApplicationBar 用法的示例代碼-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按鈕 1"/>
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按鈕 2"/>
        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Text="菜單項 1"/>
            <shell:ApplicationBarMenuItem Text="菜單項 2"/>
        </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->


</phone:PhoneApplicationPage></pre>

這里是后臺代碼,都加了注釋的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Devices;


namespace MyPocketBook
{
    public partial class MainPage : PhoneApplicationPage
    {
        PhotoCamera photocamera = new PhotoCamera();

        // 構造函數
        public MainPage()
        {
            InitializeComponent();
            //相機初始化事件
            photocamera.Initialized += new EventHandler<CameraOperationCompletedEventArgs>(photocamera_Initialized);
            //拍攝序列完成時發生
            photocamera.CaptureCompleted += new EventHandler<CameraOperationCompletedEventArgs>(photocamera_CaptureCompleted);
            //當有縮略圖像可用時發生
            photocamera.CaptureThumbnailAvailable += new EventHandler<ContentReadyEventArgs>(photocamera_CaptureThumbnailAvailable);
            double d= photocamera.Orientation;
            video.RelativeTransform =
                new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = d };


            video.SetSource(photocamera);
        }


        void photocamera_CaptureThumbnailAvailable(object sender, ContentReadyEventArgs e)
        {
            throw new NotImplementedException();
        }


        void photocamera_CaptureCompleted(object sender, CameraOperationCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }


        void photocamera_Initialized(object sender, CameraOperationCompletedEventArgs e)
        {

        }
        bool i = true;
        private void btnok_Click(object sender, RoutedEventArgs e)
        {
            photocamera.FlashMode = FlashMode.On;
            photocamera.Focus();
        }


        private void btnstop_Click(object sender, RoutedEventArgs e)
        {
            i = false;
            photocamera.FlashMode = FlashMode.Off;
        }

    }
}
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!