diff --git a/README.md b/README.md index 19ae17f..4426ed6 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/android/manifest b/android/manifest index aa04036..3165d75 100644 --- a/android/manifest +++ b/android/manifest @@ -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 diff --git a/android/src/ti/imagefactory/ImageFactory.java b/android/src/ti/imagefactory/ImageFactory.java index ce31953..01f713b 100644 --- a/android/src/ti/imagefactory/ImageFactory.java +++ b/android/src/ti/imagefactory/ImageFactory.java @@ -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; @@ -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; @@ -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) @@ -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)