最近在搞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. Ubuntu下删除卸载程序图标

    Ubuntu下删除卸载程序图标 方法一:直接在终端输入命令alacarte.可以任意增.改.隐藏.显示菜单,但无法删除菜单,即使拥有root权限. 方法二:注意几个目录和文件./usr/share/a ...

  2. IntelliJ IDEA 2017版 使用笔记(十二) 其他操作:IDEA 快捷键

    其他操作   文件操作: 一.文件操作       新建文件ctrl+shift+n    二.复制当前文件       快捷键:F5   三.移动当前文件       快捷键 :F6       文 ...

  3. nginx负载均衡总结

    在关于高并发负载均衡一文中已经提到,企业在解决高并发问题时,一般有两个方向的处理策略,软件.硬件,硬件上添加负载均衡器分发大量请求,软件上可在高并发瓶颈处:数据库+web服务器两处添加解决方案,其中w ...

  4. [小结]了解innodb锁

    原创文章,会不定时更新,转发请标明出处:http://www.cnblogs.com/janehoo/p/5603983.html 背景介绍: innodb的锁分两类:lock和latch. 其中la ...

  5. MFCC

    在语音识别研究领域,音频特征的选择至关重要.在这里介绍一种非常成功的音频特征——Mel Frequency Cepstrum Coefficient(MFCC),中文名字为梅尔频率倒谱系数.MFCC特 ...

  6. (记忆化搜索 )The Triangle--hdu --1163

    http://poj.org/problem?id=1163     Description 73 88 1 02 7 4 44 5 2 6 5 (Figure 1) Figure 1 shows a ...

  7. centos修改主机名命令

    centos修改主机名命令   需要修改两处:一处是/etc/sysconfig/network,另一处是/etc/hosts,只修改任一处会导致系统启动异常.首先切换到root用户.    vi / ...

  8. CALayer-CAShapeLayer/CAGradientLayer

    参考博客 CAShapeLayer http://blog.csdn.net/yongyinmg/article/details/38755955 CAShapeLayer和CAGradientLay ...

  9. Flashcache基本使用及注意事项

    Flashcache基本使用及注意事项 发表回复 环境:Centos6.5 x64 minal 安装方法 Contents [hide] 1 安装方法 2 自动加载模块 3 创建Flashcache ...

  10. 让你的照片更鲜艳------hsv拉伸

    如果你的照片看上去灰蒙蒙的,缺少生机,那么hsv拉伸也许可以帮你的忙.hsv拉伸是一种可以提高图像鲜艳程度的图像增强方法,它能够让图像的颜色更加鲜活.艳丽,而且它的处理结果看上去很自然,比如源图中较暗 ...