Skip to content

Commit

Permalink
fix(android): blob imageAsX() methods to read exif for non-files (#11679
Browse files Browse the repository at this point in the history
)

- Was only reading JPEG EXIF orientation for file types only. Now reads for all types via InputStream.

Fixes TIMOB-27872
  • Loading branch information
jquick-axway committed May 21, 2020
1 parent 52d4d81 commit 7ce3ae1
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions android/titanium/src/java/org/appcelerator/titanium/TiBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,17 @@ private Bitmap getImage(BitmapFactory.Options opts)
return image;
}

private int getImageOrientation()
{
int rotation = 0;
try (InputStream stream = getInputStream()) {
rotation = TiImageHelper.getOrientation(stream);
} catch (Exception ex) {
Log.e(TAG, "Failed to fetch image EXIF orientation from blob.", ex);
}
return rotation;
}

@Kroll.method
public TiBlob imageAsCropped(Object params)
{
Expand All @@ -626,10 +637,7 @@ public TiBlob imageAsCropped(Object params)
return null;
}

int rotation = 0;
if (type == TYPE_FILE) {
rotation = TiImageHelper.getOrientation(getNativePath());
}
int rotation = getImageOrientation();

KrollDict options = new KrollDict((HashMap) params);
int widthCropped = options.optInt(TiC.PROPERTY_WIDTH, width);
Expand Down Expand Up @@ -718,13 +726,10 @@ public TiBlob imageAsResized(Number width, Number height)
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
}

String nativePath = getNativePath();
int rotation = 0;
if (type == TYPE_FILE) {
rotation = TiImageHelper.getOrientation(nativePath);
}
int rotation = getImageOrientation();

String key = null;
String nativePath = getNativePath();
if (nativePath != null) {
key = nativePath + "_imageAsResized_" + rotation + "_" + dstWidth + "_" + dstHeight;
Bitmap bitmap = mMemoryCache.get(key);
Expand All @@ -747,6 +752,11 @@ public TiBlob imageAsResized(Number width, Number height)
imgWidth = img.getWidth();
imgHeight = img.getHeight();
if (rotation != 0) {
if ((rotation == 90) || (rotation == 270)) {
int value = dstWidth;
dstWidth = dstHeight;
dstHeight = value;
}
float scaleWidth = (float) dstWidth / imgWidth;
float scaleHeight = (float) dstHeight / imgHeight;
Matrix matrix = new Matrix();
Expand Down Expand Up @@ -848,10 +858,7 @@ public TiBlob imageAsThumbnail(Number size, @Kroll.argument(optional = true) Num
return null;
}

int rotation = 0;
if (type == TYPE_FILE) {
rotation = TiImageHelper.getOrientation(getNativePath());
}
int rotation = getImageOrientation();

int thumbnailSize = size.intValue();

Expand Down Expand Up @@ -938,10 +945,8 @@ public TiBlob imageWithAlpha()
if (img == null) {
return null;
}
int rotation = 0;
if (type == TYPE_FILE) {
rotation = TiImageHelper.getOrientation(getNativePath());
}

int rotation = getImageOrientation();

String nativePath = getNativePath();
String key = null;
Expand Down Expand Up @@ -995,10 +1000,7 @@ public TiBlob imageWithRoundedCorner(Number cornerRadius, @Kroll.argument(option
return null;
}

int rotation = 0;
if (type == TYPE_FILE) {
rotation = TiImageHelper.getOrientation(getNativePath());
}
int rotation = getImageOrientation();

float radius = cornerRadius.floatValue();
float border = 1f;
Expand Down Expand Up @@ -1059,10 +1061,7 @@ public TiBlob imageWithTransparentBorder(Number size)
return null;
}

int rotation = 0;
if (type == TYPE_FILE) {
rotation = TiImageHelper.getOrientation(getNativePath());
}
int rotation = getImageOrientation();

int borderSize = size.intValue();

Expand Down

0 comments on commit 7ce3ae1

Please sign in to comment.