获取图片的采样率
来源:互联网 发布:国际版淘宝网店怎么开 编辑:程序博客网 时间:2024/05/29 16:10
获取图片的采样率
使用Bitmapactory解码(decode)资源的时候,系统会尝试分配内存,这个时候如果图片的内存过大,就容易产生内存泄漏的问题。可以使用设置图片的采样率的方法来限制读取到的图片的大小,也就是分辨率的大小。设置BitmapFactory.Opitions的inJustDecodeBounds属性为true,就可以在不分配内存的情况下,获取该图片的大小和类型。
BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeResource(getResources(), R.id.myimage, options);int imageHeight = options.outHeight;int imageWidth = options.outWidth;String imageType = options.outMimeType;
获取到高度和宽度之后,就可以按照我们的需要的高度和宽度,来计算图片的缩放比,得出图片的采样率
public static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize;}
获取采样率之后,重新解码图片,就可以根据需要的大小获取对应分辨率的图片
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options);}
0 0
- 获取图片的采样率
- 计算图片采样率压缩图片
- Android图片使用采样率压缩
- 图片压缩(计算采样率)
- 固定的音频采样率
- 采样率
- 采样率
- bitmap.compress(图片压缩的两种方式)(1,质量压缩;2,采样率压缩)
- 安卓图片的压缩,尺寸,质量,采样率和微信压缩
- Android设置采样率高效加载图片
- 采样率与频率的关系
- 音频的采样率与位数
- 示波器的采样率和存储深度
- Linux音频编码的采样率模拟实现
- 关于音频采样率以及比特率的问题
- 什么是音频的采样率和采样大小
- 什么是音频的采样率和采样大小
- 音频的采样率和采样大小
- IRF学习笔记-配置
- 绝对定位和相对定位详解
- svg字符串转图片
- BZOJ_P2048 [2009国家集训队]书堆(调和级数+欧拉常数)
- swing中setSize和setPreferredSize区别
- 获取图片的采样率
- 重新编译内核步骤
- C++多重继承
- 浅谈Python程序和C程序的整合
- 使用Core Animation优化UIKit性能
- swing改变UI正确的做法
- 模仿探探(百合网,珍爱网)卡片左右滑动效果,滑动流畅,卡片view无限重生
- iOS 四舍五入问题
- Linux学习之软连接硬链接