[源码下载]

背水一战 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 保存和加载, 手写识别的更多相关文章

  1. 背水一战 Windows 10 (59) - 控件(媒体类): Image, MediaElement

    [源码下载] 背水一战 Windows 10 (59) - 控件(媒体类): Image, MediaElement 作者:webabcd 介绍背水一战 Windows 10 之 控件(媒体类) Im ...

  2. 背水一战 Windows 10 (60) - 控件(媒体类): Pointer 涂鸦板, InkCanvas 涂鸦板

    [源码下载] 背水一战 Windows 10 (60) - 控件(媒体类): Pointer 涂鸦板, InkCanvas 涂鸦板 作者:webabcd 介绍背水一战 Windows 10 之 控件( ...

  3. 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑

    [源码下载] 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑 作者:webabcd 介绍背水一战 Windows 10 之 控件(媒体类) InkCanv ...

  4. 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog

    [源码下载] 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog 作者:webabcd 介绍背水一战 Windows 10 之 控 ...

  5. 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu

    [源码下载] 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu 作者:webabcd 介绍背水一战 Windows 10 之 控件(弹 ...

  6. 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout

    [源码下载] 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout 作者:webabcd 介绍背水一战 Windows 10 之 ...

  7. 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing

    [源码下载] 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing 作者:webabcd 介绍背水一 ...

  8. 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

    [源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...

  9. 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox

    [源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...

随机推荐

  1. VisualStudio2017密钥(key)我随便输入一下居然通过了?????????!!!!!!!!!!!!

    VisualStudio2017密钥(key) 2017年12月30日 14:41:38 阅读数:5048 professional:KBJFW-NXHK6-W4WJM-CRMQB-G3CDH ent ...

  2. 【Java】Maven Tomcat插件使用

    本例是用的是tomcat7-maven-plugin 插件 依赖 tomcat7-maven-plugin 插件的pom.xml依赖为 <dependency> <groupId&g ...

  3. Linux/Python学习路线

    Linux: 初级阶段: 熟练掌握常用80个命令: 掌握Linux常用软件包的安装方法,如源码安装,rpm安装等: 学习安装设备驱动程序(如网卡,显卡驱动): 了解Grub/Lilo引导程序以及简单的 ...

  4. unity延时函数

    新建一个工具类 public class DelayToInvoke : MonoBehaviour{ public static IEnumerator DelayToInvokeDo(Action ...

  5. Codeforces 1086 简要题解

    文章目录 A题 B题 C题 D题 E题 传送门 这场比赛原地爆炸了啊!!! 只做了两道. A题 传送门 手贱没关freopenfreopenfreopen于是wawawa了一次,死活调不出错. 题意: ...

  6. 2018.12.17 bzoj4802: 欧拉函数(Pollard-rho)

    传送门 Pollard−rhoPollard-rhoPollard−rho模板题. 题意简述:求ϕ(n),n≤1e18\phi(n),n\le 1e18ϕ(n),n≤1e18 先把nnn用Pollar ...

  7. 牛客训练三:处女座的比赛(hash打表)

    题目链接:传送门 思路:由于MOD只有9983大小,所以四位小写字母的字符串组合有26+26^2+26^3+26^4=475254种组合. 所以只要每次枚举出从1到475254中的hash值对应的字符 ...

  8. Mybatis-Plus 实战完整学习笔记(九)------条件构造器核心用法大全(上)

    一.Mybatisplus通用(公共方法)CRUD,一共17种(3.0.3版),2.3系列也是这么多,这个新版本一定程度进行了改造和删减. 二.构造器UML图(3.0.3)-----实体包装器,主要用 ...

  9. JDK线程池的使用

    转载自:https://my.oschina.net/hosee/blog/614319: 摘要: 本系列基于炼数成金课程,为了更好的学习,做了系列的记录. 本文主要介绍: 1. 线程池的基本使用 2 ...

  10. javaweb项目中的文件上传下载功能的实现

    框架是基于spring+myBatis的. 前台页面的部分代码: <form action="${ctx}/file/upLoadFile.do"method="p ...