日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當前位置:首頁 > 芯聞號 > 充電吧
[導讀]追加:按照寬高壓縮????????@SuppressLint("NewApi") public?static?Bitmap?getSmallBitmap(String?file_path)?{

追加:按照寬高壓縮


????????@SuppressLint("NewApi")
	public?static?Bitmap?getSmallBitmap(String?file_path)?{
		
		Bitmap?image?=?null;
		try?{
			
			BitmapFactory.Options?opt?=?new?BitmapFactory.Options();??
			opt.inPreferredConfig?=?Bitmap.Config.RGB_565;???
			opt.inPurgeable?=?true;??
			opt.inInputShareable?=?true;??
			opt.inSampleSize?=?2;
			//獲取資源圖片??
			FileInputStream?is?=?new?FileInputStream(new?File(file_path));
			image?=?BitmapFactory.decodeStream(is,?null,?opt);
			
			if(image.getHeight()>800?||?image.getWidth()>800){//這里判斷?寬高比例?大于?800
				int?width?=?image.getWidth();
				int?height?=?image.getHeight();
				int?tmp?=??800;
//				int?newHeigth?=?800;
				float?scale?=?1;
				int?tmp2?=?width>height??width:height;
				if(tmp2>tmp){
					scale?=?(float)tmp/(float)tmp2;
				}
				
				LogsUtil.log_error(ImageUtil.class,"tmp2"+tmp2+?"tmp"+tmp+"---s"+scale);
				
				Matrix?matrix?=?new?Matrix();
				matrix.postScale(scale,scale);
				Bitmap?bitmap?=?Bitmap.createBitmap(image,0,0,?width,?height,?matrix,?true);
				LogsUtil.log_error(ImageUtil.class,?bitmap.getByteCount()+"");//1920000
				
				if(bitmap.getByteCount()>1024*1024*5){//?判斷?如果?大于500kb?進行質(zhì)量壓縮
					
					return?compressImage(bitmap);//壓縮好比例大小后再進行質(zhì)量壓縮
					
				}
				return?bitmap;
			}
		
		}?catch?(FileNotFoundException?e)?{
			//?TODO?Auto-generated?catch?block
			e.printStackTrace();
		}
		return?image;
	}


第一:我們先看下質(zhì)量壓縮方法:

?

private?Bitmap?compressImage(Bitmap?image)?{?? ?? ????????ByteArrayOutputStream?baos?=?new?ByteArrayOutputStream();?? ????????image.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?? ????????????image.compress(Bitmap.CompressFormat.JPEG,?options,?baos);//這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中?? ????????????options?-=?10;//每次都減少10?? ????????}?? ????????ByteArrayInputStream?isBm?=?new?ByteArrayInputStream(baos.toByteArray());//把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中?? ????????Bitmap?bitmap?=?BitmapFactory.decodeStream(isBm,?null,?null);//把ByteArrayInputStream數(shù)據(jù)生成圖片?? ????????return?bitmap;?? ????}?? ?

第二:圖片按比例大小壓縮方法(根據(jù)路徑獲取圖片并壓縮):

?

private?Bitmap?getimage(String?srcPath)?{?? ????????BitmapFactory.Options?newOpts?=?new?BitmapFactory.Options();?? ????????//開始讀入圖片,此時把options.inJustDecodeBounds?設回true了?? ????????newOpts.inJustDecodeBounds?=?true;?? ????????Bitmap?bitmap?=?BitmapFactory.decodeFile(srcPath,newOpts);//此時返回bm為空?? ?????????? ????????newOpts.inJustDecodeBounds?=?false;?? ????????int?w?=?newOpts.outWidth;?? ????????int?h?=?newOpts.outHeight;?? ????????//現(xiàn)在主流手機比較多是800*480分辨率,所以高和寬我們設置為?? ????????float?hh?=?800f;//這里設置高度為800f?? ????????float?ww?=?480f;//這里設置寬度為480f?? ????????//縮放比。由于是固定比例縮放,只用高或者寬其中一個數(shù)據(jù)進行計算即可?? ????????int?be?=?1;//be=1表示不縮放?? ????????if?(w?>?h?&&?w?>?ww)?{//如果寬度大的話根據(jù)寬度固定大小縮放?? ????????????be?=?(int)?(newOpts.outWidth?/?ww);?? ????????}?else?if?(w?<?h?&&?h?>?hh)?{//如果高度高的話根據(jù)寬度固定大小縮放?? ????????????be?=?(int)?(newOpts.outHeight?/?hh);?? ????????}?? ????????if?(be?<=?0)?? ????????????be?=?1;?? ????????newOpts.inSampleSize?=?be;//設置縮放比例?? ????????//重新讀入圖片,注意此時已經(jīng)把options.inJustDecodeBounds?設回false了?? ????????bitmap?=?BitmapFactory.decodeFile(srcPath,?newOpts);?? ????????return?compressImage(bitmap);//壓縮好比例大小后再進行質(zhì)量壓縮?? ????}?? ?

第三:圖片按比例大小壓縮方法(根據(jù)Bitmap圖片壓縮):

?

private?Bitmap?comp(Bitmap?image)?{?? ?????? ????ByteArrayOutputStream?baos?=?new?ByteArrayOutputStream();????????? ????image.compress(Bitmap.CompressFormat.JPEG,?100,?baos);?? ????if(?baos.toByteArray().length?/?1024>1024)?{//判斷如果圖片大于1M,進行壓縮避免在生成圖片(BitmapFactory.decodeStream)時溢出???? ????????baos.reset();//重置baos即清空baos?? ????????image.compress(Bitmap.CompressFormat.JPEG,?50,?baos);//這里壓縮50%,把壓縮后的數(shù)據(jù)存放到baos中?? ????}?? ????ByteArrayInputStream?isBm?=?new?ByteArrayInputStream(baos.toByteArray());?? ????BitmapFactory.Options?newOpts?=?new?BitmapFactory.Options();?? ????//開始讀入圖片,此時把options.inJustDecodeBounds?設回true了?? ????newOpts.inJustDecodeBounds?=?true;?? ????Bitmap?bitmap?=?BitmapFactory.decodeStream(isBm,?null,?newOpts);?? ????newOpts.inJustDecodeBounds?=?false;?? ????int?w?=?newOpts.outWidth;?? ????int?h?=?newOpts.outHeight;?? ????//現(xiàn)在主流手機比較多是800*480分辨率,所以高和寬我們設置為?? ????float?hh?=?800f;//這里設置高度為800f?? ????float?ww?=?480f;//這里設置寬度為480f?? ????//縮放比。由于是固定比例縮放,只用高或者寬其中一個數(shù)據(jù)進行計算即可?? ????int?be?=?1;//be=1表示不縮放?? ????if?(w?>?h?&&?w?>?ww)?{//如果寬度大的話根據(jù)寬度固定大小縮放?? ????????be?=?(int)?(newOpts.outWidth?/?ww);?? ????}?else?if?(w?<?h?&&?h?>?hh)?{//如果高度高的話根據(jù)寬度固定大小縮放?? ????????be?=?(int)?(newOpts.outHeight?/?hh);?? ????}?? ????if?(be?<=?0)?? ????????be?=?1;?? ????newOpts.inSampleSize?=?be;//設置縮放比例?? ????//重新讀入圖片,注意此時已經(jīng)把options.inJustDecodeBounds?設回false了?? ????isBm?=?new?ByteArrayInputStream(baos.toByteArray());?? ????bitmap?=?BitmapFactory.decodeStream(isBm,?null,?newOpts);?? ????return?compressImage(bitmap);//壓縮好比例大小后再進行質(zhì)量壓縮?? }?

本站聲明: 本文章由作者或相關機構授權發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權益,請及時聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關閉