Esempio n. 1
0
  public static Bitmap decodeThumbnail(
      JobContext jc, FileDescriptor fd, Options options, int targetSize, int type) {
    if (options == null) options = new Options();
    jc.setCancelListener(new DecodeCanceller(options));

    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(fd, null, options);
    if (jc.isCancelled()) return null;

    int w = options.outWidth;
    int h = options.outHeight;

    if (type == MediaItem.TYPE_MICROTHUMBNAIL) {
      // We center-crop the original image as it's micro thumbnail. In this case,
      // we want to make sure the shorter side >= "targetSize".
      float scale = (float) targetSize / Math.min(w, h);
      options.inSampleSize = BitmapUtils.computeSampleSizeLarger(scale);

      // For an extremely wide image, e.g. 300x30000, we may got OOM when decoding
      // it for TYPE_MICROTHUMBNAIL. So we add a max number of pixels limit here.
      final int MAX_PIXEL_COUNT = 640000; // 400 x 1600
      if ((w / options.inSampleSize) * (h / options.inSampleSize) > MAX_PIXEL_COUNT) {
        options.inSampleSize =
            BitmapUtils.computeSampleSize(FloatMath.sqrt((float) MAX_PIXEL_COUNT / (w * h)));
      }
    } else {
      // For screen nail, we only want to keep the longer side >= targetSize.
      float scale = (float) targetSize / Math.max(w, h);
      options.inSampleSize = BitmapUtils.computeSampleSizeLarger(scale);
    }

    options.inJustDecodeBounds = false;
    setOptionsMutable(options);

    Bitmap result = BitmapFactory.decodeFileDescriptor(fd, null, options);
    if (result == null) return null;

    // We need to resize down if the decoder does not support inSampleSize
    // (For example, GIF images)
    float scale =
        (float) targetSize
            / (type == MediaItem.TYPE_MICROTHUMBNAIL
                ? Math.min(result.getWidth(), result.getHeight())
                : Math.max(result.getWidth(), result.getHeight()));

    if (scale <= 0.5) result = BitmapUtils.resizeBitmapByScale(result, scale, true);
    return ensureGLCompatibleBitmap(result);
  }