频道栏目
首页 > 资讯 > Android > 正文

Android Bitmap图片的压缩

17-06-21        来源:[db:作者]  
收藏   我要投稿

Android Bitmap图片的压缩,得到图片的一些方式:

//第一种方式:从资源文件中得到图片
        Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
  
//第二种方式:SD卡中得到图片(方法1)  String SDCarePath = Environment.getExternalStorageDirectory().toString(); String filePath = SDCarePath + "/" + "helloworld.png"; Bitmap rawBitmap1 = BitmapFactory.decodeFile(filePath, null);
//第二种方式:SD卡中得到图片(方法2)
        InputStream inputStream = getBitmapInputStreamFromSDCard("helloworld.png");
        Bitmap rawBitmap2 = BitmapFactory.decodeStream(inputStream);
压缩图片的一些方式:
// 得到图片原始的高宽
        int rawHeight = rawBitmap.getHeight();
        int rawWidth = rawBitmap.getWidth();
// 设定图片新的高宽
        int newHeight = 500;
        int newWidth = 500;
// 计算缩放因子
        float heightScale = ((float) newHeight) / rawHeight;
        float widthScale = ((float) newWidth) / rawWidth;
// 新建立矩阵
        Matrix matrix = new Matrix();
        matrix.postScale(heightScale, widthScale);
// 设置图片的旋转角度
//matrix.postRotate(-30);
// 设置图片的倾斜
//matrix.postSkew(0.1f, 0.1f);
//将图片大小压缩
//压缩后图片的宽和高以及kB大小均会变化

source 产生子位图的源位图
x 子位图第一个像素在源位图的X坐标 y 子位图第一个像素在源位图的y坐标 width 子位图每一行的像素个数 height 子位图的行数 matrix 对像素值进行变换的可选矩阵 filter 如果为true,源图要被过滤。该参数仅在matrix包含了超过一个翻转才有效 return 一个描述了源图指定子集的位图。 Bitmap
 Bitmap newBitmap = Bitmap.createBitmap(rawBitmap, 0, 0, rawWidth, rawWidth, matrix, true);
//压缩图片并保存到文件中
rawBitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream);

相关TAG标签
上一篇:Lenskit备忘录 - Item-Based协同过滤推荐算法笔记
下一篇:数据库SQLite常用语句
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站