示例#1
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;
  }
示例#2
0
  public static byte[] reduceImageForUpload(byte[] imageData) {
    Bitmap bitmap = ImageResizer.resizeImageMaintainAspectRatio(imageData, SHORT_SIDE_TARGET);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
    byte[] reducedData = outputStream.toByteArray();
    try {
      outputStream.close();
    } catch (IOException e) {
      // Intentionally blank
    }

    return reducedData;
  }
示例#3
0
 @Override
 protected void flushCacheInternal() {
   super.flushCacheInternal();
   synchronized (mHttpDiskCacheLock) {
     if (mHttpDiskCache != null) {
       try {
         mHttpDiskCache.flush();
         if (CacheConfig.DEBUG) {
           Log.d(TAG, "HTTP cache flushed");
         }
       } catch (IOException e) {
         Log.e(TAG, "flush - " + e);
       }
     }
   }
 }
  /**
   * Get from disk cache.
   *
   * @param data Unique identifier for which item to get
   * @return The bitmap if found in cache, null otherwise
   */
  public Bitmap getBitmapFromDiskCache(String data) {
    // BEGIN_INCLUDE(get_bitmap_from_disk_cache)
    final String key = hashKeyForDisk(data);
    Bitmap bitmap = null;

    synchronized (mDiskCacheLock) {
      while (mDiskCacheStarting) {
        try {
          mDiskCacheLock.wait();
        } catch (InterruptedException e) {
        }
      }
      if (mDiskLruCache != null) {
        InputStream inputStream = null;
        try {
          final DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key);
          if (snapshot != null) {
            if (BuildConfig.DEBUG) {
              Log.d(TAG, "Disk cache hit");
            }
            inputStream = snapshot.getInputStream(DISK_CACHE_INDEX);
            if (inputStream != null) {
              FileDescriptor fd = ((FileInputStream) inputStream).getFD();

              // Decode bitmap, but we don't want to sample so give
              // MAX_VALUE as the target dimensions
              bitmap =
                  ImageResizer.decodeSampledBitmapFromDescriptor(
                      fd, Integer.MAX_VALUE, Integer.MAX_VALUE, this);
            }
          }
        } catch (final IOException e) {
          Log.e(TAG, "getBitmapFromDiskCache - " + e);
        } finally {
          try {
            if (inputStream != null) {
              inputStream.close();
            }
          } catch (IOException e) {
          }
        }
      }
      return bitmap;
    }
    // END_INCLUDE(get_bitmap_from_disk_cache)
  }
示例#5
0
 public ImageDescription getImageDescription(Integer idImage, String suffix, String extension)
     throws IOException {
   File file = getPathManager().getPath(new FileDescriptor(idImage, suffix, extension), false);
   if (file.exists()) {
     Dimension dim = imageResizer.getImageDimesion(file);
     if (dim == null) {
       return null;
     } else {
       ImageDescription out = new ImageDescription();
       out.setWidth((int) dim.getWidth());
       out.setHeight((int) dim.getHeight());
       out.setExtension(extension);
       return out;
     }
   } else {
     return null;
   }
 }
示例#6
0
 @Override
 protected void closeCacheInternal() {
   super.closeCacheInternal();
   synchronized (mHttpDiskCacheLock) {
     if (mHttpDiskCache != null) {
       try {
         if (!mHttpDiskCache.isClosed()) {
           mHttpDiskCache.close();
           mHttpDiskCache = null;
           if (CacheConfig.DEBUG) {
             Log.d(TAG, "HTTP cache closed");
           }
         }
       } catch (IOException e) {
         Log.e(TAG, "closeCacheInternal - " + e);
       }
     }
   }
 }
示例#7
0
 @Override
 protected void clearCacheInternal() {
   super.clearCacheInternal();
   synchronized (mHttpDiskCacheLock) {
     if (mHttpDiskCache != null && !mHttpDiskCache.isClosed()) {
       try {
         mHttpDiskCache.delete();
         if (CacheConfig.DEBUG) {
           Log.d(TAG, "HTTP cache cleared");
         }
       } catch (IOException e) {
         Log.e(TAG, "clearCacheInternal - " + e);
       }
       mHttpDiskCache = null;
       mHttpDiskCacheStarting = true;
       initHttpDiskCache();
     }
   }
 }
  public static void resizeImages(File folder, int standardHeight) throws IOException {
    File[] listOfFiles = folder.listFiles();

    // resize the images to a standard size
    System.out.println("Total No of Files:" + listOfFiles.length);
    BufferedImage img = null;
    BufferedImage tempJPG = null;
    File newFileJPG = null;

    for (int i = 0; i < listOfFiles.length; i++) {
      if (listOfFiles[i].isFile()) {
        System.out.println("Resizing File " + folder.getPath() + "\\" + listOfFiles[i].getName());
        img = ImageIO.read(new File(folder.getPath() + "\\" + listOfFiles[i].getName()));
        int newWidth = img.getWidth() * standardHeight / img.getHeight();
        tempJPG = ImageResizer.resizeImage(img, newWidth, standardHeight);
        newFileJPG = new File(folder.getPath() + "\\" + "New_" + listOfFiles[i].getName());
        ImageIO.write(tempJPG, "jpg", newFileJPG);
        System.out.println("    Resized to " + newFileJPG);
      }
    }
  }
示例#9
0
 @Override
 protected void initDiskCacheInternal() {
   super.initDiskCacheInternal();
   initHttpDiskCache();
 }