处理图片(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 ...
随机推荐
- OpenCV亚像素级的角点检测
亚像素级的角点检测 目标 在本教程中我们将涉及以下内容: 使用OpenCV函数 cornerSubPix 寻找更精确的角点位置 (不是整数类型的位置,而是更精确的浮点类型位置). 理论 代码 这个教程 ...
- hdu-4725-The Shortest Path in Nya Graph-层次网络
我们依据每一个人的layer把同样layer的人分配到同一个层次中. 然后记录走到每一个层次的最小值. 假设这个最小值被更新了. 那么我们就更新与这个层次相连的层次上的点. 其它的就是最普通的spfa ...
- 错误Name node is in safe mode的解决方法 (转)
原文链接:错误Name node is in safe mode的解决方法 将本地文件拷贝到hdfs上去,结果上错误:Name node is in safe mode 这是因为在分布式文件系统启动的 ...
- HDU2089 ------不要62(数位dp)
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- (笔试题)关于C++的虚函数和多态性
以下两段程序的输出是什么? 程序1: #include "stdio.h" class Base { public: int Bar(char x) { return (int)( ...
- java中循环控制结构
1. break结束break所在循环 for(i……) { for(j……) { break; //结束循环j } } 2.带标签的break. java中的标签只用在循环语句前面. outer: ...
- Warning: cast to/from pointer from/to integer of different size
将int变量转为(void*)时出现错误 error: cast to pointer from integer of different size [-Werror=int-to-pointer-c ...
- webservice接口示例(spring+xfire+webservice)
webservice接口示例(spring+xfire+webservice) CreateTime--2018年4月2日17:36:07 Author:Marydon 一.准备工作 1.1 ja ...
- Eclipse默认编码格式设置方式
看图即可 STEP ONE: STEP TWO: STEP THREE: STEP FOUR: 项目右击——>Properties 参阅: eclipse编码格式设置 - AlanLee(Jav ...
- FFmpeg进行屏幕录像和录音
文章转自:http://www.cucer.cn/2016/03/10/ffmpeg-screen-capture.html 有些时候我们需要对屏幕进行录制,比如制作视频教程,录制直播等.然而这方面的 ...