zoukankan      html  css  js  c++  java
  • ANDROID笔记:根据长宽实现图片压缩

       /**
         * 得到一定高度的图片
         * 
         * @param height
         * @param imagePath
         * @return
         */
        public Bitmap getImageByHeight(float height, String imagePath) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            // 获取这个图片的宽和高
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options); // 此时返回bm为空
            options.inJustDecodeBounds = false;
            // 计算缩放比
            int be = (int) (options.outHeight / height);
            if (be <= 0)
                be = 1;
            options.inSampleSize = be;
            // 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false
            bitmap = BitmapFactory.decodeFile(imagePath, options);
            return bitmap;
    
        }
    
        /**
         * 得到一定宽度的图片
         * 
         * @param width
         * @param imagePath
         * @return
         */
        public Bitmap getImageByWidth(float width, String imagePath) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            // 获取这个图片的宽和高
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options); // 此时返回bm为空
            options.inJustDecodeBounds = false;
            // 计算缩放比
            int be = (int) (options.outWidth / width);
            if (be <= 0)
                be = 1;
            options.inSampleSize = be;
            // 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false
            bitmap = BitmapFactory.decodeFile(imagePath, options);
            return bitmap;
    
        }
        /**
         * 得到一定宽度或高度的图片
         * @param width 宽度
         * @param height 高度
         * @param srcPath 图片路径
         * @return Bitmap
         */
        public static Bitmap getBitmap(String srcPath,float width,float height) {  
            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;  
           
            //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可  
            int be = 1;//be=1表示不缩放  
            if (w > h && w > width) {//如果宽度大的话根据宽度固定大小缩放  
                be = (int) (newOpts.outWidth / width);  
            } else if (w < h && h > height) {//如果高度高的话根据高度固定大小缩放  
                be = (int) (newOpts.outHeight / height);  
            }  
            if (be <= 0)  
                be = 1;  
            newOpts.inSampleSize = be;//设置缩放比例  
            //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了  
            bitmap = BitmapFactory.decodeFile(srcPath, newOpts);  
            return bitmap;
        }
  • 相关阅读:
    程序员要善于在工作中找到偷懒的办法
    关于count(1) 和 count(*)
    前端设计+程序开发那点事
    关于MySQL Connector/C++那点事儿
    windows下编译php5.2.17这是闹哪样?
    easyui使用时出现这个Uncaught TypeError: Cannot read property 'combo' of undefined
    视频文件自动转rtsp流
    Jenkins Pipeline如何动态的并行任务
    Jenkins的Dockerfile中如何批量迁移原Jenkins安装的插件
    Groovy中json的一些操作
  • 原文地址:https://www.cnblogs.com/afluy/p/3385306.html
Copyright © 2011-2022 走看看