android获取图片尺寸的两种方法以及bitmap的缩放

您所在的位置:网站首页 如何计算bmp文件大小尺寸的方法 android获取图片尺寸的两种方法以及bitmap的缩放

android获取图片尺寸的两种方法以及bitmap的缩放

2024-07-03 20:22| 来源: 网络整理| 查看: 265

//Uri.parse("file://"+result.getImage().getCompressPath())) String path=uri.getPath(); Log.e("图片路径",path+""); SpannableString spannableString=new SpannableString(path); //方法一:通过uri把图片转化为bitmap的方法 Bitmap bitmap= BitmapFactory.decodeFile(path); int height= bitmap.getHeight(); int width= bitmap.getWidth(); Log.e("通过bitmap获取到的图片大小","width:"+width+"height"+height); //方法二:使用Options类来获取 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;//这个参数设置为true才有效, Bitmap bmp = BitmapFactory.decodeFile(path, options);//这里的bitmap是个空 if(bmp==null){ Log.e("通过options获取到的bitmap为空","==="); } int outHeight=options.outHeight; int outWidth= options.outWidth; Log.e("通过Options获取到的图片大小","width:"+outWidth+"height"+outHeight);

关于两种方法:第一种直接把bitmap加载到内存中,通过对bitmap的测量,得出宽高,由于这个方法直接把图片引入内存,如果图片过大,将会引发OOM;

第二种方法:bitmap.options类为bitmap的裁剪类,通过他可以实现bitmap的裁剪;如果不设置裁剪后的宽高和裁剪比例,返回的bitmap对象将为空,但是这个对象存储了原bitmap的宽高信息。

打log输出信息如下:

缩放:

Bitmap bitmap=null; BitmapFactory.Options options=new BitmapFactory.Options(); options.inSampleSize=2; options.inJustDecodeBounds = false; if(path.equals("a1")){ bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.a1,options); inSampleSize表示缩放比例



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3