@Test public void testInitializeMatrixSetsScaleIfFlipHorizontal() { Matrix matrix = mock(Matrix.class); TransformationUtils .initializeMatrixForRotation(ExifInterface.ORIENTATION_FLIP_HORIZONTAL, matrix); verify(matrix).setScale(-1, 1); }
/** * Rotate and/or flip the image to match the given exif orientation. * * @param pool A pool that may or may not contain an image of the necessary * dimensions. * @param inBitmap The bitmap to rotate/flip. * @param exifOrientation the exif orientation [1-8]. * @return The rotated and/or flipped image or toOrient if no rotation or flip was necessary. */ public static Bitmap rotateImageExif(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int exifOrientation) { if (!isExifOrientationRequired(exifOrientation)) { return inBitmap; } final Matrix matrix = new Matrix(); initializeMatrixForRotation(exifOrientation, matrix); // From Bitmap.createBitmap. final RectF newRect = new RectF(0, 0, inBitmap.getWidth(), inBitmap.getHeight()); matrix.mapRect(newRect); final int newWidth = Math.round(newRect.width()); final int newHeight = Math.round(newRect.height()); Bitmap.Config config = getNonNullConfig(inBitmap); Bitmap result = pool.get(newWidth, newHeight, config); matrix.postTranslate(-newRect.left, -newRect.top); result.setHasAlpha(inBitmap.hasAlpha()); applyMatrix(inBitmap, result, matrix); return result; }
@Test public void testInitializeMatrixSetsRotateOnRotation() { Matrix matrix = mock(Matrix.class); TransformationUtils.initializeMatrixForRotation(ExifInterface.ORIENTATION_ROTATE_90, matrix); verify(matrix).setRotate(90); TransformationUtils.initializeMatrixForRotation(ExifInterface.ORIENTATION_ROTATE_180, matrix); verify(matrix).setRotate(180); TransformationUtils.initializeMatrixForRotation(ExifInterface.ORIENTATION_ROTATE_270, matrix); verify(matrix).setRotate(-90); }
@Test public void testInitializeMatrixSetsScaleAndRotateIfTranspose() { Matrix matrix = mock(Matrix.class); TransformationUtils.initializeMatrixForRotation(ExifInterface.ORIENTATION_TRANSPOSE, matrix); verify(matrix).setRotate(90); verify(matrix).postScale(-1, 1); }
@Test public void testInitializeMatrixSetsScaleAndRotateIfTransverse() { Matrix matrix = mock(Matrix.class); TransformationUtils.initializeMatrixForRotation(ExifInterface.ORIENTATION_TRANSVERSE, matrix); verify(matrix).setRotate(-90); verify(matrix).postScale(-1, 1); }
@Test public void testInitializeMatrixSetsScaleAndRotateIfFlipVertical() { Matrix matrix = mock(Matrix.class); TransformationUtils .initializeMatrixForRotation(ExifInterface.ORIENTATION_FLIP_VERTICAL, matrix); verify(matrix).setRotate(180); verify(matrix).postScale(-1, 1); }
/** * Rotate and/or flip the image to match the given exif orientation. * * @param toOrient The bitmap to rotate/flip. * @param pool A pool that may or may not contain an image of the necessary dimensions. * @param exifOrientation the exif orientation [1-8]. * @return The rotated and/or flipped image or toOrient if no rotation or flip was necessary. */ public static Bitmap rotateImageExif(Bitmap toOrient, BitmapPool pool, int exifOrientation) { final Matrix matrix = new Matrix(); initializeMatrixForRotation(exifOrientation, matrix); if (matrix.isIdentity()) { return toOrient; } // From Bitmap.createBitmap. final RectF newRect = new RectF(0, 0, toOrient.getWidth(), toOrient.getHeight()); matrix.mapRect(newRect); final int newWidth = Math.round(newRect.width()); final int newHeight = Math.round(newRect.height()); Bitmap.Config config = getSafeConfig(toOrient); Bitmap result = pool.get(newWidth, newHeight, config); if (result == null) { result = Bitmap.createBitmap(newWidth, newHeight, config); } matrix.postTranslate(-newRect.left, -newRect.top); final Canvas canvas = new Canvas(result); final Paint paint = new Paint(PAINT_FLAGS); canvas.drawBitmap(toOrient, matrix, paint); return result; }
/** * Rotate and/or flip the image to match the given exif orientation. * * @param pool A pool that may or may not contain an image of the necessary * dimensions. * @param inBitmap The bitmap to rotate/flip. * @param exifOrientation the exif orientation [1-8]. * @return The rotated and/or flipped image or toOrient if no rotation or flip was necessary. */ public static Bitmap rotateImageExif(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int exifOrientation) { if (!isExifOrientationRequired(exifOrientation)) { return inBitmap; } final Matrix matrix = new Matrix(); initializeMatrixForRotation(exifOrientation, matrix); // From Bitmap.createBitmap. final RectF newRect = new RectF(0, 0, inBitmap.getWidth(), inBitmap.getHeight()); matrix.mapRect(newRect); final int newWidth = Math.round(newRect.width()); final int newHeight = Math.round(newRect.height()); Bitmap.Config config = getSafeConfig(inBitmap); Bitmap result = pool.get(newWidth, newHeight, config); matrix.postTranslate(-newRect.left, -newRect.top); applyMatrix(inBitmap, result, matrix); return result; }