zoukankan      html  css  js  c++  java
  • Android 图片的压缩

    /**
    * TODO filePath:图片路径
    */
    public Bitmap compressToBitmap(String filePath) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);
    options.inSampleSize = calculateInSampleSize(options, lastUploadImgInfo.maxWidth,lastUploadImgInfo.maxWidth);
    options.inJustDecodeBounds = false;
       return BitmapFactory.decodeFile(filePath, options);
    }


    /**
    *
    * 计算采样率
    */

    public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {

    // Calculate ratios of height and width to requested height and
    // width
    final int heightRatio = Math.round((float) height / (float) reqHeight);
    final int widthRatio = Math.round((float) width / (float) reqWidth);

    inSampleSize = heightRatio > widthRatio ? heightRatio : widthRatio;
    }
    return inSampleSize;
    }

    /**
    * 获取图片旋转角度
    */
    public static int getDegree(String path) {
    int degree = 0;
    try {
    ExifInterface exifInterface = new ExifInterface(path);
    int orientation = exifInterface.getAttributeInt(
    ExifInterface.TAG_ORIENTATION,
    ExifInterface.ORIENTATION_NORMAL);
    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
    degree = 90;
    break;
    case ExifInterface.ORIENTATION_ROTATE_180:
    degree = 180;
    break;
    case ExifInterface.ORIENTATION_ROTATE_270:
    degree = 270;
    break;
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    return degree;
    }
    /**
    * 将图片纠正到正确方向
    *
    * @param degree : 图片被系统旋转的角度
    * @param bitmap : 需纠正方向的图片
    * @return 纠向后的图片
    */
    public static Bitmap rotateBitmap(int degree, Bitmap bitmap) {
    Matrix matrix = new Matrix();
    matrix.postRotate(degree);

    Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
    bitmap.getHeight(), matrix, true);
    return bm;
    }


  • 相关阅读:
    js-封装几个常用函数
    js获取地址栏的几种方法
    vue 子组件传值给父组件,兄弟组件传参以及实现动态组件
    实现同一个组件页面通过不同的tab标签页打开
    echarts柱状图一组数不同柱子的颜色修改
    echarts实现环形图
    vue 中JSONPath的使用方法之一
    vue element-ui 左侧菜单栏 el-menu属性实现动态菜单
    vue 前端服务器代理,proxyTable简要叙述
    分享一篇IBN(Intent-based networking)调研报告
  • 原文地址:https://www.cnblogs.com/x-bing/p/5717083.html
Copyright © 2011-2022 走看看