处理图片(updated)
高像素的图片,比如分辨率为 7712x4352 的照片,当加载到一个 bitmap 中时会占用相当大的内存。
每个像素会占用 4个字节的内存,所以当没有被压缩时,全部的图片会占用 12800万字节(约122MB)。高像素
图片的另一个问题就是渲染,因为图片不适合windows phone 8 的最大纹理尺寸为 4096x4096 像素,所以
它会被裁切。无论怎样,因为有很多方法来处理高像素图片,所以没有什么好担心的。
显示捕获的照片
首先,把一个 Image 控件放到页面中,用来显示预览:
<!-- 取景框 -->
<phone:PhoneApplicationPage x:Class="PreviewPage" ... >
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid x:Name="ContentPanel">
<Image x:Name="PreviewImage"/>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
然后,在 C# 页面我们可以用 BitmapImage 的 intDecodePixelWidth和 int DecodePixelHeight 属性来初始化
通过void BitmapImage.SetSource(Stream streamSource) 方法调用加载的 JPEG 的数据流 bitmap 的尺寸。
为了知道 stream 中时一个横向的 或者 纵向的照片,你可以使用 Nokia Imaging SDK 中的ImageProviderInfo。
using Nokia.Graphics.Imaging;
using System.Runtime.InteropServices.WindowsRuntime; ... public partial class PreviewPage : PhoneApplicationPage
{
... private BitmapImage _bitmap = new BitmapImage(); public PreviewPage()
{
InitializeComponent(); PreviewImage.Source = _bitmap; ...
} ... private void InitializePreview(Stream stream)
{
// 使用 Nokia Imaging SDK 找出 image 的 orientation 属性,
//并且相应地设置 BitmapImage 解码选项 stream.Position = ; using (StreamImageSource source = new StreamImageSource(stream))
{
ImageProviderInfo info = null; Task.Run(async () => { info = await source.GetInfoAsync(); }).Wait(); if (info.ImageSize.Width >= info.ImageSize.Height)
{
_bitmap.DecodePixelWidth = ;
_bitmap.DecodePixelHeight = ;
}
else
{
_bitmap.DecodePixelWidth = ;
_bitmap.DecodePixelHeight = ;
} // 把 stream 设置为 bitmap 的源 stream.Position = ; _bitmap.SetSource(stream);
}
} ...
}
可以注意到通常你从平台 APIs 中得到的照片数据是一个 Stream,然后在 Nokia Imaging SDK 中通常采用 IBuffers.
通过下面的方法你可以进行方便的类型转换:
using System.Runtime.InteropServices.WindowsRuntime; // MemoryStream 的扩展方法命名空间 ... public class Utilities
{
... /// <summary>
/// 把 Stream 转换为 IBuffer 对象
/// <param name="stream">stream源</param>
/// <returns>包含stream数据的IBuffer 对象</returns>
/// </summary>
public static IBuffer StreamToBuffer(Stream stream)
{
var memoryStream = stream as MemoryStream; if (memoryStream == null)
{
using (memoryStream = new MemoryStream())
{
stream.Position = ;
stream.CopyTo(memoryStream); try
{
// 一些流类型不支持
stream.Flush();
}
catch (Exception ex)
{
} return memoryStream.GetWindowsRuntimeBuffer();
}
}
else
{
return memoryStream.GetWindowsRuntimeBuffer();
}
} /// <summary>
/// 把 IBuffer 对象转换为 stream
/// <param name="stream">buffer源</param>
/// <returns>stream</returns>
/// </summary>
public static Stream BufferToStream(IBuffer buffer)
{
return buffer.AsStream();
} ...
}
手动缩小图片的尺寸
实现的方式除了使用 BitmapImage 外,你也可以使用 Nokia Imaging SDK 方便的实现图片的缩小。Nokia Imaging SDK
可以让你指定比如 buffer 的最大的 size(bytes) 和 图片的最大 size(pixels) 来进行操作,并且为你提供了一个新的 data stream
从而你也可以用于其他的目的,而不仅仅把缩小的图片显示到屏幕中——比如保存和分享。
using Nokia.Graphics.Imaging; ... public class Utilities
{
... /// <summary>
/// 异步压缩一个 image 并且最终 JPEG 数据在字节上不会超过 maxBytes,并且在尺寸上不会超过指定的 maxSize.
/// <param name="image">压缩的 Image</param>
/// <param name="maxBytes">缓冲区最大字节大小</param>
/// <param name="maxSize">压缩的最大图片的像素</param>
/// <returns>返回压缩后的 JPEG数据缓冲区 </returns>
/// </summary>
private static async Task<IBuffer> ScaleAsync(IBuffer image, uint maxBytes, Size maxSize)
{
using (var source = new BufferImageSource(image))
{
var info = await source.GetInfoAsync(); if (info.ImageSize.Width * info.ImageSize.Height > maxSize)
{
var resizeConfiguration = new AutoResizeConfiguration(maxBytes, maxSize,
new Size(, ), AutoResizeMode.Automatic, , ColorSpace.Yuv420); return await Nokia.Graphics.Imaging.JpegTools.AutoResizeAsync(buffer, resizeConfiguration);
}
else
{
return image;
}
}
} ...
}
裁切高像素图片
显示高像素图片你可以创建各种 bitmaps 对象,更小或者等于 4096x4096 像素的最大纹理尺寸。
使用 Nokia Imaging SDK 去裁切高像素原图的各个部分是很容易的。
using Nokia.Graphics.Imaging;
using Windows.Foundation; ... public class Utilities
{
... /// <summary>
/// 异步重构(裁切)照片,并且返回一个 JPEG data buffer
/// <param name="image">将要重构的照片</param>
/// <param name="area">裁切的区域</param>
/// <returns>处理后的 JPEG data buffer </returns>
/// </summary>
public static async Task<IBuffer> Reframe(IBuffer image, Rect area)
{
using (var source = new BufferImageSource(image))
using (var effect = new FilterEffect(source))
{
effect.Filters = new List<IFilter>()
{
new ReframingFilter()
{
ReframingArea = area
}
}; using (var renderer = new JpegRenderer(effect))
{
return await renderer.RenderAsync();
}
}
} /// <summary>
/// 异步重构(裁切)照片并且把结果输出到指定的 bitmap 对象
/// <param name="image">将要重构的照片</param>
/// <param name="area">重构的区域</param>
/// <param name="bitmap">输出结果到 bitmap 对象</param>
/// </summary>
public static async Task Reframe(IBuffer image, Rect area, WriteableBitmap bitmap)
{
using (var source = new BufferImageSource(image))
using (var effect = new FilterEffect(source))
{
effect.Filters = new List<IFilter>()
{
new ReframingFilter()
{
ReframingArea = area
}
}; using (var renderer = new WriteableBitmapRenderer(effect, bitmap))
{
await renderer.RenderAsync();
}
}
} ...
}
Nokia Wiki 原文链接:http://developer.nokia.com/Resources/Library/Lumia/#!imaging/working-with-high-resolution-photos/processing-photos.html
处理图片(updated)的更多相关文章
- The certificate used to sign ***has either expired or has been revoked. An updated certificate is required to sign and install the application
真机测试的时候弹出这样的提示:The certificate used to sign ***has either expired or has been revoked. An updated ce ...
- 环信SDK报错处理方法obtain an updated library from the vendor, or disable bitcode for this target. for archit
ld: '/Users/momo/Desktop/ThreeFingers/Pods/EaseMobSDKFull/EaseMobSDKFull/lib/libEaseMobClientSDK_arm ...
- nodejs处理图片、CSS、JS链接
接触Nodejs不深,看到页面上每一个链接都要写一个handler,像在页面显示图片,或者调用外部CSS.JS文件,每个链接都要写一个handler,觉得太麻烦,是否可以写个程序出来,能够自动识别图片 ...
- PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转
[强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...
- 解决VS2015启动时Package manager console崩溃的问题 - Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope
安装VS2015,启动以后,Package manager console崩溃,错误信息如下: Windows PowerShell updated your execution policy suc ...
- Filter Effects - 使用 CSS3 滤镜处理图片
CSS3 Filter(滤镜)属性提供了提供模糊和改变元素颜色的功能.CSS3 Fitler 常用于调整图像的渲染.背景或边框显示效果.这里给大家分享的这个网站,大家可以体验下 CSS3 对图片的处理 ...
- 安装glue,用glue批量处理图片的步骤
glue批量处理图片:http://glue.readthedocs.io/en/latest/quickstart.html#and-why-those-css-class-names 首先需要安 ...
- The certificate used to sign “AppName” has either expired or has been revoked. An updated certificate is required to sign and install the application解决
问题 The certificate used to sign "AppName" has either expired or has been revoked. An updat ...
- delphi 处理图片(剪切,压缩)
剪切bmp:效果为指定的rect大小,若图片比rect小,则会放大. 都要uses Vcl.Imaging.jpeg; 需要注意的是FMX里也需要jpeg的支持,虽然没引用编译器不会报错,但用到jpg ...
随机推荐
- JS中实现字符串和数组的相互转化
早上起来看了一道js的面试题,是这样描述的:利用var s1=prompt("请输入任意的字符串","")可以获取用户输入 的字符串,试编程将用户输入的字符串“ ...
- Converting a fisheye image into a panoramic, spherical or perspective projection [转]
Converting a fisheye image into a panoramic, spherical or perspective projection Written by Paul Bou ...
- Reservoir Sampling - 蓄水池抽样算法&&及相关等概率问题
蓄水池抽样——<编程珠玑>读书笔记 382. Linked List Random Node 398. Random Pick Index 从n个数中随机选取m个 等概率随机函数面试题总结 ...
- 一次WEB前端优化尝试
今天对自己做的项目中的一个设计器界面加载速度上进行了优化,因为页面在加载的时候,感觉有点慢.首先,我用firefox的yslow和chrome的pagespeed进行了测试,效果如下,分数有点不同,但 ...
- windows下安装rabbitmq的php扩展amqp(原创)
从php官方下载相应的版本http://pecl.php.net/package/amqp,我这里使用的是1.4.0版本(http://pecl.php.net/package/amqp/1.4.0/ ...
- phpnow 在win7下遇到“安装服务[apache_pn]失败”问题的一种解决办法
安装PHPnow时如果遇到下列问题: 安装服务[apache_pn]失败.可能原因如下: 1. 服务名已存在,请卸载或使用不同的服务名. 2. 非管理员权限,不能操作 Windows NT 服务. 将 ...
- Cocos2d-x 3.2 Lua演示样例 AssetsManagerTest(资源管理器)
Cocos2d-x 3.2 Lua演示样例 AssetsManagerTest(资源管理器) 本篇博客介绍Cocos2d-x 为我们提供的一个类--AssetsManager在Lua中的使用样例,效果 ...
- tarjan+缩点+强连通定理
C - Network of Schools Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I ...
- 〖Qt编程〗Qt编程中的各种数据类型的相互转换
char * 与 const char *的转换 char *ch1=”hello11″; const char *ch2=”hello22″; ch2 = ch1;//不报错,但有警告 ch1 = ...
- mysql 5.5安装手记
从MySQL5.5 开始就要用cmake安装,本次安装的版本为mysql-5.5.22.tar.gz #准备工作 yum install -y gcc gcc-c++ libtool autoconf ...