背水一战 Windows 10 (62) - 控件(媒体类): InkCanvas 保存和加载, 手写识别
作者:webabcd
介绍
背水一战 Windows 10 之 控件(媒体类)
- InkCanvas 保存和加载
- InkCanvas 手写识别
示例
1、演示 InkCanvas 涂鸦板的保存和加载
Controls/MediaControl/InkCanvasDemo3.xaml
<Page
x:Class="Windows10.Controls.MediaControl.InkCanvasDemo3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.MediaControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <Border Background="White" Width="480" Height="320" Margin="5" HorizontalAlignment="Left">
<!--
InkCanvas - 涂鸦板控件
-->
<InkCanvas Name="inkCanvas" />
</Border> <Button Name="save" Content="保存到文件" Margin="5" Click="save_Click" />
<Button Name="load" Content="从文件读取" Margin="5" Click="load_Click" /> </StackPanel>
</Grid>
</Page>
Controls/MediaControl/InkCanvasDemo3.xaml.cs
/*
* InkCanvas - 涂鸦板控件(继承自 FrameworkElement, 请参见 /Controls/BaseControl/FrameworkElementDemo/)
* InkPresenter - 获取 InkPresenter 对象
*
* InkPresenter - 涂鸦板
* StrokeContainer - 返回 InkStrokeContainer 类型的对象
*
* InkStrokeContainer - 用于管理涂鸦
* IAsyncOperationWithProgress<UInt32, UInt32> SaveAsync(IOutputStream outputStream) - 保存涂鸦数据
* IAsyncActionWithProgress<UInt64> LoadAsync(IInputStream inputStream) - 加载涂鸦数据
*/ using System;
using System.Collections.Generic;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Input.Inking;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Controls.MediaControl
{
public sealed partial class InkCanvasDemo3 : Page
{
public InkCanvasDemo3()
{
this.InitializeComponent(); inkCanvas.InkPresenter.InputDeviceTypes = CoreInputDeviceTypes.Mouse | CoreInputDeviceTypes.Pen | CoreInputDeviceTypes.Touch; InkDrawingAttributes drawingAttributes = inkCanvas.InkPresenter.CopyDefaultDrawingAttributes();
drawingAttributes.IgnorePressure = true;
drawingAttributes.Color = Colors.Red;
drawingAttributes.Size = new Size(, );
inkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(drawingAttributes);
} private async void save_Click(object sender, RoutedEventArgs e)
{
if (inkCanvas.InkPresenter.StrokeContainer.GetStrokes().Count == )
return; // 用于保存涂鸦数据
IRandomAccessStream stream = new InMemoryRandomAccessStream();
await inkCanvas.InkPresenter.StrokeContainer.SaveAsync(stream); // 文件保存对话框
var picker = new FileSavePicker
{
SuggestedStartLocation = PickerLocationId.DocumentsLibrary
};
picker.FileTypeChoices.Add("ink files", new List<string>() { ".ink" }); // 弹出文件保存对话框
var file = await picker.PickSaveFileAsync();
if (file == null)
return; // 在调用 CompleteUpdatesAsync 之前,阻止对文件的更新
CachedFileManager.DeferUpdates(file); // Stream 转 byte[]
DataReader reader = new DataReader(stream.GetInputStreamAt());
await reader.LoadAsync((uint)stream.Size);
byte[] bytes = new byte[stream.Size];
reader.ReadBytes(bytes); // 写入文件
await FileIO.WriteBytesAsync(file, bytes); // 保存文件
await CachedFileManager.CompleteUpdatesAsync(file);
} private async void load_Click(object sender, RoutedEventArgs e)
{
// 文件打开对话框
var picker = new FileOpenPicker
{
SuggestedStartLocation = PickerLocationId.DocumentsLibrary
};
picker.FileTypeFilter.Add(".ink"); // 弹出文件打开对话框
var pickedFile = await picker.PickSingleFileAsync();
if (pickedFile != null)
{
// 读取涂鸦数据
IRandomAccessStreamWithContentType stream = await pickedFile.OpenReadAsync(); // 加载指定的涂鸦数据
await inkCanvas.InkPresenter.StrokeContainer.LoadAsync(stream);
}
}
}
}
2、演示 InkCanvas 涂鸦板的手写识别
Controls/MediaControl/InkCanvasDemo4.xaml
<Page
x:Class="Windows10.Controls.MediaControl.InkCanvasDemo4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.MediaControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <Border Background="White" Width="480" Height="320" Margin="5" HorizontalAlignment="Left">
<!--
InkCanvas - 涂鸦板控件
-->
<InkCanvas Name="inkCanvas" />
</Border> <Button Name="recognize" Content="手写识别" Margin="5" Click="recognize_Click" /> <TextBlock Name="lblMsg" Margin="5" /> </StackPanel>
</Grid>
</Page>
Controls/MediaControl/InkCanvasDemo4.xaml.cs
/*
* InkCanvas - 涂鸦板控件(继承自 FrameworkElement, 请参见 /Controls/BaseControl/FrameworkElementDemo/)
* InkPresenter - 获取 InkPresenter 对象
*
* InkRecognizerContainer - 用于管理手写识别
* GetRecognizers() - 获取 InkRecognizer 对象集合
* SetDefaultRecognizer(InkRecognizer recognizer) - 将指定的 InkRecognizer 设置为默认的手写识别器
* RecognizeAsync(InkStrokeContainer strokeCollection, InkRecognitionTarget recognitionTarget) - 识别,返回 InkRecognitionResult 对象集合
* InkStrokeContainer strokeCollection - 需要识别的 InkStrokeContainer 对象
* InkRecognitionTarget recognitionTarget - 需要识别的目标
* All - 识别全部涂鸦数据
* Selected - 识别被选中的涂鸦数据
* Recent - 识别 InkStroke 的 Recognized 为 false 的涂鸦数据
*
* InkRecognizer - 手写识别器
* Name - 手写识别器的名字(只读)
*
* InkRecognitionResult - 手写识别结果
* BoundingRect - 获取用于识别的涂鸦的 Rect 区域
* GetStrokes() - 获取用于识别的 InkStroke 对象集合
* GetTextCandidates() - 获取识别结果,这是一个候选结果列表
*
* InkPresenter - 涂鸦板
* StrokeContainer - 返回 InkStrokeContainer 类型的对象
*
* InkStrokeContainer - 用于管理涂鸦
* UpdateRecognitionResults(IReadOnlyList<InkRecognitionResult> recognitionResults) - 将指定的识别结果通知给 InkStrokeContainer(此时 InkStrokeContainer 中被识别的 InkStroke 的 Recognized 将被标记为 true)
* 如果使用的是 InkRecognitionTarget.All 则 InkStrokeContainer 中的所有的 InkStroke 的 Recognized 将被标记为 true
* 如果使用的是 InkRecognitionTarget.Selected 则 InkStrokeContainer 中的被选中的 InkStroke 的 Recognized 将被标记为 true
* GetRecognitionResults() - 返回之前通过 UpdateRecognitionResults 方法设置的数据
*
* InkStroke - 涂鸦对象(这是一次的涂鸦对象,即鼠标按下后移动然后再抬起后所绘制出的涂鸦)
* Recognized - 此 InkStroke 是否被识别了
* Selected - 此 InkStroke 是否被选中了
*/ using System;
using System.Collections.Generic;
using System.Diagnostics;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Input.Inking;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Controls.MediaControl
{
public sealed partial class InkCanvasDemo4 : Page
{
public InkCanvasDemo4()
{
this.InitializeComponent(); inkCanvas.InkPresenter.InputDeviceTypes = CoreInputDeviceTypes.Mouse | CoreInputDeviceTypes.Pen | CoreInputDeviceTypes.Touch; InkDrawingAttributes drawingAttributes = inkCanvas.InkPresenter.CopyDefaultDrawingAttributes();
drawingAttributes.IgnorePressure = true;
drawingAttributes.Color = Colors.Red;
drawingAttributes.Size = new Size(, );
inkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(drawingAttributes);
} private async void recognize_Click(object sender, RoutedEventArgs e)
{
if (inkCanvas.InkPresenter.StrokeContainer.GetStrokes().Count == )
return; InkRecognizerContainer container = new InkRecognizerContainer(); lblMsg.Text = "手写识别器: ";
lblMsg.Text += Environment.NewLine;
// 获取当前支持的手写识别器列表,如果有多个的话可以通过 SetDefaultRecognizer 方法来指定默认的手写识别器
IReadOnlyList<InkRecognizer> recognizers = container.GetRecognizers();
foreach (InkRecognizer ir in recognizers)
{
lblMsg.Text += ir.Name;
lblMsg.Text += Environment.NewLine;
} lblMsg.Text += Environment.NewLine;
lblMsg.Text += "识别结果: ";
lblMsg.Text += Environment.NewLine;
IReadOnlyList<InkRecognitionResult> result = await container.RecognizeAsync(inkCanvas.InkPresenter.StrokeContainer, InkRecognitionTarget.All);
foreach (string textCandidate in result[].GetTextCandidates())
{
lblMsg.Text += textCandidate;
lblMsg.Text += Environment.NewLine;
} // 将识别结果通知给 InkStrokeContainer
inkCanvas.InkPresenter.StrokeContainer.UpdateRecognitionResults(result); // 识别结果通知给 InkStrokeContainer 后,被识别的 InkStroke 的 Recognized 将被标记为 true
// 如果在识别的时候使用的是 InkRecognitionTarget.All 则所有的 InkStroke 的 Recognized 将被标记为 true
// 如果在识别的时候使用的是 InkRecognitionTarget.Selected 则被选中的 InkStroke 的 Recognized 将被标记为 true
IReadOnlyList<InkStroke> strokes = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();
foreach (InkStroke stroke in strokes)
{
Debug.WriteLine("stroke.Recognized: " + stroke.Recognized);
} // 这个获取到的就是之前通过 InkStrokeContainer 方式设置的数据
result = inkCanvas.InkPresenter.StrokeContainer.GetRecognitionResults();
}
}
}
OK
[源码下载]
背水一战 Windows 10 (62) - 控件(媒体类): InkCanvas 保存和加载, 手写识别的更多相关文章
- 背水一战 Windows 10 (59) - 控件(媒体类): Image, MediaElement
[源码下载] 背水一战 Windows 10 (59) - 控件(媒体类): Image, MediaElement 作者:webabcd 介绍背水一战 Windows 10 之 控件(媒体类) Im ...
- 背水一战 Windows 10 (60) - 控件(媒体类): Pointer 涂鸦板, InkCanvas 涂鸦板
[源码下载] 背水一战 Windows 10 (60) - 控件(媒体类): Pointer 涂鸦板, InkCanvas 涂鸦板 作者:webabcd 介绍背水一战 Windows 10 之 控件( ...
- 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑
[源码下载] 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑 作者:webabcd 介绍背水一战 Windows 10 之 控件(媒体类) InkCanv ...
- 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog
[源码下载] 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog 作者:webabcd 介绍背水一战 Windows 10 之 控 ...
- 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu
[源码下载] 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu 作者:webabcd 介绍背水一战 Windows 10 之 控件(弹 ...
- 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout
[源码下载] 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout 作者:webabcd 介绍背水一战 Windows 10 之 ...
- 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing
[源码下载] 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing 作者:webabcd 介绍背水一 ...
- 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch
[源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...
- 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox
[源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...
随机推荐
- CSS实现背景透明而背景上的文字不透明
在我们设计制作一些网页的时候可能会用到半透明的效果,首先我们可能会想到用PNG图片处理,当然这是一个不错的办法,唯一的兼容性问题就是ie6 下的BUG,但这也不困难,加上一段js处理就行了.但假如我们 ...
- Globalization and accessibility for tile and toast notifications (Windows Store apps)
http://msdn.microsoft.com/en-us/library/windows/apps/hh831183.aspx 做 HighContrast时,采用以下分目录方式: /Proje ...
- kbmmw 的HTTPSmartService 上传文件到服务器端
前面我写过了 HTTPSmartService 使用介绍,可以参见以前的文章. 前一向有同学问如何在http 页面表单上上传文件.一直没有时间回答,自己简单做了例子, 发现无法实现功能,今天花了一天时 ...
- JPA错误
2016-11-141.2016-10-31: hibernate用注解 一对多 报Could not determine type for错误 原因: 接下来继续解决第二个问题:怎么又与集合打交道 ...
- python模块:re
# # Secret Labs' Regular Expression Engine # # re-compatible interface for the sre matching engine # ...
- Linux+mysql+apache+php
1.1.1 所需软件 cmake ncourse mysql apr apr-util pcre apache php 1.1.2 解压缩软件 ...
- matlab2016b和c# .net4.0混合编程
参考:https://www.cnblogs.com/eniac12/p/4390845.html 主要想用c#写软件界面,利用matlab绘图,或者用里面的遗传算法. 我的环境是:Win10 64位 ...
- .Net实现Windows服务安装完成后自动启动的两种方法
考虑到部署方便,我们一般都会将C#写的Windows服务制作成安装包.在服务安装完成以后,第一次还需要手动启动服务,这样非常不方便. 方法一:在安装完成事件里面调用命令行的方式启动服务 此操作之前要先 ...
- nginx负载均衡的5种策略
nginx可以根据客户端IP进行负载均衡,在upstream里设置ip_hash,就可以针对同一个C类地址段中的客户端选择同一个后端服务器,除非那个后端服务器宕了才会换一个. nginx的upstre ...
- js 匿名函数 用法
JS执行顺序为从上到下 先声明存储匿名函数的变量放在JS文件中 <script src="/Scripts/niming.js" type="text/javasc ...