最近在搞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. matlab 设定坐标比例

    figure() u=-0.1:0.005:0.1; v=-0.1:0.005:0.1; [x,y]=meshgrid(u,v); z=sin(x-y)./abs(x)+abs(y); surf(x, ...

  2. README.md 编写

    Spring Boot Demo =========================== 该文件用来测试和展示书写README的各种markdown语法.GitHub的markdown语法在标准的ma ...

  3. vue中的前置守卫

    前置守卫是为了验证用户信息真实性,一些内容只能在用户登陆以后才能进行查看,例如个人中心,我的购物车,等个人页面,非隐私页面 用router.beforeEach进行验证,这个方法必须写在router实 ...

  4. eclipse 创建servlet 出现继承 HttpServlet 报红线

    eclipse创建servlet出现红线: 解决方案1,鼠标右键项目 -> 鼠标右击项目——>Build Path——> 点击comfigure Build Path进入-----& ...

  5. 解决Linux下IDEA无法使用ibus输入法的问题和tip乱码

    一:可以先按网上的配置/etc/profile里的输入法的一些参数,我是先配置了这些参数的,但是输入法还是没用,后来一直没管它了,今天用了一些方式可以了但不敢保证不需要先配置那些参数: 二:情况:开启 ...

  6. AngularJS实战之filter的使用二

    博文一中的filter是angular自带的filter,一般不会满足我们的使用.我们可以自定义filter. 一.自定义filter实现反转字符串 <div>{{ceshi|revers ...

  7. 老树新芽,在ES6下使用Express

    要让Express在ES6下跑起来就不得不用转码器Babel了.首先新建一个在某目录下新建一个项目.然后跳转到这个目录下开始下面的操作. 简单走起 安装babel-cli $ npm install ...

  8. ADALINE小demo

    线性逼近 clear;clc;close all x = [1,0.5; 1.5,1.1; 3,3; -1.2,-1]; y = x(:,2); x = [ones(size(x,1),1),x(:, ...

  9. noip第3课作业

    1.    求最大值 [问题描述] 输入三个数a,b,c,输出三个整数中的最大值 [样例输入] 10 20 30 [样例输出] 30 #include <iostream> using n ...

  10. cxgrid动态创建列

    cxgrid动态创建列 procedure TFrmRuleEdit.CreateCols;varColumn: TcxGridDBColumn;begincdsPowerPrj.First;whil ...