bitmap过大无法显示图片的问题 - z
2017-08-24 11:57
435 查看
public Bitmap ratio(Bitmap image, float pixelW, float pixelH) {
Bitmap bitmap = null;
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, os);
if( os.toByteArray().length / 1024>1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
os.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, 50, os);//这里压缩50%,把压缩后的数据存放到baos中
}
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
newOpts.inPreferredConfig = Config.RGB_565;
bitmap = BitmapFactory.decodeStream(is, null, newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = pixelH;// 设置高度为240f时,可以明显看到图片缩小了
float ww = pixelW;// 设置宽度为120f,可以明显看到图片缩小了
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0) be = 1;
newOpts.inSampleSize = be;//设置缩放比例
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
is = new ByteArrayInputStream(os.toByteArray());
bitmap = BitmapFactory.decodeStream(is, null, newOpts);
//压缩好比例大小后再进行质量压缩
//return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大,反而耗资源,删除
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Log.e("--ImageFactory.ratio--", e.toString());
}
return bitmap;
}
Bitmap bitmap = null;
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, os);
if( os.toByteArray().length / 1024>1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
os.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, 50, os);//这里压缩50%,把压缩后的数据存放到baos中
}
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
newOpts.inPreferredConfig = Config.RGB_565;
bitmap = BitmapFactory.decodeStream(is, null, newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = pixelH;// 设置高度为240f时,可以明显看到图片缩小了
float ww = pixelW;// 设置宽度为120f,可以明显看到图片缩小了
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0) be = 1;
newOpts.inSampleSize = be;//设置缩放比例
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
is = new ByteArrayInputStream(os.toByteArray());
bitmap = BitmapFactory.decodeStream(is, null, newOpts);
//压缩好比例大小后再进行质量压缩
//return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大,反而耗资源,删除
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Log.e("--ImageFactory.ratio--", e.toString());
}
return bitmap;
}
相关文章推荐
- 【史上最简单】android硬件加速——关于图片过大无法显示,直接变黑色问题
- 部分图片上传数据库后无法显示问题
- UIImageView无法显示图片的问题
- 两步轻松搞定 编辑器上传图片尺寸过大 超出前台显示区域问题!
- struts2中action跳转导致图片相对路径无法显示问题
- MyEclipse中创建WEB工程,运用<img>标签显示图片等,但无法显示问题
- word无法显示图片的问题终于搞定!oh yeah!
- ubuntu下使用PIL中的show函数,无法显示图片的问题
- 个别图片IE中无法显示问题
- office2007 word 图片无法显示问题解决了
- j2ee中ueditor上传图片无法显示的问题
- Qt Gif图片在其他机器上无法显示问题
- 图片验证码有时会出现无法显示的问题
- HTML:使用绝对路径插入图片后在浏览器中无法显示的问题
- 无法显示TabHost的setIndicator设置的图片的问题解决办法
- 修复CSDN Blog中以前图片无法显示的问题
- Django1.4上传图片无法在页面显示的问题
- 在ios中使用opencv将彩色图片转换成为灰度图,uiimageview无法显示问题
- 图片url带有中文名无法显示的问题
- Asp.net2.0部署时TreeView控件无法正常显示没有图片的问题