在Android上调整大位图文件的大小以缩放输出文件 [英] Resize a large bitmap file to scaled output file on Android

查看:136
本文介绍了在Android上调整大位图文件的大小以缩放输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件中有一个很大的位图(例如3888x2592).现在,我想将该位图的大小调整为800x533并将其保存到另一个文件中. 我通常会通过调用Bitmap.createBitmap方法缩放位图,但是它需要源位图作为第一个参数,我无法提供,因为将原始图像加载到Bitmap对象中当然会超出内存(请参见此处.

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.decodeFile(file, options)来读取位图,并提供了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.

总结一下我的问题: 有没有办法读取10MP或更大的大图像文件并将其保存到新的图像文件中,并调整大小为新的特定宽度和高度,而不会出现OutOfMemory异常?

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.decodeFile(file, options)并将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:

  1. 计算可能的最大inSampleSize仍会产生比目标大的图像.
  2. 使用BitmapFactory.decodeFile(file, options)加载图像,并传入inSampleSize作为选项.
  3. 使用Bitmap.createScaledBitmap()调整为所需的尺寸.
  1. Calculate the maximum possible inSampleSize that still yields an image larger than your target.
  2. Load the image using BitmapFactory.decodeFile(file, options), passing inSampleSize as an option.
  3. Resize to the desired dimensions using Bitmap.createScaledBitmap().

这篇关于在Android上调整大位图文件的大小以缩放输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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