与众不同 windows phone (43) - 8.0 相机和照片: 镜头的可扩展性, 图片的可扩展性, 图片的自动上传扩展

[源码下载]


与众不同 windows phone (43) - 8.0 相机和照片: 镜头的可扩展性, 图片的可扩展性, 图片的自动上传扩展



作者:webabcd


介绍
与众不同 windows phone 8.0 之 相机和照片

  • 镜头的可扩展性
  • 图片的可扩展性
  • 图片的自动上传扩展



示例
1、演示如何将本 app 注册为镜头扩展
CameraAndPhoto/LensExtensibility.xaml

<phone:PhoneApplicationPage
    x:Class="Demo.CameraAndPhoto.LensExtensibility"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">

    <Grid Background="Transparent">
        <StackPanel Orientation="Vertical">

            <TextBlock x:Name="lblMsg" Text="启动相机后,可以通过“滤镜”启动本 app" />

            <Button x:Name="btnPhotoCapture" Content="用此 app 照相" Click="btnPhotoCapture_Click" />

            <Button x:Name="btnAudioVideoCapture" Content="用此 app 录像" Click="btnAudioVideoCapture_Click" />

        </StackPanel>
    </Grid>

</phone:PhoneApplicationPage>

CameraAndPhoto/LensExtensibility.xaml.cs

/*
 * 演示如何将本 app 注册为镜头扩展
 * 
 * 1、需要在 manifest 中增加配置 <Extension TaskID="_default" ExtensionName="Camera_Capture_App" ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5631}" />
 * 2、在 Assets 根文件夹内添加 3 个文件,作为镜头选取器的图标
 *    Lens.Screen-WVGA.png - 800*480  15:9
 *    Lens.Screen-WXGA.png - 1280*768 15:9
 *    Lens.Screen-720p.png - 1280*720 16:9
 * 
 * 
 * 注:
 * 相关的 UriMapper 参见 MyUriMapper.cs
 * 
 * 
 * 什么是镜头扩展?
 * 就是打开相机后,单击“滤镜”按钮,会出现一排 app 列表,这里的每一个 app 就是一个镜头扩展程序
 */

using System.Collections.Generic;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using System;

namespace Demo.CameraAndPhoto
{
    public partial class LensExtensibility : PhoneApplicationPage
    {
        public LensExtensibility()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            IDictionary<string, string> queryStrings = this.NavigationContext.QueryString;

            if (queryStrings.ContainsKey("fromLens"))
            {
                lblMsg.Text = "您是通过相机的“滤镜”启动本 app 的";
            }

            base.OnNavigatedTo(e);
        }

        private void btnPhotoCapture_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/CameraAndPhoto/PhotoCaptureDeviceDemo.xaml", UriKind.Relative));
        }

        private void btnAudioVideoCapture_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/CameraAndPhoto/AudioVideoCaptureDeviceDemo.xaml", UriKind.Relative));
        }
    }
}


2、演示如何将本 app 注册为图片扩展
CameraAndPhoto/PhotoExtensibility.xaml

<phone:PhoneApplicationPage
    x:Class="Demo.CameraAndPhoto.PhotoExtensibility"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <StackPanel>

            <TextBlock Name="lblMsg" TextWrapping="Wrap" Text="用此 app 照个照片,然后再在照片中心打开该照片,再打开上下文菜单,以查看图片扩展的效果" />
            
            <Image Name="img" Width="320" Height="240" Margin="0 10 0 0" />
            
        </StackPanel>
    </Grid>

</phone:PhoneApplicationPage>

CameraAndPhoto/PhotoExtensibility.xaml.cs

/*
 * 演示如何将本 app 注册为图片扩展
 * 
 * 
 * 1、注册为“共享”扩展:在照片中心的图片的上下文菜单中,单击“共享...”,弹出的 app 列表窗口包含此 app,需要在 manifest 中增加 <Extension TaskID="_default" ExtensionName="Photos_Extra_Share" ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5632}" />
 * 
 * 2、注册为“编辑”扩展:在照片中心的图片的上下文菜单中,单击“编辑...”,弹出的 app 列表窗口包含此 app,需要在 manifest 中增加 <Extension TaskID="_default" ExtensionName="Photos_Extra_Image_Editor" ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5632}" />
 *    wp7 时代的做法(与图片的上下文菜单“应用程序...”关联)在 wp8 中已经遭到了弃用,参考:http://www.cnblogs.com/webabcd/archive/2012/07/26/2609357.html 
 * 
 * 3、注册为“Photos_Rich_Media_Edit”扩展:会有以下两个特性,以 app 的名字为“WP8 Demo”为例,需要在 manifest 中增加 <Extension TaskID="_default" ExtensionName="Photos_Rich_Media_Edit" ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5632}" />
 *    a) 在照片中心打开由此 app 拍摄的照片时,在屏幕的左下角会有一段文字“由 WP8 Demo 拍摄”
 *    b) 在照片中心打开由此 app 拍摄的照片后,在其上下文菜单中会多出一项“在 WP8 Demo 中打开”
 * 
 * 
 * 注:
 * 1、需要在 manifest 中增加配置 <Capability Name="ID_CAP_MEDIALIB_PHOTO" />
 * 2、相关的 UriMapper 参见 MyUriMapper.cs
 */

