public static InputStream cropImage(InputStream inputStream,int imageWidth,int imageHeight,int outWidth,int outHeight) throws IllegalArgumentException {
  if (inputStream == null) {
    throw new IllegalArgumentException("inputStream cannot be null");
  }
  if (imageWidth <= 0 || imageHeight <= 0) {
    throw new IllegalArgumentException(String.format("imageWidth and imageHeight must be > 0: imageWidth=%d" + " imageHeight=%d",imageWidth,imageHeight));
  }
  if (outWidth <= 0 || outHeight <= 0) {
    throw new IllegalArgumentException(String.format("outWidth and outHeight must be > 0: outWidth=%d" + " outHeight=%d",imageWidth,outHeight));
  }
  int scaleDownSampleSize=Math.min(imageWidth / outWidth,imageHeight / outHeight);
  if (scaleDownSampleSize > 0) {
    imageWidth/=scaleDownSampleSize;
    imageHeight/=scaleDownSampleSize;
  }
 else {
    float ratio=(float)outWidth / outHeight;
    if (imageWidth < imageHeight * ratio) {
      outWidth=imageWidth;
      outHeight=(int)(outWidth / ratio);
    }
 else {
      outHeight=imageHeight;
      outWidth=(int)(outHeight * ratio);
    }
  }
  int left=(imageWidth - outWidth) / 2;
  int top=(imageHeight - outHeight) / 2;
  InputStream compressed=null;
  try {
    BitmapFactory.Options options=new BitmapFactory.Options();
    if (scaleDownSampleSize > 1) {
      options.inSampleSize=scaleDownSampleSize;
    }
    Bitmap bitmap=BitmapFactory.decodeStream(inputStream,null,options);
    if (bitmap == null) {
      return null;
    }
    Bitmap cropped=Bitmap.createBitmap(bitmap,left,top,outWidth,outHeight);
    ByteArrayOutputStream tmpOut=new ByteArrayOutputStream(2048);
    if (cropped.compress(Bitmap.CompressFormat.PNG,DEFAULT_IMG_QUALITY,tmpOut)) {
      byte[] outByteArray=tmpOut.toByteArray();
      compressed=new ByteArrayInputStream(outByteArray);
    }
  }
 catch (  Exception e) {
    Log.e(TAG,"Exception " + e);
  }
  return compressed;
}

public static InputStream cropImage ( InputStream inputStream , int imageWidth , int imageHeight , int outWidth , int outHeight ) throws IllegalArgumentException { if ( inputStream == null ) { throw new IllegalArgumentException ( "inputStream cannot be null" ) ; } if ( imageWidth <= 0 || imageHeight <= 0 ) { throw new IllegalArgumentException ( String . format ( "imageWidth and imageHeight must be > 0: imageWidth=%d" + " imageHeight=%d" , imageWidth , imageHeight ) ) ; } if ( outWidth <= 0 || outHeight <= 0 ) { throw new IllegalArgumentException ( String . format ( "outWidth and outHeight must be > 0: outWidth=%d" + " outHeight=%d" , imageWidth , outHeight ) ) ; } int scaleDownSampleSize = Math . min ( imageWidth / outWidth , imageHeight / outHeight ) ; if ( scaleDownSampleSize > 0 ) { imageWidth /= scaleDownSampleSize ; imageHeight /= scaleDownSampleSize ; } else { float ratio = ( float ) outWidth / outHeight ; if ( imageWidth < imageHeight * ratio ) { outWidth = imageWidth ; outHeight = ( int ) ( outWidth / ratio ) ; } else { outHeight = imageHeight ; outWidth = ( int ) ( outHeight * ratio ) ; } } int left = ( imageWidth - outWidth ) / 2 ; int top = ( imageHeight - outHeight ) / 2 ; InputStream compressed = null ; try { BitmapFactory . Options options = new BitmapFactory . Options ( ) ; if ( scaleDownSampleSize > 1 ) { options . inSampleSize = scaleDownSampleSize ; } Bitmap bitmap = BitmapFactory . decodeStream ( inputStream , null , options ) ; if ( bitmap == null ) { return null ; } Bitmap cropped = Bitmap . createBitmap ( bitmap , left , top , outWidth , outHeight ) ; ByteArrayOutputStream tmpOut = new ByteArrayOutputStream ( 2048 ) ; if ( cropped . compress ( Bitmap . CompressFormat . PNG , DEFAULT_IMG_QUALITY , tmpOut ) ) { byte [ ] outByteArray = tmpOut . toByteArray ( ) ; compressed = new ByteArrayInputStream ( outByteArray ) ; } } catch ( Exception e ) { Log . e ( TAG , "Exception " + e ) ; } return compressed ; }
crops the input image and returns a new inputstream of the cropped area
