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

feat(android): backgroundColor property in imageAsResized #365

Open
wants to merge 2 commits into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Creates a new image by creating a copy of the given image that is rescaled to th
* imagefactory.QUALITY\_MEDIUM
* imagefactory.QUALITY\_HIGH
* Android. format [int]: The output format of the image: either ImageFactory.PNG or ImageFactory.JPEG (default: ImageFactory.JPEG)
* Android. backgroundColor [int]: Background color of the resized image e.g. when using a transparent PNG that you want to convert to a JPEG.
* quality[float]: The quality of the resulting JPEG or WebP image, expressed as a value from 0.0 to 1.0. The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality). (default: 0.7)

### imageAsCropped(blob, options)
Expand Down
2 changes: 1 addition & 1 deletion android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 5.1.0
version: 5.1.1
apiversion: 4
architectures: arm64-v8a armeabi-v7a x86 x86_64
description: Image Factory
Expand Down
25 changes: 23 additions & 2 deletions android/src/ti/imagefactory/ImageFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
Expand All @@ -18,6 +19,7 @@
import java.io.OutputStream;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBlob;
import org.appcelerator.titanium.util.TiConvert;

Expand All @@ -33,7 +35,16 @@ public static TiBlob imageRotate(TiBlob blob, KrollDict args)
args = updateFormatOption(args, blob.getMimeType(), false);
Bitmap oldBitmap = blob.getImage();
Bitmap newBitmap = imageRotate(oldBitmap, args, TiExifOrientation.from(blob));
return compressToBlob(newBitmap, args, (newBitmap != oldBitmap));

if (args.containsKeyAndNotNull("backgroundColor")) {
Bitmap mutableBitmap = newBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawColor(TiConvert.toColor(args.get("backgroundColor"), TiApplication.getAppRootOrCurrentActivity()));
canvas.drawBitmap(newBitmap, 0F, 0F, null);
return compressToBlob(mutableBitmap, args, (newBitmap != oldBitmap));
} else {
return compressToBlob(newBitmap, args, (newBitmap != oldBitmap));
}
}

public static Bitmap imageRotate(Bitmap bitmap, KrollDict args, TiExifOrientation exifOrientation)
Expand Down Expand Up @@ -147,7 +158,17 @@ public static TiBlob imageResize(TiBlob blob, KrollDict args)
args = updateFormatOption(args, blob.getMimeType(), false);
Bitmap oldBitmap = blob.getImage();
Bitmap newBitmap = imageResize(oldBitmap, args, TiExifOrientation.from(blob));
return compressToBlob(newBitmap, args, (newBitmap != oldBitmap));

if (args.containsKeyAndNotNull("backgroundColor")) {
Bitmap mutableBitmap = newBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawColor(TiConvert.toColor(args.get("backgroundColor"), TiApplication.getAppRootOrCurrentActivity()));
canvas.drawBitmap(newBitmap, 0F, 0F, null);
return compressToBlob(mutableBitmap, args, (newBitmap != oldBitmap));
} else {
return compressToBlob(newBitmap, args, (newBitmap != oldBitmap));
}

}

public static Bitmap imageResize(Bitmap bitmap, KrollDict args, TiExifOrientation exifOrientation)
Expand Down