Skip to content

Commit db6d900

Browse files
author
radeva
authored
Merge pull request #52 from EddyVerbruggen/issue-24-image-orientation-android
Fix image orientation for certain (Samsung) devices.
2 parents b7cd6f9 + 6280eb3 commit db6d900

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/nativescript-camera.android.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,17 @@ export var takePicture = function (options?): Promise<any> {
8787
trace.write(`An error occurred while scanning file ${picturePath}: ${ex.message}!`, trace.categories.Debug);
8888
}
8989
}
90-
};
90+
}
91+
92+
let exif = new android.media.ExifInterface(picturePath);
93+
let orientation = exif.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL);
94+
if (orientation === android.media.ExifInterface.ORIENTATION_ROTATE_90) {
95+
rotateBitmap(picturePath, 90);
96+
} else if (orientation === android.media.ExifInterface.ORIENTATION_ROTATE_180) {
97+
rotateBitmap(picturePath, 180);
98+
} else if (orientation === android.media.ExifInterface.ORIENTATION_ROTATE_270) {
99+
rotateBitmap(picturePath, 270);
100+
}
91101

92102
let asset = new imageAssetModule.ImageAsset(picturePath);
93103
asset.options = {
@@ -133,4 +143,22 @@ var createDateTimeStamp = function () {
133143
date.getMinutes().toString() +
134144
date.getSeconds().toString();
135145
return result;
146+
}
147+
148+
var rotateBitmap = function (picturePath, angle) {
149+
try {
150+
let matrix = new android.graphics.Matrix();
151+
matrix.postRotate(angle);
152+
let bmOptions = new android.graphics.BitmapFactory.Options();
153+
let oldBitmap = android.graphics.BitmapFactory.decodeFile(picturePath, bmOptions);
154+
let finalBitmap = android.graphics.Bitmap.createBitmap(oldBitmap, 0, 0, oldBitmap.getWidth(), oldBitmap.getHeight(), matrix, true);
155+
let out = new java.io.FileOutputStream(picturePath);
156+
finalBitmap.compress(android.graphics.Bitmap.CompressFormat.JPEG, 100, out);
157+
out.flush();
158+
out.close();
159+
} catch (ex) {
160+
if (trace.isEnabled()) {
161+
trace.write(`An error occurred while rotating file ${picturePath} (using the original one): ${ex.message}!`, trace.categories.Debug);
162+
}
163+
}
136164
}

0 commit comments

Comments
 (0)