android图片压缩工具类
生活随笔
收集整理的這篇文章主要介紹了
android图片压缩工具类
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?? 好久沒(méi)寫(xiě)博客了,一方面是因?yàn)樽罱伊思覍?shí)習(xí)單位,很累基本上下班后就沒(méi)有打不起精神去學(xué)習(xí),另一方面我自己覺(jué)得寫(xiě)博客確實(shí)有點(diǎn)耗時(shí)間,趁著周六周日想花點(diǎn)時(shí)間研究下fresco,picass,Glide等框架,但是如何哪種框架,Bitmap總是基礎(chǔ),花了一上午的時(shí)間整理了下bitmap壓縮的工具類(lèi),這里分享一下
?
package com.example.liujian.bitmapdemo;import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.util.Log; import android.view.WindowManager;import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.IOException;/*** create by liujian 2015/11/10* 圖片壓縮工具類(lèi)*/ public class ImageResizerUtils {private static final String TAG = ImageResizerUtils.class.getSimpleName();private Context context;private WindowManager mWindowManager;public ImageResizerUtils(Context context) {this.context=context;}/*** 放大縮小圖片,指定寬高* @param bitmap* @param w* @param h* @return*/public Bitmap resizeBitmap(Bitmap bitmap, int w, int h) {int width = bitmap.getWidth();int height = bitmap.getHeight();Matrix matrix = new Matrix();float scaleWidht = ((float) w / width);float scaleHeight = ((float) h / height);matrix.postScale(scaleWidht, scaleHeight);Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,matrix, true);if (!bitmap.isRecycled()) {bitmap.recycle();}return newbmp;}/*** Matrix類(lèi)實(shí)現(xiàn)圖片放大縮小,指定寬,寬高比例跟原圖一致* @param bitmap * @param newWidth * @return */ public Bitmap resizeBitmap(Bitmap bitmap, int newWidth) {if (bitmap == null) return null; int w = bitmap.getWidth(); int h = bitmap.getHeight(); float temp = ((float) h) / ((float) w); int newHeight = (int) (newWidth * temp); float scaleWidth = ((float) newWidth) / w; float scaleHeight = ((float) newHeight) / h; Matrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); if (!bitmap.isRecycled()) {bitmap.recycle();}return resizedBitmap; }/*** 壓縮成屏幕能夠接受的大小* @param is* @return*/public Bitmap resizeBitmap(FileInputStream is) throws IOException {return resizeBitmap(is.getFD());}public Bitmap resizeBitmap(FileInputStream is,int reqWidth,int reqHeight) throws IOException {return resizeBitmap(is.getFD(),reqWidth,reqHeight);}public Bitmap resizeBitmap(FileDescriptor fd) {final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFileDescriptor(fd, null, options);options.inSampleSize = calculateInWindowSize(options);options.inJustDecodeBounds = false;return BitmapFactory.decodeFileDescriptor(fd, null, options);}/*** 壓縮方法對(duì)FileInputStream的縮放存在問(wèn)題,原因是FileInputStream是一種有序的* 文件流,而兩次decodeStream調(diào)用影響了文件流的位置屬性,導(dǎo)致第二次BitmapFactory.* decodeStream時(shí)得到是null。我們可以通過(guò)文件流來(lái)得到它所對(duì)應(yīng)的文件描述符,* @param fd:FileDescriptor df = is.getFD();* @param reqWidth:所需圖片寬(像素)* @param reqHeight:所需圖片高(像素)* @return*/public Bitmap resizeBitmap(FileDescriptor fd,int reqWidth, int reqHeight) {final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFileDescriptor(fd, null, options);options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);options.inJustDecodeBounds = false;return BitmapFactory.decodeFileDescriptor(fd, null, options);}/**** @param res:Resources對(duì)象* @param resId:資源文件id* @return*/public Bitmap resizeBitmap(Resources res, int resId) {final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;// 不去解析真正的位圖,只是去獲取這個(gè)文件的頭文件信息BitmapFactory.decodeResource(res, resId, options);// 得到壓縮的縮放比options.inSampleSize = calculateInWindowSize(options);options.inJustDecodeBounds = false;return BitmapFactory.decodeResource(res, resId, options);}/**** @param res:Resources對(duì)象* @param resId:資源文件id* @param reqWidth:縮放的目標(biāo)Bitmao的寬度* @param reqHeight:縮放的目標(biāo)Bitmap的高度* @return*/public Bitmap resizeBitmap(Resources res, int resId,int reqWidth, int reqHeight) {final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;// 不去解析真正的位圖,只是去獲取這個(gè)文件的頭文件信息BitmapFactory.decodeResource(res, resId, options);// 得到壓縮的縮放比options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);options.inJustDecodeBounds = false;return BitmapFactory.decodeResource(res, resId, options);}/*** 根據(jù)路徑壓縮成指定的寬高* @param path* @param reqWidth* @param reqHeight* @return*/public Bitmap resizeBitmap(String path,int reqWidth,int reqHeight){BitmapFactory.Options opts=new BitmapFactory.Options();//解析位圖的附加條件opts.inJustDecodeBounds=true;BitmapFactory.decodeFile(path,opts);opts.inSampleSize=calculateInSampleSize(opts,reqWidth,reqHeight);opts.inJustDecodeBounds=false;return BitmapFactory.decodeFile(path,opts);}/*** 根據(jù)路徑壓縮成屏幕可以接受的大小* @param path* @return*/public Bitmap resizeBitmap(String path){BitmapFactory.Options opts=new BitmapFactory.Options();//解析位圖的附加條件opts.inJustDecodeBounds=true;BitmapFactory.decodeFile(path,opts);opts.inSampleSize=calculateInWindowSize(opts);opts.inJustDecodeBounds=false;return BitmapFactory.decodeFile(path,opts);}/*** 根據(jù)需要的圖片寬高(px像素)來(lái)壓縮 返回縮放比 縮放比大于1時(shí),比如為2,* 壓縮后的圖片其寬高均為原圖大小的1/2,其所占的內(nèi)存大小也為原圖的1/4* @param options* @param reqWidth:壓縮的目標(biāo)寬* @param reqHeight:壓縮的目標(biāo)高* @return 返回縮放比*/private int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {if (reqWidth <= 0 || reqHeight <= 0) {return 1;}// 得到圖片的寬高final int height = options.outHeight;final int width = options.outWidth;Log.d(TAG, "origin, w= " + width + " h=" + height);int inSampleSize = 1;if (height > reqHeight || width > reqWidth) {final int halfHeight = height / 2;final int halfWidth = width / 2;while ((halfHeight / inSampleSize) >= reqHeight&& (halfWidth / inSampleSize) >= reqWidth) {inSampleSize *= 2;}}Log.d(TAG, "sampleSize:" + inSampleSize);return inSampleSize;}/*** 壓縮成屏幕能夠接受的大小的縮放比* @param options* @return*/private int calculateInWindowSize(BitmapFactory.Options options){//得到屏幕寬高if(mWindowManager==null){mWindowManager=((Activity)context).getWindowManager();}int windowWidth=mWindowManager.getDefaultDisplay().getWidth();int windowHeight=mWindowManager.getDefaultDisplay().getHeight();int bitmapWidth=options.outWidth;int bitmapHeight=options.outHeight;int dx=bitmapWidth/windowWidth;int dy=bitmapHeight/windowHeight;int scale=1;if(dx>dy&&dy>1){scale=dx;}if(dy>dx&&dy>1){scale=dy;}return scale;} }總結(jié)
以上是生活随笔為你收集整理的android图片压缩工具类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 自学php看什么视频,PHP自学要多久?
- 下一篇: uniapp 开发安卓App实现高德地图