最近在搞Ocr相关的windows universal app, 用到了一些图像处理相关的知识。

涉及到了BitmapDecoder/BitmapEncoder/IRandomAccessStream等类,下面总结了IRandomAccessStream的一些扩展方法,以后还会慢慢加上其他常用的。

 public static class RandomAccessStreamExtension
{
/// <summary>
/// Retrieves an adjusted thumbnail image with the specified file stream.
/// </summary>
/// <param name="inputStream">The input stream.</param>
/// <param name="requestedSize">The requested size, in pixels.</param>
/// <returns></returns>
public static async Task<byte[]> GetThumbnailAsync(this IRandomAccessStream inputStream, uint requestedSize)
{
if (inputStream == null)
return null; var decoder = await BitmapDecoder.CreateAsync(inputStream);
var originalPixelWidth = decoder.PixelWidth;
var originalPixelHeight = decoder.PixelHeight;
if (originalPixelWidth < requestedSize || originalPixelHeight < requestedSize)
{
return await inputStream.GetBytesAsync();
} using (var outputStream = new InMemoryRandomAccessStream())
{
var encoder = await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder);
double widthRatio = (double)requestedSize / originalPixelWidth;
double heightRatio = (double)requestedSize / originalPixelHeight; uint aspectHeight = requestedSize;
uint aspectWidth = requestedSize; if (originalPixelWidth > originalPixelHeight)
{
aspectWidth = (uint)(heightRatio * originalPixelWidth);
}
else
{
aspectHeight = (uint)(widthRatio * originalPixelHeight);
} encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear;
encoder.BitmapTransform.ScaledHeight = aspectHeight;
encoder.BitmapTransform.ScaledWidth = aspectWidth;
await encoder.FlushAsync(); return await outputStream.GetBytesAsync();
}
} /// <summary>
/// Retrieves byte array from the input stream.
/// </summary>
/// <param name="stream">The input stream.</param>
/// <returns></returns>
public static async Task<byte[]> GetBytesAsync(this IRandomAccessStream stream)
{
var bytes = new byte[stream.Size];
using (var reader = new DataReader(stream.GetInputStreamAt()))
{
await reader.LoadAsync((uint)stream.Size);
reader.ReadBytes(bytes);
return bytes;
}
} /// <summary>
/// Retrieves the pixel data.
/// </summary>
/// <param name="stream">The input stream.</param>
/// <returns></returns>
public static async Task<byte[]> GetPixelDataAsync(this IRandomAccessStream stream)
{
var decoder = await BitmapDecoder.CreateAsync(stream);
var provider = await decoder.GetPixelDataAsync();
return provider.DetachPixelData();
}
}

Byte array 转 IRandomAccessStream。

下面的两个方法用到了 WindowsRuntimeBufferExtensions 和 WindowsRuntimeStreamExtensions两个类的扩展方法。

需要引用System.Runtime.InteropServices.WindowsRuntime 和 System.IO 命名空间

public static IRandomAccessStream AsRandomAccessStream(this byte[] bytes)
{
return bytes.AsBuffer().AsStream().AsRandomAccessStream();
} public static IRandomAccessStream ConvertToRandomAccessStream(this byte[] bytes)
{
var stream = new MemoryStream(bytes);
return stream.AsRandomAccessStream();
}

How to resize or create a thumbnail image from file stream on UWP的更多相关文章

  1. Create Shortcut to Get Jar File Meta Information

    You have to get meta information of cobertura.jar with command "unzip -q -c cobertura.jar META- ...

  2. CabArc to create or extract a cab file

    CabArc n D:\test.cab D:\output\*.* CabArc x D:\test.cab -r -p D:\output\*.*

  3. create a large size empty file to measure transfer speed

    OS : Windows open cmd fsutil file createnew file_name 1073741824 // 1GB fsutil file createnew file_n ...

  4. Stream 基础和常用

    来源 : http://www.cnblogs.com/jimmyzheng/archive/2012/03/17/2402814.html 系列 目前只作为个人参考. 微软的 stream 结构老大 ...

  5. Windows下Thumbnail的开发总结

    一.引言 Windows Thumbnail Handler是Windows平台下用来为关联的文件类型提供内容预览图的一套COM接口.通过实现Thumbnail相关的COM接口,就可以为为自定义的文件 ...

  6. How to Resize a Datafile (文档 ID 1029252.6)

    APPLIES TO: Oracle Database - Enterprise Edition - Version 9.2.0.1 and laterInformation in this docu ...

  7. How To Create/Extend Swap Partition In Linux Using LVM

    https://www.2daygeek.com/how-to-create-extend-swap-partition-in-linux-using-lvm/ BY RAMYA NUVVULA ·  ...

  8. create和grant配合使用,对Mysql进行创建用户和对用户授权

    1.首先创建用户username以及密码passwd,授权主机localhost. create user ‘username’@'localhost' identified by 'passwd' ...

  9. Git – fatal: Unable to create ‘/.git/index.lock’: File exists错误解决办法

    有时候在提交的时候,中间提交出错,导致有文件被lock,所以会报下面的错误: fatal: Unable to create ‘/msg/.git/index.lock’: File exists. ...

随机推荐

  1. CodeCraft-19 and Codeforces Round #537 (Div. 2) D 多重排列 + 反向01背包 + 离线处理

    https://codeforces.com/contest/1111/problem/D 多重排列 + 反向01背包 题意: 给你一个字符串(n<=1e5,n为偶数),有q个询问,每次询问两个 ...

  2. mfc标题栏 菜单 退出 关机 重启

    隐藏标题栏 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStru ...

  3. IntellJ IDEA 2017 激活编译器配置,读取多个配置文件

    1.设置编译器,找到1,点击2 2.输入设置命令:--spring.profiles.active=test,如果是多个文件输入--spring.profiles.active=test,dev 3. ...

  4. matchesSelector()方法

    let result = element.matches(selectorString); result 的值为 true 或 false. selectorString 是个css选择器字符串. i ...

  5. tokudb_tmp_dir导致的tokudb加载失败

    安装TOKUDB数据库,安装完成后为了使配置生效,重启完数据库后,发现: mysql> show engines; +--------------------+---------+ | Engi ...

  6. ubuntu-server-12.04.2安装配置jdk

    原文链接:http://blog.csdn.net/amymengfan/article/details/9958461 我选择的是离线安装,这需要先下载好jdk安装包(下载地址:http://www ...

  7. X Window(远程桌面)

    X Window在位映射屏幕上的一个或多个窗口中运行程序.用户可以在每个窗口中同时运行多个程序,并且可以通过用鼠标在窗口之间进行切换. x服务器的程序在本地工作站上运行,并且管理它的窗口和程序. 每个 ...

  8. Android 如何通过浏览器打开App

    首先给出参考资料http://my.oschina.net/liucundong/blog/354029?p=3#rpl_291191828 通过浏览器直接打开Android应用程序 Android ...

  9. https://www.cnblogs.com/hnxxcxg/p/6085149.html

    DELPHI微信支付代码   不管是微信支付还是支付宝支付, 3个最棘手的问题是:1,如何生成签名2,支付请求如何提交3, 如何验证签名 下面就围绕这二个问题来讲. 我使用的是XE3. 先看微信支付: ...

  10. Convolutional Restricted Boltzmann Machines

    参考论文:1.Stacks of Convolutional Restricted Boltzmann Machines for Shift-Invariant Feature Learning   ...