0

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 );
}
Community
  • 1
  • 1
SoH
  • 2,180
  • 2
  • 24
  • 53
  • Yep. So post some code with what you've tried! Best wishes. – Tom Oct 21 '13 at 12:30
  • Right. So in `rotateMtrix.postRotate( 5 );` you're rotating by 5 degrees here (so not noticeable) *and* you're rotating in degrees, whereas the math above (do you really need that much trig?) will be in radians (docs: http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html for reference). Why are you creating a new Matrix(). Should just need to `postScale` and `postRotate` one matrix. Furthermore, if you're `postScaling` a matrix why do you need to use `ScalingUtilities`. May be good answers to these questions but it'd be helpful to know in order to give an answer. Thanks. Best. – Tom Oct 21 '13 at 13:43
  • Ok sorry for not explaining myself that much. The link that I provided in my question showed what I wanted to achieve. I have an image which has an aspect ratio of 1:1. I want to add a straighten functionlity on top of it, while maintaining the aspect ration as well as the width and height. To achieve this effect, Instagram zooms in the image while rotating in (rotating is done minimillay from -5 deg to +5degrees). – SoH Oct 21 '13 at 15:19
  • From the code I have provided above what I get is that the rotation and scaling is done but the image is no longer a square. But a parallogram. Which is understandable since I have rotated the bitmap. But I want to achieve rotation and keep the same square image. The only way to do that is if I provide a scale. I cannot somehow get the effect that I am trying to achieve. I hope I am able to clarify my question. – SoH Oct 21 '13 at 15:21
  • Right. Got it. The zooming is absolutely right, that's the only way to do it. By parallelogram you mean that the square image bounds are rotated, right? Why are you creating and rotating bitmaps, rather than using a custom image view and canvas? Do you need to save these bitmaps. – Tom Oct 21 '13 at 15:33
  • @SoH Did you resolve the issue? The rotated bitmap is not actually turned into a parallelogram, but it looks like so because the rotated bitmap does not fit in the original bounds of ImageView. – Nj Subedi Nov 29 '14 at 13:08
  • @SoH Did you resolve the issue? The rotated bitmap is not actually turned into a parallelogram, but it looks like so because the rotated bitmap does not fit in the original bounds of ImageView. I am building a similar app and stuck at auto-zooming the image upon rotation. – Nj Subedi Nov 29 '14 at 13:09

0 Answers0