Android Image压缩工具类

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/** * Image压缩工具类 * * @author yzy * */
public class ImageUtils {
    /** * 按压缩比进行压缩:实际大小不一定 FixHeight: 指定不超过高度 FixWidth: 指定不超过宽度 FixAll: 不超过指定大小 * 固定宽高:直接设置宽高(待测试) FixFixWidth FixFixHeight FixFixAll * * @author yzy * */
    public enum ZoomType { FixHeight, FixWidth, FixAll, FixFixWidth, FixFixHeight, FixFixAll }

    /** 获取压缩image之后的bitmap **/
    public static Bitmap zoomImage(String srcPath, int width, int height, ZoomType zoomType) {
        switch (zoomType) {
            case FixHeight: { }
            case FixWidth: { }
            case FixAll: {
                return compressImage(getZoomBitmap(srcPath, width, height, zoomType));
            }
            case FixFixWidth: { }
            case FixFixHeight: { }
            case FixFixAll: {
                return compressImage(zoomImageBitmapFix(srcPath, width, height, zoomType));
            }
            default:
                return null;
        }
    }
    
    /** 获取压缩image之后的beyt **/
    public static byte[] zoomImageBytes(String srcPath, int width, int height, ZoomType zoomType) {
        switch (zoomType) {
            case FixHeight: { }
            case FixWidth: { }
            case FixAll: {
                return getCompressImageByte(getZoomBitmap(srcPath, width, height, zoomType));
            }
            case FixFixWidth: { }
            case FixFixHeight: { }
            case FixFixAll: {
                return getCompressImageByte(zoomImageBitmapFix(srcPath, width, height, zoomType));
            }
            default: return null;
        }
    }

    /** 压缩质量 **/
    private static Bitmap compressImage(Bitmap image) {
        ByteArrayInputStream isBm = new ByteArrayInputStream( getCompressImageByte(image));// 把压缩后的数据baos存放到ByteArrayInputStream中
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
        // 把ByteArrayInputStream数据生成图片
        return bitmap;
    }

