启动器和选择器学习-----(2)选择器之照相机(CameraCaptureTask)与照片(PhotoChooserTask)...

使用CameraCaptureTask和PhotoChooserTask进行图像处理与选择
本文介绍了如何使用CameraCaptureTask拍摄照片,并通过PhotoChooserTask选择设备上的图片。详细展示了如何在代码中实现图像的读取、处理及展示,包括创建CameraCaptureTask实例、调用Show方法、处理Camera_Completed事件以及使用WriteableBitmap进行图像翻转等操作。

CameraCaptureTask允许应用程序从照相机中检索照片,而PhotoChooserTask可用于选择存储在设备中的图片。

1、CameraCaptureTask

使用照相机拍摄只需创建一个CameraCaptureTask的新实例并调用Show方法。当照相机关闭时,如果已经拍摄了照片,则返回一个PhotoResult实例,其中包含该图片的引用(一个易于读取的流)以及一个在设备中的完整文件名字:

 1         private CameraCaptureTask nCamera = new CameraCaptureTask();
 2 
 3         public RingLoadingPage()
 4         {
 5             InitializeComponent();
 6             nCamera.Completed += new EventHandler<PhotoResult>(Camera_Completed);
 7         }
 8 
 9         private void LoadingStoryboard_Completed(object sender, EventArgs e)
10         {
11              nCamera.Show();
12         }
13 
14         void Camera_Completed(object sender, PhotoResult e)
15         {
16             if (e.TaskResult == TaskResult.OK)
17             {
18                 if (e.ChosenPhoto != null && e.OriginalFileName.Length > 0)
19                 {
20                     BitmapImage bitMap = new BitmapImage();
21                     bitMap.SetSource(e.ChosenPhoto);
22 
23                     ImageBrush imageBrush = new ImageBrush();
24                     imageBrush.ImageSource = bitMap;
25 
26                     ContentPanel.Background = imageBrush;
27                 }
28             }
29         }

如上代码显示,在Camera_Completed方法中的PhotoResult有两个可以访问的属性。下面代码中的OriginalFileName属性用于创建一个BitmapImage实例,然后把该实例作为ImageBrush的ImageSource属性,最后设置给grid的Background作为Grid的背景。

 1         void Camera_Completed(object sender, PhotoResult e)
 2         {
 3             if (e.TaskResult == TaskResult.OK)
 4             {
 5                 if (e.OriginalFileName.Length > 0)
 6                 {
 7                     BitmapImage bitMap = new BitmapImage(new Uri(e.OriginalFileName));
 8 
 9                     ImageBrush imageBrush = new ImageBrush();
10                     imageBrush.ImageSource = bitMap;
11 
12                     ContentPanel.Background = imageBrush;
13                 }
14             }
15         }

也可以通过读取ChosenPhoto属性得到所提供的流来访问指定文件的图片内容,在这里处理图像的内容时十分有用。下面的代码示例使用所选图像的内容创建一个WriteableBitmap实例,从而演示了该属性的使用方法:

 1         void Camera_Completed(object sender, PhotoResult e)
 2         {
 3             //Grid_Background(sender, e);
 4 
 5             if (e.TaskResult == TaskResult.OK)
 6             {
 7                 if (e.ChosenPhoto != null)
 8                 {
 9                     BitmapImage bitMapS = new BitmapImage();
10                     bitMapS.SetSource(e.ChosenPhoto);
11                     this.ImageSource.Source = bitMapS;
12 
13                     BitmapImage bitMapD = new BitmapImage();
14                     bitMapD.SetSource(e.ChosenPhoto);
15                     WriteableBitmap writeBitmap = new WriteableBitmap(bitMapD);
16                     GenerateMirrorImage(writeBitmap);
17                 }
18             }
19         }
20 
21         private void GenerateMirrorImage(WriteableBitmap nWriteableBitmap)
22         {
23             nWriteableBitmap.Invalidate();
24 
25             int pixelPosition = 0;//起始位置
26             int reversePosition = nWriteableBitmap.PixelWidth * nWriteableBitmap.PixelHeight;//结束位置
27             int pixlValue;
28             //取高度的中心线为准,上下对换。
29             for (int i = 0; i < nWriteableBitmap.PixelHeight / 2; i++)
30             {   //取最后一行的第一个元素的下标
31                 reversePosition -= nWriteableBitmap.PixelWidth;
32                 for (int j = 0; j < nWriteableBitmap.PixelWidth; j++)
33                 {
34                     pixlValue = nWriteableBitmap.Pixels[reversePosition];
35                     nWriteableBitmap.Pixels[reversePosition] = nWriteableBitmap.Pixels[pixelPosition];
36                     nWriteableBitmap.Pixels[pixelPosition] = pixlValue;
37 
38                     pixelPosition++;
39                     reversePosition++;
40                 }
41                 //取倒数第二行的第一个元素的下标
42                 reversePosition -= nWriteableBitmap.PixelWidth;
43             }
44             this.ImageDes.Source = nWriteableBitmap;
45         }

标准的BitmapImage实例是只读的。一旦被加载后是无法更改图片的内容。而WriteableBitmap允许获取并替换单个像素的颜色。上面的代码实现了图像的水平翻转。

其实还可以对WriteableBitmap执行更多的操作,其中涉及最多的就是围绕像素的操作。WriteableBitmapEx是一个开源的库,提供了用于绘制图形、直线、曲线以及更多内容的扩展。可在CodePlex(http://writeablebitmapex.codeplex.com/)中找到它。

2、PhotoChooserTask

与CameraCaptureTask类似,PhotoChooserTask也会在Completed事件中返回一个包含图像引用的PhotoResult对象。PhotoChooserTask会显示一个Picture Picker(图片选取器)让用户在图片库中进行选择。

 1         private PhotoChooserTask nPhoto = new PhotoChooserTask();
 2 
 3         public RingLoadingPage()
 4         {
 5             InitializeComponent();
 6             //nCamera.Completed += new EventHandler<PhotoResult>(Camera_Completed);
 7             nPhoto.Completed += new EventHandler<PhotoResult>(nPhoto_Completed);
 8         }
 9 
10         void nPhoto_Completed(object sender, PhotoResult e)
11         {
12             if (e.TaskResult == TaskResult.OK)
13             {
14                 if (e.ChosenPhoto != null)
15                 {
16                     BitmapImage bitMapS = new BitmapImage();
17                     bitMapS.SetSource(e.ChosenPhoto);
18                     this.ImageChooser.Source = bitMapS;
19                 }
20             }
21         }
22 
23         private void Chooser_Click(object sender, EventArgs e)
24         {
25             this.nPhoto.PixelHeight = 100;
26             this.nPhoto.PixelWidth = 100;
27             this.nPhoto.ShowCamera = true;
28             this.nPhoto.Show();
29         }

上面的代码不仅显示了Picture Picker(图片选取器),同时对返回的图像进行100*100的尺寸剪裁,此外还想用户提供照相机拍摄照片的选择。最后在一个150*150的Image中按原图大小展示。

本文案例代码:http://files.cnblogs.com/qq278360339/PhoneApp.zip

 

转载于:https://www.cnblogs.com/qq278360339/archive/2012/06/01/2531037.html

(1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友正在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值