温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

根据屏幕缩放图片

发布时间:2020-06-21 21:44:41 来源:网络 阅读:409 作者:stlong515 栏目:开发技术

// 由url获取bitmap对象

is = conn.getInputStream(); // 简写方法:is = params[0].openStream()

BitmapFactory.Options newOpts = new BitmapFactory.Options();

bitmap = BitmapFactory.decodeStream(is, null ,

newOpts);

// newOpts.inJustDecodeBounds = false;

// bitmap = BitmapFactory.decodeStream(is);

newOpts.inJustDecodeBounds = false;

Matrix matrix = new Matrix();

int w = newOpts.outWidth;

int h = newOpts.outHeight;

WindowManager manage = getWindowManager();

Display display = manage.getDefaultDisplay();

float hh = display.getHeight();

float ww = display.getWidth();



// Log.e("display.getWidth()t", "display.getWidth()" + ww);

// Log.e("display.getHeight()", "display.getHeight()" + hh);

// Log.e("bitmap.getWidth()", "bitmap.getWidth()" + w);

// Log.e("bitmap.getHeight()", "bitmap.getHeight()" + h);

// int hh = 800;// 这里设置高度为800f

// int ww = 480;// 这里设置宽度为480f

// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可

float be = 1;// be=1表示不缩放

if ( w > ww)

{// 如果宽度大的话根据宽度固定大小缩放

be = (float) (w / ww);

Log.e("matrix0.", "matrix0." + be);

newbmp = compressImage(bitmap);

return newbmp;

}

else if (w < ww )

{

be = (float) (ww / w);

Log.e("matrix1.", "matrix1." + be);

}

else if (w < h && h > hh)

{// 如果高度高的话根据高度固定大小缩放

be = (int) (h / hh);

Log.e("matrix2.", "matrix2." + be);

}

if (be <= 0)

be = 1;

// newOpts.inSampleSize = (int) be;//设置缩放比例

Log.e("matrix.postScale", "matrix.postScale" + be);

matrix.postScale(be, be);

// newOpts.inSampleSize = (int) be;// 设置缩放比例

// 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了

// bitmap = BitmapFactory.decodeStream(is, null ,

// newOpts);

bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);

newbmp = compressImage(bitmap);// 压缩好比例大小后再进行质量压缩

mCacheMap.put(strURL, newbmp);



/**

* 质量压缩

*

* @param p_w_picpath

* @return

*/

private Bitmap compressImage(Bitmap p_w_picpath)

{


ByteArrayOutputStream baos = new ByteArrayOutputStream();

p_w_picpath.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中

int options = 100;

while (baos.toByteArray().length / 1024 > 100)

{ // 循环判断如果压缩后图片是否大于100kb,大于继续压缩

baos.reset();// 重置baos即清空baos

options -= 10;// 每次都减少10

p_w_picpath.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中


}

ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中

Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片

// try {

// FileOutputStream out = new

// FileOutputStream(Environment.getExternalStorageDirectory()

// + "/faceImage1.jpg");

// Log.i("path",

// Environment.getExternalStorageDirectory()+"/faceImage1.jpg");

// bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);

// }

// catch (Exception e) {

// e.printStackTrace();

// }

return bitmap;

}



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI