Android Open Source - Photochat Image Resizer






From Project

Back to project page Photochat.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project Photochat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.navneet.photochat;
// ww w  .j  a v a2  s.co m
import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Pair;

public class ImageResizer {

  /*
   * Call this static method to resize an image to a specified width and height.
   * 
   * @param targetWidth  The width to resize to.
   * @param targetHeight The height to resize to.
   * @returns        The resized image as a Bitmap.
   */
  public static Bitmap resizeImage(byte[] imageData, int targetWidth, int targetHeight) {
    // Use BitmapFactory to decode the image
        BitmapFactory.Options options = new BitmapFactory.Options();
        
        // inSampleSize is used to sample smaller versions of the image
        options.inSampleSize = calculateInSampleSize(options, targetWidth, targetHeight);
        
        // Decode bitmap with inSampleSize and target dimensions set
        options.inJustDecodeBounds = false;  
        
        Bitmap reducedBitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(reducedBitmap, targetWidth, targetHeight, false);

        return resizedBitmap;        
  }

  public static Bitmap resizeImageMaintainAspectRatio(byte[] imageData, int shorterSideTarget) {
        Pair<Integer, Integer> dimensions = getDimensions(imageData);
    
        // Determine the aspect ratio (width/height) of the image
        int imageWidth = dimensions.first;
        int imageHeight = dimensions.second;
        float ratio = (float) dimensions.first / dimensions.second;
        
        int targetWidth;
        int targetHeight;

        // Determine portrait or landscape
        if (imageWidth > imageHeight) {
            // Landscape image. ratio (width/height) is > 1
          targetHeight = shorterSideTarget; 
            targetWidth = Math.round(shorterSideTarget * ratio);
        }
        else {
            // Portrait image. ratio (width/height) is < 1
          targetWidth = shorterSideTarget;
            targetHeight = Math.round(shorterSideTarget / ratio);
        }
        
    return resizeImage(imageData, targetWidth, targetHeight);
  }
  
  public static Pair<Integer, Integer> getDimensions(byte[] imageData) {
    // Use BitmapFactory to decode the image
        BitmapFactory.Options options = new BitmapFactory.Options();

        // Only decode the bounds of the image, not the whole image, to get the dimensions
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
        
        return new Pair<Integer, Integer>(options.outWidth, options.outHeight);
  }
  
  public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
      // Raw height and width of image
      final int height = options.outHeight;
      final int width = options.outWidth;
      int inSampleSize = 1;
  
      if (height > reqHeight || width > reqWidth) {
  
          final int halfHeight = height / 2;
          final int halfWidth = width / 2;
  
          // Calculate the largest inSampleSize value that is a power of 2 and keeps both
          // height and width larger than the requested height and width.
          while ((halfHeight / inSampleSize) > reqHeight
                  && (halfWidth / inSampleSize) > reqWidth) {
              inSampleSize *= 2;
          }
      }
  
      return inSampleSize;
  }
}




Java Source Code List

com.navneet.photochat.EditFriendsActivity.java
com.navneet.photochat.FileHelper.java
com.navneet.photochat.FriendsFragment.java
com.navneet.photochat.ImageResizer.java
com.navneet.photochat.InboxFragment.java
com.navneet.photochat.LoginActivity.java
com.navneet.photochat.MD5Util.java
com.navneet.photochat.MainActivity.java
com.navneet.photochat.ParseConstants.java
com.navneet.photochat.PhotoChatApplication.java
com.navneet.photochat.RecipientsActivity.java
com.navneet.photochat.SectionsPagerAdapter.java
com.navneet.photochat.SignUpActivity.java
com.navneet.photochat.UserAdpater.java
com.navneet.photochat.ViewImagesActivity.java
com.navneet.photochat.messageAdapter.java