• 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;
    }


  • 相关阅读:
    Ubuntu Dash 添加 Sublime Text 图标
    Ubuntu14.04修改DNS服务
    VNC viewer访问Ubuntu 14.04
    GPT和parted命令详解
    linux 标准IO缓冲机制探究
    浅谈无缓存I/O操作和标准I/O文件操作区别
    jsp利用request获取当前的链接地址
    eclipse jsp formatter
    spring bean
    ubuntu google-chrome-stable
  • 原文地址:https://www.cnblogs.com/x-bing/p/5717083.html
Copyright © 2020-2023  润新知