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


  • 相关阅读:
    发布两个android程序遇到证书不一致的问题!
    第一次下载程序到单片机
    人人网是明文传输,所以只要抓包就能知道用户名和密码
    [转]java调用cmd命令
    qq发送文件是在两个计算机之间建立tcp连接
    CardLayout实现分页效果
    控件 ListView1 的类 MSComctlLib.ListView 不是一个已加载的控件类
    VB中declare function 出现49错误
    oracle instant client
    WARN Please initialize the log4j system properly的解决办法【转载】
  • 原文地址:https://www.cnblogs.com/x-bing/p/5717083.html
Copyright © 2011-2022 走看看