    /** 获取压缩之后的字节 **/
    private static byte[] getCompressImageByte(Bitmap image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        int options = 100;
        while (baos.toByteArray().length / 1024 > 100) {
            baos.reset(); options -= 10;
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);
        }
        return baos.toByteArray();
    }

    /** 获取压缩后的bitmap **/
    private static Bitmap getZoomBitmap(String srcPath, int width, int height, ZoomType zoomType) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = true;
        // 开始读入图片,options.inJustDecodeBounds // 设回true // 只读出图片的长和宽,不会真正返回bitmap,设置为false,才会真正返回bitmap
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        newOpts.inSampleSize = getSampleSize(getSampleSize(newOpts, width, height, zoomType));
        // 只能取值2的幂
        newOpts.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(srcPath, newOpts); return bitmap;
    }

    /** * 计算压缩比 * *
     * @param options 图片原始大小参数
     * @param maxWidth 最大宽度
     * @param maxHeight 最大高度
     * * @return */
    private static int getSampleSize(BitmapFactory.Options options, int maxWidth, int maxHeight, ZoomType zoomType) {
        int width = options.outWidth;
        int height = options.outHeight;
        int inSampleSize = 1;
        switch (zoomType) {
            case FixWidth: {
            if (width > maxWidth) {
                inSampleSize = width % maxWidth == 0 ? width / maxWidth : width / maxWidth + 1; }
                break;
            }
        case FixHeight: {
            if (height > maxHeight) {
                inSampleSize = height % maxHeight == 0 ? height / maxHeight : height / maxHeight + 1;
            }
         break;
        }
        case FixAll: {
            if (height > maxHeight || width > maxWidth) {
                // 当压缩后的大小小于当前大小时,计算压缩比,否则原大小显示
                int heightScale = height % maxHeight == 0 ? height / maxHeight : height / maxHeight + 1;
                int widthScale = width % maxWidth == 0 ? width / maxWidth : width / maxWidth + 1;
                inSampleSize = heightScale > widthScale ? heightScale : widthScale;
                // 按长宽压缩比大的比例来进行压缩
            } break;
        }
        default: break;
        }
        return inSampleSize;
    }

    public static Bitmap zoomImage(String srcPath, int width, int height) {
        return zoomImage(srcPath, width, height, ZoomType.FixAll);
    }

    public static byte[] getZoomImageBytes(String srcPath, int width, int height) {
        return zoomImageBytes(srcPath, width, height, ZoomType.FixAll);
    }

    public static Bitmap zoomImage(String srcPath) {
        return zoomImage(srcPath, 480, 800, ZoomType.FixAll);
    }

    public static byte[] getZoomImageBytes(String srcPath) {
        return zoomImageBytes(srcPath, 480, 800, ZoomType.FixAll);
     }

     /** 固定长宽计算方式 **/
     private static Bitmap zoomImageBitmapFix(String srcPath, int width, int height, ZoomType zoomType) {
         BitmapFactory.Options options = new BitmapFactory.Options();
         options.inJustDecodeBounds = true;
         Bitmap bitmap = BitmapFactory.decodeFile(srcPath, options);
         int outWidth = options.outWidth; int outHeight = options.outHeight;
         options.inJustDecodeBounds = false;
         int afterWidth = 0;
         int afterHeight = 0;
         switch (zoomType) {
             case FixFixWidth: {
                 afterWidth = width; afterHeight = width * outHeight / outWidth;
                 break;
             }
             case FixFixHeight: {
                 afterHeight = height; afterWidth = height * outWidth / outHeight;
                 break;
             }
             case FixFixAll: {
                 afterWidth = width; afterHeight = height;
                 break;
             }
             default: break;
         }
         options.outHeight = afterHeight;
         options.outWidth = afterWidth;
         bitmap = BitmapFactory.decodeFile(srcPath, options);
         return bitmap;
     }

     /** 调整设置SampleSize **/
     private static int getSampleSize(int size) {
         if ((size & (size - 1)) == 0) {// 表示该数为2的指数
             return size;
         }
         return size * 2;
     }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、Android显示GIF动画 GifView GifView 是一个为了解决android中现在没有直接显示gif的view,只能通过mediaplay来显示这个问题的项目,其用法和 ImageView一样,支持gif图片 使用方法:1-把GifView.jar加入你的项目。2-在xml中配置GifView的基本属性,GifView继承自View类,和Button、ImageView一样是一个UI控件。 如: 3-在代码中配置常用属性: // 从xml中得到GifView的句柄 gf1 = (GifView) findViewById(R.id.gif1); // 设置Gif图片源 gf1.setGifImage(R.drawable.gif1); // 添加监听器 gf1.setOnClickListener(this); // 设置显示的大小,拉伸或者压缩 gf1.setShowDimension(300, 300); // 设置加载方式:先加载后显示、边加载边显示、只显示第一帧再显示 gf1.setGifImageType(GifImageType.COVER); GifView的Jar包共有四个类: GifAction.java 观察者类,监视GIF是否加载成功 GifFrame.java 里面三个成员:当前图片、延时、下张Frame的链接。 GifDecoder.java 解码线程类 GifView.java 主类,包括常用方法,如GifView构造方法、设置图片源、延迟、绘制等。 2、Calendar.v0.5.0 是 Android 平台的一个日历显示组件。 3、CWAC EndlessAdapter 是 Android 上一个可以无限往下滑进行列表数据加载的控件。 4、Android Horizontal ListView 是 Android 上一个水平滑动的 ListView 组件。 5、Android ViewBadger 视图布局。 6、滑动刷新的ListView Android PullToRefresh 为 Android 应用提供一个向下滑动即刷新列表的功能,就两个目标文件。 7、pakerfeldt-android-viewflow 是 Android 平台上一个视图切换的效果库。ViewFlow 相当于 Android UI 部件提供水平滚动的 ViewGroup,使用 Adapter 进行条目绑定。 8、Android 导航菜单 RibbonMenu 是 Android 上的一个导航菜单组件。就三个目标文件,菜单项直接在 XML 中定义,可添加文本和图标。 9、Android的UI工具包 android-ui-utils 是一个工具包用来帮助设计和开发 Android 用户界面,包含三个单独的工具:Android Asset Studio用户界面原型模具,Android 设计预览,时常需要重复确认程序版面设计状况的 Android App 开发者,应该会爱上这个轻量级的 Java 程序:Andorid Design Preview 工具,通过 USB 连接之后,只要简单的在计算机中选取您想要显示的程序版面范围,就可将镜像结果直接显示于手机装置之上。 10、Android的ui开发类库 GreenDroid 是一个Android的ui开发类库,能够使你的Android开发更加简便和快捷。 11、Android滑动式菜单 SlidingMenu 是 Android 上实现类似 Facebook 和 Path 2.0 滑动式菜单的组件。 12、AsyncImageView 是 Android 上的一个异步从网络上获取图片并进行浏览的开源组件,可自动在本地进行缓存。该项目是 GreenDroid 的一部分。 13、仿Path按钮动画效果 PathButton 仿照Path应用首页左下角的Button动画效果写了个简单的Demo,由于数学不好,坐标总是和理想有出入,只是大致实现了动画效果,若果有人能把坐标算对,那么修改我的demo就能做成类似Path的那种动画效果!希望大家出点力帮着优化一下,并分享出来! 14、Android Intent开发包 OpenIntents Ope

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值