예제 #1
0
 protected Bitmap downsampleWithSize(
     RecyclableBufferedInputStream bis,
     BitmapFactory.Options options,
     BitmapPool pool,
     int inWidth,
     int inHeight,
     int sampleSize) {
   if (sampleSize > 1) {
     options.inSampleSize = sampleSize;
   } else {
     setInBitmap(options, pool.get(inWidth, inHeight, getConfig(bis)));
   }
   return decodeStream(bis, options);
 }
예제 #2
0
  /**
   * Load the image for the given InputStream. If a recycled Bitmap whose dimensions exactly match
   * those of the image for the given InputStream is available, the operation is much less expensive
   * in terms of memory.
   *
   * <p>Note - this method will throw an exception of a Bitmap with dimensions not matching those of
   * the image for the given InputStream is provided.
   *
   * @param bis An InputStream to the data for the image
   * @param options The options to pass to {@link BitmapFactory#decodeStream(java.io.InputStream,
   *     android.graphics.Rect, android.graphics.BitmapFactory.Options)}
   * @param pool A pool of recycled bitmaps
   * @param outWidth The width the final image should be close to
   * @param outHeight The height the final image should be close to
   * @return A new bitmap containing the image from the given InputStream, or recycle if recycle is
   *     not null
   */
  public Bitmap downsample(
      RecyclableBufferedInputStream bis,
      BitmapFactory.Options options,
      BitmapPool pool,
      int outWidth,
      int outHeight) {
    bis.mark(MARK_POSITION);
    int orientation = 0;
    try {
      orientation = new ImageHeaderParser(bis).getOrientation();
    } catch (IOException e) {
      e.printStackTrace();
    }
    try {
      bis.reset();
    } catch (IOException e) {
      e.printStackTrace();
    }

    final int[] inDimens = getDimensions(bis, options);
    final int inWidth = inDimens[0];
    final int inHeight = inDimens[1];

    final int degreesToRotate = ImageResizer.getExifOrientationDegrees(orientation);
    final int sampleSize;
    if (degreesToRotate == 90 || degreesToRotate == 270) {
      // if we're rotating the image +-90 degrees, we need to downsample accordingly so the image
      // width is
      // decreased to near our target's height and the image height is decreased to near our target
      // width
      sampleSize = getSampleSize(inHeight, inWidth, outWidth, outHeight);
    } else {
      sampleSize = getSampleSize(inWidth, inHeight, outWidth, outHeight);
    }

    final Bitmap downsampled =
        downsampleWithSize(bis, options, pool, inWidth, inHeight, sampleSize);
    final Bitmap rotated = ImageResizer.rotateImageExif(downsampled, pool, orientation);

    if (downsampled != rotated) {
      pool.put(downsampled);
    }

    return rotated;
  }