Exemplo n.º 1
0
  private static boolean rotateImage(
      String imageFile, String outputFile, int quality, Bitmap.CompressFormat compressFormat) {
    int degree = ImageUtils.getExifOrientation(imageFile);
    if (degree != 0) {
      Log.i(TAG, "rotate image from:" + degree);
      Bitmap origin = BitmapFactory.decodeFile(imageFile);
      Bitmap rotate = ImageUtils.rotateImage(degree, origin);
      if (rotate != null) {
        ImageUtils.saveBitmap(rotate, outputFile, compressFormat, quality);
        rotate.recycle();
        origin.recycle();
        return true;
      } else {
        Log.i(TAG, "rotate image failed:" + imageFile);
        Log.i(TAG, "use origin image");
      }
      origin.recycle();
    }

    return false;
  }
Exemplo n.º 2
0
  /**
   * 压缩图片文件
   *
   * @param srcFile 源文件
   * @param dstFile 目标文件
   * @param maxWidth 最大宽度
   * @param maxHeight 最大高度
   * @return true进行了压缩,false无需压缩
   */
  public static boolean compressImageFile(
      String srcFile,
      String dstFile,
      int maxWidth,
      int maxHeight,
      int quality,
      Bitmap.CompressFormat compressFormat) {

    Log.i(TAG, "compress file:" + srcFile);
    Log.i(TAG, "file length:" + (int) (new File(srcFile).length() / 1024d) + "kb");
    Log.i(TAG, "output size:(" + maxWidth + ", " + maxHeight + ")");

    BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
    decodeOptions.inJustDecodeBounds = true;

    BitmapFactory.decodeFile(srcFile, decodeOptions);
    int actualWidth = decodeOptions.outWidth;
    int actualHeight = decodeOptions.outHeight;

    Log.i(TAG, "input size:(" + actualWidth + ", " + actualHeight + ")");

    if (actualWidth < maxWidth && actualHeight < maxHeight) {
      Log.i(TAG, "stop compress: input size < output size");
      return rotateImage(srcFile, dstFile, quality, compressFormat);
    }

    int sampleSize;
    int w;
    int h;
    if (actualWidth * maxHeight > maxWidth * actualHeight) {
      w = maxWidth;
      h = (int) (w * actualHeight / (double) actualWidth);
      sampleSize = (int) (actualWidth / (double) maxWidth);
    } else {
      h = maxHeight;
      w = (int) (h * actualWidth / (double) actualHeight);
      sampleSize = (int) (actualHeight / (double) maxHeight);
    }

    Log.i(TAG, "in simple size:" + sampleSize);

    decodeOptions.inJustDecodeBounds = false;
    decodeOptions.inSampleSize = sampleSize;
    decodeOptions.inPreferredConfig = Bitmap.Config.RGB_565;
    decodeOptions.inPurgeable = true;
    decodeOptions.inInputShareable = true;
    Bitmap bitmap = null;
    try {
      bitmap = BitmapFactory.decodeFile(srcFile, decodeOptions);
    } catch (OutOfMemoryError error) {
      error.printStackTrace();
      Log.i(
          TAG, "OutOfMemoryError:" + srcFile + ", size(" + actualWidth + ", " + actualHeight + ")");
    }

    if (bitmap == null) {
      Log.i(TAG, "stop compress:decode file error");
      return false;
    }

    Log.i(TAG, "origin bitmap size:(" + bitmap.getWidth() + ", " + bitmap.getHeight() + ")");

    if (bitmap.getWidth() > maxWidth || bitmap.getHeight() > maxHeight) {
      Bitmap tempBitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
      bitmap.recycle();
      bitmap = tempBitmap;
      Log.i(TAG, "scale down:(" + bitmap.getWidth() + ", " + bitmap.getHeight() + ")");
    }

    int degree = ImageUtils.getExifOrientation(srcFile);
    if (degree != 0) {
      Log.i(TAG, "rotate image from:" + degree);
      Bitmap rotate = ImageUtils.rotateImage(degree, bitmap);
      bitmap.recycle();
      bitmap = rotate;
    }

    ImageUtils.saveBitmap(bitmap, dstFile, compressFormat, quality);

    Log.i(TAG, "output file length:" + (int) (new File(dstFile).length() / 1024d) + "kb");
    Log.i(TAG, "------------------ compress file complete ---------------");
    return true;
  }