简体   繁体   English

wp7中jpeg解码中的内存问题

[英]memory issue in jpeg decoding in wp7

I working on windows phone 7 camera app.I need to convert captured stream ,fix it rotation using exif helper and save as jpeg with quality,orientation,output size parameters.I followed exif rotation article to fix rotation. 我在Windows Phone 7摄像头应用程序上工作。我需要转换捕获的流,使用exif助手将其固定,并使用质量,方向,输出大小参数将其另存为jpeg。我按照exif旋转文章修复了旋转。 But the core problem is I need to decode the stream to jpeg first and then perform rotation fix as mentioned in order to save picture to media library. 但是核心问题是我需要先将流解码为jpeg,然后按照上述说明执行旋转修复,以将图片保存到媒体库。

I use following code: 我使用以下代码:

private WriteableBitmap DecodeImage(Stream photo, int angle)
 {
            WriteableBitmap source = PictureDecoder.DecodeJpeg(photo);

            photo.Seek(0, SeekOrigin.Begin);           

            System.GC.Collect();

            UiDispatcher.BeginInvoke(() =>
            {
                MessageBox.Show(App.LogMemory("after decode"));
            });
            switch (angle)
            {
                case 90:
                case 270:
                    return RotateBitmap(source, source.PixelHeight,
                        source.PixelWidth, angle);
                case 180:
                    return RotateBitmap(source, source.PixelWidth,
                        source.PixelHeight, angle);
                default:
                    return source;
            }
            return null;
        }

In RotateBitMap method i have the rotation logic as specified in link but it creates a new WritableBitmap object from source as follows: 在RotateBitMap方法中,我具有链接中指定的旋转逻辑,但是它从源创建了一个新的WritableBitmap对象,如下所示:

WritablBitmap target = new WritableBitmap(soure.width,source.height); //source is the bitmap passed in argument.

The problem is 问题是

PictureDecoder.decodejpeg --consumes 30 mb for my camera captured stream and creation of the new bitmap in rotate stream method is consuming 30 mb more.Resulting in hike of 60 mb application memory. PictureDecoder.decodejpeg-为我的相机捕获的流消耗30 mb,而在旋转流方法中创建新的位图则要多消耗30 mb,导致应用程序内存增加60 mb。

this is causing application crash due to memory in lower end(256mb) windows phone devices. 这是由于低端(256mb)Windows Phone设备中的内存导致应用程序崩溃。 Why is decoding jpeg take 30mb and rotation of stream 30mb.(I tried to source and target bitmaps in rotate stream method to null and forced gc but were of no use.Applications hardly get 60mb on devices.How can i cope up with this requirement?? 为什么解码jpeg占用30mb的空间并旋转30mb的流。(我试图通过旋转流的方法将位图的源和目标设置为null和强制gc,但没有用。应用程序在设备上几乎没有60mb的空间。我该如何应对这一要求??

Any ideas..?How to optimize memory consumption in these cases??? 任何想法..?如何在这些情况下优化内存消耗???

Note: I need to take bitmap from rotatestream method as result as I need to use that bitmap to save as jpeg with output size,quality. 注意:由于需要使用该位图以输出大小,质量保存为jpeg,因此需要从rotatestream方法中获取位图。

When you use JPEG decoding, you usually end up with a photo in it's full size. 使用JPEG解码时,通常会得到全尺寸的照片。 In case it was taken with 8MP (roughly 8000000 pixels) camera, the calculation is this: 如果是用8MP(大约8000000像素)的相机拍摄的,则计算结果如下:

8000000 * 32bits = 256 000 000 bits for one picture in memory (which is roughly around 30MB) 8000000 * 32位= 256 000 000位用于内存中的一张图片(大约30 MB左右)

(let me remind you that the HTC Titan II has a camera of 16MP, so if you used a photo in full size, it would take up roughly around 62MB in memory!!) (让我提醒您,HTC Titan II的相机为16MP,因此,如果您使用全尺寸的照片,则它将占用大约62MB的内存!)

Obviously, to create just one WriteableBitmap, you need 30MB. 显然,要仅创建一个WriteableBitmap,则需要30MB。 In order to manipulate the photo in some way, you usually can't do it in place. 为了以某种方式处理照片,通常无法就地进行处理。 In other words, you need to create a copy, and that's why it duplicates. 换句话说,您需要创建一个副本,这就是它重复的原因。 Windows Phone has a way of preventing such a big memory consumption by automatically lowering the resolution of the loaded picture, but only when you use it with BitmapImage and SetSource method which takes JPEG stream as a parameter. Windows Phone具有一种通过自动降低所加载图片的分辨率来防止如此大的内存消耗的方法,但仅当与BitmapImage和SetSource方法(将JPEG流作为参数)一起使用时,Windows Phone才可以使用。

I wrote about it some time ago in my article How to open and work with large photos on Windows Phone and it's mentioned in the performance considerations for Windows Phone 我前段时间在文章《 如何在Windows Phone上打开和使用大照片》中对此进行了介绍,并在Windows Phone性能注意事项中提到了该问题。

So, basically, you should cut down the size of the loaded photo (do you need it in full size anyway?) and the BitmapImage class can easily do it for you automatically keeping it under 2000x2000 px which is roughly 15MB (MAX), so you should be fine if you don't want to be bothered with that. 因此,基本上,您应该减小加载的照片的大小(是否需要全尺寸的照片?), BitmapImage类可以轻松实现此目的,因为您可以自动将其保持在2000x2000 px(约15MB(最大))以下,因此如果您不想为此烦恼,应该会没事的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM