@@ -504,16 +504,69 @@ static Bitmap transformResult(Request data, Bitmap result, int exifOrientation)
504
504
505
505
Matrix matrix = new Matrix ();
506
506
507
- if (data .needsMatrixTransform ()) {
507
+ if (data .needsMatrixTransform () || exifOrientation != 0 ) {
508
508
int targetWidth = data .targetWidth ;
509
509
int targetHeight = data .targetHeight ;
510
510
511
511
float targetRotation = data .rotationDegrees ;
512
512
if (targetRotation != 0 ) {
513
+ double cosR = Math .cos (Math .toRadians (targetRotation ));
514
+ double sinR = Math .sin (Math .toRadians (targetRotation ));
513
515
if (data .hasRotationPivot ) {
514
516
matrix .setRotate (targetRotation , data .rotationPivotX , data .rotationPivotY );
517
+ // Recalculate dimensions after rotation around pivot point
518
+ double x1T = data .rotationPivotX * (1.0 - cosR ) + (data .rotationPivotY * sinR );
519
+ double y1T = data .rotationPivotY * (1.0 - cosR ) - (data .rotationPivotX * sinR );
520
+ double x2T = x1T + (data .targetWidth * cosR );
521
+ double y2T = y1T + (data .targetWidth * sinR );
522
+ double x3T = x1T + (data .targetWidth * cosR ) - (data .targetHeight * sinR );
523
+ double y3T = y1T + (data .targetWidth * sinR ) + (data .targetHeight * cosR );
524
+ double x4T = x1T - (data .targetHeight * sinR );
525
+ double y4T = y1T + (data .targetHeight * cosR );
526
+
527
+ double maxX = Math .max (x4T , Math .max (x3T , Math .max (x1T , x2T )));
528
+ double minX = Math .min (x4T , Math .min (x3T , Math .min (x1T , x2T )));
529
+ double maxY = Math .max (y4T , Math .max (y3T , Math .max (y1T , y2T )));
530
+ double minY = Math .min (y4T , Math .min (y3T , Math .min (y1T , y2T )));
531
+ targetWidth = (int ) Math .floor (maxX - minX );
532
+ targetHeight = (int ) Math .floor (maxY - minY );
515
533
} else {
516
534
matrix .setRotate (targetRotation );
535
+ // Recalculate dimensions after rotation (around origin)
536
+ double x1T = 0.0 ;
537
+ double y1T = 0.0 ;
538
+ double x2T = (data .targetWidth * cosR );
539
+ double y2T = (data .targetWidth * sinR );
540
+ double x3T = (data .targetWidth * cosR ) - (data .targetHeight * sinR );
541
+ double y3T = (data .targetWidth * sinR ) + (data .targetHeight * cosR );
542
+ double x4T = -(data .targetHeight * sinR );
543
+ double y4T = (data .targetHeight * cosR );
544
+
545
+ double maxX = Math .max (x4T , Math .max (x3T , Math .max (x1T , x2T )));
546
+ double minX = Math .min (x4T , Math .min (x3T , Math .min (x1T , x2T )));
547
+ double maxY = Math .max (y4T , Math .max (y3T , Math .max (y1T , y2T )));
548
+ double minY = Math .min (y4T , Math .min (y3T , Math .min (y1T , y2T )));
549
+ targetWidth = (int ) Math .floor (maxX - minX );
550
+ targetHeight = (int ) Math .floor (maxY - minY );
551
+ }
552
+ }
553
+
554
+ // EXIf interpretation should be done before cropping in case the dimensions need to
555
+ // be recalculated
556
+ if (exifOrientation != 0 ) {
557
+ int exifRotation = getExifRotation (exifOrientation );
558
+ int exifTranslation = getExifTranslation (exifOrientation );
559
+ if (exifRotation != 0 ) {
560
+ matrix .preRotate (exifRotation );
561
+ if (exifRotation == 90 || exifRotation == 270 ) {
562
+ // Recalculate dimensions after exif rotation
563
+ int tmpHeight = targetHeight ;
564
+ targetHeight = targetWidth ;
565
+ targetWidth = tmpHeight ;
566
+ }
567
+ }
568
+ if (exifTranslation != 1 ) {
569
+ matrix .postScale (exifTranslation , 1 );
517
570
}
518
571
}
519
572
@@ -569,14 +622,6 @@ static Bitmap transformResult(Request data, Bitmap result, int exifOrientation)
569
622
}
570
623
}
571
624
572
- if (exifOrientation != 0 ) {
573
- matrix .preRotate (getExifRotation (exifOrientation ));
574
- int exifTranslation = getExifTranslation (exifOrientation );
575
- if (exifTranslation != 1 ) {
576
- matrix .postScale (exifTranslation , 1 );
577
- }
578
- }
579
-
580
625
Bitmap newResult =
581
626
Bitmap .createBitmap (result , drawX , drawY , drawWidth , drawHeight , matrix , true );
582
627
if (newResult != result ) {
0 commit comments