安卓:调整一个大的位图文件缩放输出文件 [英] Android: Resize a large bitmap file to scaled output file
问题描述
我有一个大的位图(例如3888x2592)在一个文件中。现在,我要调整位图到800x533,并将其保存到另一个文件。
我通常会缩放位图通过调用 Bitmap.createBitmap
方法,但它需要一个源位图作为第一个参数,这是我无法提供,因为加载原始图像成位图当然,对象将超出内存(请参阅这里,例如)。
I have a large bitmap (say 3888x2592) in a file. Now, I want to resize that bitmap to 800x533 and save it to another file.
I normally would scale the bitmap by calling Bitmap.createBitmap
method but it needs a source bitmap as the first argument, which I can't provide because loading the original image into a Bitmap object would of course exceed the memory (see here, for example).
我也无法读取位图,例如, BitmapFactory.de codeFILE(文件,选项)
,提供 BitmapFactory.Options.inSampleSize
,因为我想将它调整到一个确切的宽度和高度。使用 inSampleSize
将调整位图到972x648(如果我使用 inSampleSize = 4
)或778x518(如果我使用 inSampleSize = 5
,这是不是2连电源)。
I also can't read the bitmap with, for example, BitmapFactory.decodeFile(file, options)
, providing a BitmapFactory.Options.inSampleSize
, because I want to resize it to an exact width and height. Using inSampleSize
would resize the bitmap to 972x648 (if I use inSampleSize=4
) or to 778x518 (if I use inSampleSize=5
, which isn't even a power of 2).
我也想避免读取使用inSampleSize与图像,例如,972x648在第一步骤中,然后在第二步骤中它调整到恰好800x533,因为相对于原来的直接调整大小的质量会很差图像。
I would also like to avoid reading the image using inSampleSize with, for example, 972x648 in a first step and then resizing it to exactly 800x533 in a second step, because the quality would be poor compared to a direct resizing of the original image.
要总结一下我的问题: 有没有一种方法来读取与1000万像素以上的大型图像文件并将其保存到一个新的图像文件,调整到一个特定的新高度和宽度,没有得到一个内存不足的异常?
To sum up my question: Is there a way to read a large image file with 10MP or more and save it to a new image file, resized to a specific new width and height, without getting an OutOfMemory exception?
我也试过 BitmapFactory.de codeFILE(文件,选项)
并设置Options.outHeight和Options.outWidth值手动800和533,但它不这样的。
I also tried BitmapFactory.decodeFile(file, options)
and setting the Options.outHeight and Options.outWidth values manually to 800 and 533, but it doesn't work that way.
推荐答案
否。我想有人纠正我的爱,但我接受了负载/调整你尝试过作为一个折中的方法。
No. I'd love for someone to correct me, but I accepted the load/resize approach you tried as a compromise.
下面是步骤,任何人浏览:
Here are the steps for anyone browsing:
- 计算最大可能的
inSampleSize
仍然产生的图像比你的目标较大。 - 使用加载图像
BitmapFactory.de codeFILE(文件,选项)
,传递inSampleSize作为一个选项。 - 调整使用所需的尺寸
Bitmap.createScaledBitmap()
。
- Calculate the maximum possible
inSampleSize
that still yields an image larger than your target. - Load the image using
BitmapFactory.decodeFile(file, options)
, passing inSampleSize as an option. - Resize to the desired dimensions using
Bitmap.createScaledBitmap()
.
这篇关于安卓:调整一个大的位图文件缩放输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!