Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): optimize imageAsResized #10380

Merged
merged 7 commits into from
Apr 26, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 12 additions & 15 deletions android/titanium/src/java/org/appcelerator/titanium/TiBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -720,23 +720,20 @@ public TiBlob imageAsResized(Number width, Number height)

opts = new BitmapFactory.Options();
opts.inSampleSize = sampleSize;
opts.inDensity = imgWidth;
opts.inTargetDensity = dstWidth * sampleSize;
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
}

Bitmap img = getImage(opts);
if (img == null) {
return null;
}

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

String nativePath = getNativePath();
String key = null;
if (nativePath != null) {
key = getNativePath() + "_imageAsResized_" + rotation + "_" + dstWidth + "_" + dstHeight;
key = nativePath + "_imageAsResized_" + rotation + "_" + dstWidth + "_" + dstHeight;
Bitmap bitmap = mMemoryCache.get(key);
if (bitmap != null) {
if (!bitmap.isRecycled()) {
Expand All @@ -747,21 +744,21 @@ public TiBlob imageAsResized(Number width, Number height)
}
}

Bitmap img = getImage(opts);
if (img == null) {
return null;
}

try {
Bitmap imageResized = null;
imgWidth = img.getWidth();
imgHeight = img.getHeight();
if (rotation != 0) {
float scaleWidth = (float) dstWidth / imgWidth;
float scaleHeight = (float) dstHeight / imgHeight;
Matrix matrix = new Matrix();
//resize
matrix.postScale(scaleWidth, scaleHeight);
//rotate
matrix.postRotate(rotation);
imageResized = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
imageResized = Bitmap.createBitmap(img, 0, 0, imgWidth, imgHeight, matrix, true);
} else {
imageResized = Bitmap.createScaledBitmap(img, dstWidth, dstHeight, true);
imageResized = img;
}
if (img != image && img != imageResized) {
img.recycle();
Expand Down