从文件加载大量的图片,并添加水印 [英] Load large picture from file and add watermark

查看:247
本文介绍了从文件加载大量的图片,并添加水印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图寻找到装入一个有效的方式位图含有任意大的图像文件,将水印(标志)超过它,将它保存回文件。

我知道的内存问题,都与负责Android上一个大的位图。我不想加载图像的较小的样本,因为我需要将其保存回文件时保持相同的质量。

任何帮助是AP preciated。

解决方案
  

使用BitmapFactory.de codeFILE(文件)会导致内存不足的问题解码大照片时

的OutOfMemoryError 发生的原因是堆空间为您的要求足够大的任何一个模块。在Dalvik垃圾收集器是一种非压缩收集器,所以堆将得到裂成大量的内存更小的块。

如果所有的图片将是相同的分辨率,你的的minSdkVersion 是11或更高,然后分配位图它当你的进程启动(例如,的onCreate()自定义应用程序类的),和使用与 inBitmap BitmapFactory.Options 。您试图分配位图应该会成功的早期,你的堆被碎片化之前,如果图像可以容纳在堆摆在首位。

如果他们不会全部相同的分辨率,但你知道的最大分辨率,和你的的minSdkVersion 是19或更高,然后做同样的事情,因为我上面描述。 Android 4.4系统的扩展 inBitmap 来支持位图比您加载的图像大小相同或更大,而在此之前它不得不完全匹配的大小。

如果没有这是可能的:

  • 使用安卓largeHeap =真正的来获得API等级11+的设备更大的堆,或

  • 将图像处理逻辑的NDK,作为本机内存分配不会对Dalvik的堆限制数,或

  • 将图像处理逻辑,专用短暂的过程,所以你得到一个干净的独立的堆在一个时间点,当你需要处理特定图像

而后面提到的三个选项在你的应用程序和其他效果的系统内存消耗方面的成本给用户。

I am trying to find an efficient way to load a Bitmap from a file containing an arbitrarily large image, place a watermark (a logo) over it, and save it back to file.

I am aware of the memory problems that come with dealing with a large bitmap on Android. I don't want to load a smaller sample of the image, because I need to keep the same quality when saving it back to file.

Any help is appreciated.

解决方案

Using BitmapFactory.decodeFile(File) leads to OutOfMemory issues when decoding large pictures

OutOfMemoryError occurs because there is no single block of heap space big enough for your request. The Dalvik garbage collector is a non-compacting collector, and so the heap will get fragmented into lots of smaller blocks of memory.

If all of the pictures will be the same resolution, and your minSdkVersion is 11 or higher, then allocate the Bitmap for it when your process starts up (e.g., in onCreate() of a custom Application class), and use that with inBitmap on your BitmapFactory.Options. Your attempt to allocate the Bitmap should succeed early on, before your heap gets fragmented, if the image can fit in the heap in the first place.

If they will not all be the same resolution, but you know the maximum resolution, and your minSdkVersion is 19 or higher, then do the same thing as I describe above. Android 4.4 extended inBitmap to support a Bitmap that is the same size or bigger than the image you are loading, whereas before it had to match the size exactly.

If none of that is possible:

  • Use android:largeHeap="true" to get a larger heap on API Level 11+ devices, or

  • Move your image-processing logic to the NDK, as native memory allocations do not count against the Dalvik heap limit, or

  • Move your image-processing logic to a dedicated short-lived process, so you get a clean separate heap at the point in time when you need to process a particular image

Those latter three options have costs to the user in terms of the system RAM consumption of your app and other effects.

这篇关于从文件加载大量的图片,并添加水印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