using System.Collections.Generic;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Media;
using System.Windows.Media.Imaging;
using Microsoft.Xna.Framework.Media.PhoneExtensions;

namespace Demo.CameraAndPhoto
{
    public partial class PhotoExtensibility : PhoneApplicationPage
    {
        public PhotoExtensibility()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            IDictionary<string, string> queryStrings = this.NavigationContext.QueryString;

            if (queryStrings.ContainsKey("type"))
            {
                switch (queryStrings["type"])
                {
                    case "share":
                        lblMsg.Text = "由“共享...”打开";
                        break;
                    case "edit":
                        lblMsg.Text = "由“编辑...”打开";
                        break;
                    case "rich":
                        lblMsg.Text = "由“在 WP8 Demo 中打开”打开";
                        break;
                }
            }

            // 显示用户选择的图片的预览图
            if (queryStrings.ContainsKey("token"))
            {
                MediaLibrary library = new MediaLibrary();
                Picture photoFromLibrary = library.GetPictureFromToken(queryStrings["token"]);

                BitmapImage bitmapFromPhoto = new BitmapImage();
                // GetPreviewImage() - 是扩展方法,来自 Microsoft.Xna.Framework.Media.PhoneExtensions.MediaLibraryExtensions
                bitmapFromPhoto.SetSource(photoFromLibrary.GetPreviewImage());
                img.Source = bitmapFromPhoto;
            }

            base.OnNavigatedTo(e);
        }
    }
}


3、演示如何将本 app 注册为自动上传的图片扩展
CameraAndPhoto/PhotoAutoUpload.xaml

<phone:PhoneApplicationPage
    x:Class="Demo.CameraAndPhoto.PhotoAutoUpload"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">

    <Grid Background="Transparent">
        <StackPanel Orientation="Vertical">

            <TextBlock x:Name="lblMsg" TextWrapping="Wrap">
                <Run>请通过如下方式查找支持自动上传的应用:“设置 -> 应用程序 -> 照片+相机 -> 应用”或者“照片中心 -> 设置 -> 应用”</Run>
                <LineBreak />
                <Run>你至少会看到一个名为“WP8 Demo”的应用,单击它</Run>
            </TextBlock>

        </StackPanel>
    </Grid>


</phone:PhoneApplicationPage>

CameraAndPhoto/PhotoAutoUpload.xaml.cs

/*
 * 演示如何将本 app 注册为自动上传的图片扩展
 * 
 * 
 * 1、需要在 manifest 中增加配置 <Extension TaskID="_default" ExtensionName="Photos_Auto_Upload" ConsumerID = "{5B04B775-356B-4AA0-AAF8-6491FFEA5632}" />
 * 2、需要通过注册一个资源密集型代理,去执行图片处理(一般是上传图库中的新的图片到服务器)
 * 3、关于资源密集型代理参见:http://www.cnblogs.com/webabcd/archive/2012/07/12/2587440.html
 * 4、注册为自动上传的 app ,其资源密集型代理不会有 14 天过期一说,但其他限制都仍然是有的
 * 5、后台代理的任务一般是检查是否有没同步到你服务器的图片,如果有没同步的就去同步
 * 6、当然你不去同步图片到自己的服务器,而是做其他事情,也是没关系的(如果提交 app 时能通过微软审核的话)
 * 
 * 
 * 注:
 * 相关的 UriMapper 参见 MyUriMapper.cs
 * 像 SkyDrive 那样的在图片中心提示还有几个未传项,第三方的 app 是无法做到的
 * 
 * 
 * 什么是自动上传的图片扩展?
 * 1、就是可以在“设置 -> 应用程序 -> 照片+相机 -> 应用 -> WP8 Demo”或者“照片中心 -> 设置 -> 应用 -> WP8 Demo”找到的程序
 * 2、其资源密集型代理不会有 14 天过期一说,但其他限制都仍然是有的
 * 3、一般来说你应该在后台代理检查是否有没同步到你服务器的图片,如果有没同步的就去同步
 * 4、一般来说,你应该在“设置 -> 应用程序 -> 照片+相机 -> 应用 -> WP8 Demo”或者“照片中心 -> 设置 -> 应用 -> WP8 Demo”进来的页面内列出正在上传的图片列表和进度
 */

using System.Collections.Generic;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using System;

namespace Demo.CameraAndPhoto
{
    public partial class PhotoAutoUpload : PhoneApplicationPage
    {
        public PhotoAutoUpload()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            IDictionary<string, string> queryStrings = this.NavigationContext.QueryString;

            if (queryStrings.ContainsKey("fromConfig"))
            {
                lblMsg.Text = "您是通过“设置 -> 应用程序 -> 照片+相机 -> 应用 -> WP8 Demo”或者“照片中心 -> 设置 -> 应用 -> WP8 Demo”启动本 app 的";
                lblMsg.Text += Environment.NewLine;
                lblMsg.Text += "一般来说,参考 SkyDrive 的话,你应该在这里列出正在上传的图片列表和上传进度,并可以取消或暂停图片的上传任务";
            }
            
            base.OnNavigatedTo(e);
        }
    }
}


自定义 UriMapper,用于处理当本 app 由文件打开或协议打开或镜头扩展打开或图片扩展打开时,导航到相关的处理页面
MyUriMapper.cs

/*
 * 自定义 UriMapper,用于处理当本 app 由文件打开或协议打开或镜头扩展打开或图片扩展打开时,导航到相关的处理页面
 * 
 * 注:
 * 要使此 UriMapper 有效,需要在 App.xaml.cs 中增加 RootFrame.UriMapper = new Demo.MyUriMapper();
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Windows.Navigation;
using Windows.Phone.Storage.SharedAccess;

namespace Demo
{
    public class MyUriMapper : UriMapperBase
    {
        public override Uri MapUri(Uri uri)
        {
            string tempUrl = HttpUtility.UrlDecode(uri.ToString());

            // 由文件启动本 app 时会通过 /FileTypeAssociation?fileToken=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 启动
            if (tempUrl.StartsWith("/FileTypeAssociation"))
            {
                // 获取 fileToken
                int fileTokenIndex = tempUrl.IndexOf("fileToken=") + 10;
                string fileToken = tempUrl.Substring(fileTokenIndex);

                // 获取相关的文件名
                string fileName = SharedStorageAccessManager.GetSharedFileName(fileToken); // myLog.log

                // 获取相关的文件名的扩展名
                string fileType = Path.GetExtension(fileName);

                // 根据文件类型的不同导航到不同的处理页面
                switch (fileType)
                {
                    case ".log":
                        return new Uri("/AssociationLaunching/FileTypeAssociation.xaml?fileToken=" + fileToken, UriKind.Relative);
                    default:
                        return new Uri("/MainPage.xaml", UriKind.Relative);
                }
            }
            // 由协议启动本 app 时会通过 /Protocol?encodedLaunchUri=webabcd:xxxxxxxxxx 启动
            else if (tempUrl.StartsWith("/Protocol"))
            {
                // 获取协议的详细信息
                int protocolIndex = tempUrl.IndexOf("encodedLaunchUri=") + 17;
                string protocol = tempUrl.Substring(protocolIndex);

                // 导航到处理 webabcd 协议的处理页面
                return new Uri("/AssociationLaunching/ProtocolAssociation.xaml?protocol=" + protocol, UriKind.Relative);
            }
            // 由镜头扩展启动本 app 时会通过 /MainPage.xaml?Action=ViewfinderLaunch 启动
            else if (tempUrl.Contains("Action=ViewfinderLaunch"))
            {
                return new Uri("/CameraAndPhoto/LensExtensibility.xaml?fromLens=true", UriKind.Relative);
            }
            // 由图片扩展之“共享...”启动本 app 时会通过 /MainPage.xaml?Action=ShareContent&FileId={xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} 启动
            else if (tempUrl.Contains("Action=ShareContent"))
            {
                string fileId = tempUrl.Substring(tempUrl.IndexOf("FileId=") + 7).Replace("{", "").Replace("}", "");
                return new Uri("/CameraAndPhoto/PhotoExtensibility.xaml?type=share&token=" + fileId, UriKind.Relative);
            }
            // 由图片扩展之“编辑...”启动本 app 时会通过 /MainPage.xaml?Action=EditPhotoContent&FileId={xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} 启动
            else if (tempUrl.Contains("Action=EditPhotoContent"))
            {
                string fileId = tempUrl.Substring(tempUrl.IndexOf("FileId=") + 7).Replace("{", "").Replace("}", "");
                return new Uri("/CameraAndPhoto/PhotoExtensibility.xaml?type=edit&token=" + fileId, UriKind.Relative);
            }
            // 由图片扩展之“自动上传”启动本 app 时会通过 /MainPage.xaml?Action=ConfigurePhotosUploadSettings 启动
            else if (tempUrl.Contains("Action=ConfigurePhotosUploadSettings"))
            {
                return new Uri("/CameraAndPhoto/PhotoAutoUpload.xaml?fromConfig=true", UriKind.Relative);
            }
            // 在锁屏设置界面,如果将本 app 设置为背景提供程序,则锁屏界面上会有一个名为“打开应用”的按钮,点击后会通过 /MainPage.xaml?WallpaperSettings=1 启动本 app
            else if (tempUrl.Contains("WallpaperSettings=1"))
            {
                return new Uri("/Others/LockScreen.xaml?WallpaperSettings=1", UriKind.Relative);
            }

            return uri;
        }
    }
}



OK
[源码下载]

posted @ 2013-12-26 08:09  webabcd  阅读(2023)  评论(2编辑  收藏  举报