Using this link: Image Straightening in Android
I have successfully zoomed the image enough according to degree of tilt. But now I want to rotate the image as well keeping the aspect ratio in a 1:1 ratio. Is it possible? The updated Instagram app provides this zoom + rotate functionality. Does anyone how this can be done?
public void setImageStraighten ( Bitmap bitmap, int bmpHeight, int bmpWidth, double theta ) {
float a = ( float ) Math.atan( bmpHeight / bmpWidth );
// the length from the center to the corner of the green
double len1 = ( ( bmpWidth / 2 ) / Math.cos( a - Math.abs( theta ) ) );
// the length from the center to the corner of the black (^ = power)
double len2 = Math.sqrt( Math.pow( ( bmpWidth / 2 ) , 2 ) + Math.pow(( bmpHeight / 2 ) , 2 ));
// compute the scaling factor
float curScale = ( float ) ( len2 / len1 );
Matrix matrix = new Matrix();
matrix.postScale( curScale, curScale );
Matrix rotateMtrix = new Matrix( );
rotateMtrix.postRotate( 5 );
Bitmap resizedRotatedBitmap = Bitmap.createBitmap( bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), rotateMtrix, false );
Bitmap bmp = ScalingUtilities.createScaledBitmap( resizedRotatedBitmap , 720,720, ScalingUtilities.ScalingLogic.CROP);
int startHeight = (int)((curScale*bmpHeight)- bmpHeight);
Bitmap resizedBitmap = cropBitmap1( bmp , bmpHeight);
imgSwap.setImageBitmap( resizedBitmap );
imgSwap.setScaleType( ImageView.ScaleType.CENTER_CROP );
}