高像素的图片,比如分辨率为 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)的更多相关文章

  1. 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 ...

  2. 环信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 ...

  3. nodejs处理图片、CSS、JS链接

    接触Nodejs不深,看到页面上每一个链接都要写一个handler,像在页面显示图片,或者调用外部CSS.JS文件,每个链接都要写一个handler,觉得太麻烦,是否可以写个程序出来,能够自动识别图片 ...

  4. PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转

    [强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...

  5. 解决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 ...

  6. Filter Effects - 使用 CSS3 滤镜处理图片

    CSS3 Filter(滤镜)属性提供了提供模糊和改变元素颜色的功能.CSS3 Fitler 常用于调整图像的渲染.背景或边框显示效果.这里给大家分享的这个网站,大家可以体验下 CSS3 对图片的处理 ...

  7. 安装glue,用glue批量处理图片的步骤

     glue批量处理图片:http://glue.readthedocs.io/en/latest/quickstart.html#and-why-those-css-class-names 首先需要安 ...

  8. 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 ...

  9. delphi 处理图片(剪切,压缩)

    剪切bmp:效果为指定的rect大小,若图片比rect小,则会放大. 都要uses Vcl.Imaging.jpeg; 需要注意的是FMX里也需要jpeg的支持,虽然没引用编译器不会报错,但用到jpg ...

随机推荐

  1. iOS:CoreData数据库的使用二(创建多个数据库表,表之间有对应关系)

    CoreData数据库框架是一个封装性好,功能强大数据库,它底层使用的还是sqlite数据库,不过苹果公司在其基础上,为其封装新和安全性的维护上做了大量的处理,例如对一些事物做了详细的操作,如读脏数据 ...

  2. Qt 维护工具MaintenanceTool.exe 使用

    QT如何管理组件(解决“要继续此操作,至少需要一个有效且已启用的储存库”问题) 转载2017-10-26 01:48:46 标签:qt QT的组件管理软件并没有在开始菜单或者桌面添加快捷方式(5.9版 ...

  3. meterpreter源码

    /*    by codeliker @2014.12.08    github: https://github.com/codeliker*/ #include <WinSock2.h> ...

  4. 基于cookie或session的登陆验证之安全性问题

    因为session是关了浏览器就没了.所以可以通过cookie结合session方法来做验证! 第一次登陆,生成一个cookie,保存一些加密的帐号信息,然后再生成一个session 这样去其他需要验 ...

  5. liunx修改字体为宋体

    有找到修改Linux默认字体的方法sudo vi /etc/fonts/conf.d/69-language-selector-zh-cn.conf修改下sans-serif相关设定    <m ...

  6. SVM 总结

    SVM有一个核心函数SMO,也就是序列最小最优化算法.SMO基本是最快的二次规划优化算法,其核心就是找到最优参数α,计算超平面后进行分类.SMO方法可以将大优化问题分解为多个小优化问题求解,大大简化求 ...

  7. IOS开发帐号与发布问题综合

    一.iOS开发:AD-HOC版应用测试方法:http://hi.baidu.com/kangle1208/item/163f39530abb4d3195eb05a7 二.plist的方式发布: 1.y ...

  8. 同步网络时间到linux服务器(先修改时区再进行同步网络时间)

    查看时区:date -R 修改整个系统时区: rm -f /etc/localtime ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 网 ...

  9. org.codehaus.xfire.fault.XFireFault: Could not read XML stream.. Nested exception is javax.xml.strea

    xfire使用中出现故障: 1. [2014-04-16 14:51:07.564]-[ERROR] org.apache.struts2.dispatcher.Dispatcher Exceptio ...

  10. SQL语法 之 操作语句

    一.插入语句 1.插入单行记录 INSERT INTO table_name|view_name[(column1_name [,column2_name, ... ])] VALUES( value ...