您好,登錄后才能下訂單哦!
// 由url獲取bitmap對(duì)象
is = conn.getInputStream(); // 簡(jiǎn)寫方法: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;// 這里設(shè)置高度為800f
// int ww = 480;// 這里設(shè)置寬度為480f
// 縮放比。由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(jì)算即可
float be = 1;// be=1表示不縮放
if ( w > ww)
{// 如果寬度大的話根據(jù)寬度固定大小縮放
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)
{// 如果高度高的話根據(jù)高度固定大小縮放
be = (int) (h / hh);
Log.e("matrix2.", "matrix2." + be);
}
if (be <= 0)
be = 1;
// newOpts.inSampleSize = (int) be;//設(shè)置縮放比例
Log.e("matrix.postScale", "matrix.postScale" + be);
matrix.postScale(be, be);
// newOpts.inSampleSize = (int) be;// 設(shè)置縮放比例
// 重新讀入圖片,注意此時(shí)已經(jīng)把options.inJustDecodeBounds 設(shè)回false了
// bitmap = BitmapFactory.decodeStream(is, null ,
// newOpts);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
newbmp = compressImage(bitmap);// 壓縮好比例大小后再進(jìn)行質(zhì)量壓縮
mCacheMap.put(strURL, newbmp);
/**
* 質(zhì)量壓縮
*
* @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);// 質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 100)
{ // 循環(huán)判斷如果壓縮后圖片是否大于100kb,大于繼續(xù)壓縮
baos.reset();// 重置baos即清空baos
options -= 10;// 每次都減少10
p_w_picpath.compress(Bitmap.CompressFormat.JPEG, options, baos);// 這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream數(shù)據(jù)生成圖片
// 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;
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。