From 571764668d0f4c988833945d43b32acc5a1e6952 Mon Sep 17 00:00:00 2001 From: feiyanke Date: Fri, 9 Mar 2018 15:34:46 +0800 Subject: [PATCH 01/10] add some extension functions for Bitmap --- src/main/java/androidx/graphics/Bitmap.kt | 54 +++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/main/java/androidx/graphics/Bitmap.kt b/src/main/java/androidx/graphics/Bitmap.kt index 47047862..f45b141e 100644 --- a/src/main/java/androidx/graphics/Bitmap.kt +++ b/src/main/java/androidx/graphics/Bitmap.kt @@ -19,10 +19,18 @@ package androidx.graphics import android.graphics.Bitmap +import android.graphics.Bitmap.CompressFormat +import android.graphics.Bitmap.CompressFormat.JPEG +import android.graphics.Bitmap.CompressFormat.PNG +import android.graphics.Bitmap.createBitmap +import android.graphics.BitmapFactory import android.graphics.Canvas import android.graphics.ColorSpace +import android.graphics.Matrix import android.support.annotation.ColorInt import android.support.annotation.RequiresApi +import java.io.ByteArrayOutputStream +import java.io.File /** * Creates a new [Canvas] to draw on this bitmap and executes the specified @@ -111,3 +119,49 @@ inline fun createBitmap( ): Bitmap { return Bitmap.createBitmap(width, height, config, hasAlpha, colorSpace) } + + +/** + * Bitmap to bytes. + * + * @param format The format of bitmap. + * @param quality Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. + * @return ByteArray + */ +fun Bitmap.toBytes(format:CompressFormat = JPEG, quality: Int=100):ByteArray + = ByteArrayOutputStream().also { compress(format, quality, it) }.toByteArray() + +/** + * Return the clipped bitmap. + * + * @param x The x coordinate of the first pixel. + * @param y The y coordinate of the first pixel. + * @param width The width. + * @param height The height. + * @return the clipped bitmap + */ +fun Bitmap.clip(x:Int, y:Int, width: Int, height: Int):Bitmap + = Bitmap.createBitmap(this, x, y, width, height) + +/** + * Return the skewed bitmap. + * + * @param kx The skew factor of x. + * @param ky The skew factor of y. + * @param px The x coordinate of the pivot point. + * @param py The y coordinate of the pivot point. + * @return the skewed bitmap + */ +fun Bitmap.skew(kx:Float, ky:Float, px:Float = 0f, py:Float = 0f):Bitmap + = createBitmap(this, 0, 0 , width, height, Matrix().apply { skew(kx, ky, px, py) }, true) + +/** + * Return the rotated bitmap. + * + * @param degrees The number of degrees. + * @param px The x coordinate of the pivot point. + * @param py The y coordinate of the pivot point. + * @return the rotated bitmap + */ +fun Bitmap.rotate(degrees:Int, px: Float, py: Float):Bitmap + = createBitmap(this, 0, 0, width, height, Matrix().apply { rotate(degrees, px, py) }, true) \ No newline at end of file From 1e6bbfacdaf7a19c4c64dfc45568cff8f83e5c91 Mon Sep 17 00:00:00 2001 From: feiyanke Date: Mon, 12 Mar 2018 10:03:56 +0800 Subject: [PATCH 02/10] 1. modify toBytes to toByteArray 2. add inline for functions --- src/main/java/androidx/graphics/Bitmap.kt | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/java/androidx/graphics/Bitmap.kt b/src/main/java/androidx/graphics/Bitmap.kt index f45b141e..ad272d34 100644 --- a/src/main/java/androidx/graphics/Bitmap.kt +++ b/src/main/java/androidx/graphics/Bitmap.kt @@ -20,17 +20,14 @@ package androidx.graphics import android.graphics.Bitmap import android.graphics.Bitmap.CompressFormat -import android.graphics.Bitmap.CompressFormat.JPEG -import android.graphics.Bitmap.CompressFormat.PNG import android.graphics.Bitmap.createBitmap -import android.graphics.BitmapFactory import android.graphics.Canvas import android.graphics.ColorSpace import android.graphics.Matrix import android.support.annotation.ColorInt +import android.support.annotation.IntRange import android.support.annotation.RequiresApi import java.io.ByteArrayOutputStream -import java.io.File /** * Creates a new [Canvas] to draw on this bitmap and executes the specified @@ -128,7 +125,7 @@ inline fun createBitmap( * @param quality Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. * @return ByteArray */ -fun Bitmap.toBytes(format:CompressFormat = JPEG, quality: Int=100):ByteArray +inline fun Bitmap.toByteArray(format:CompressFormat = CompressFormat.JPEG, @IntRange(from = 0, to = 100) quality: Int=100) = ByteArrayOutputStream().also { compress(format, quality, it) }.toByteArray() /** @@ -140,7 +137,7 @@ fun Bitmap.toBytes(format:CompressFormat = JPEG, quality: Int=100):ByteArray * @param height The height. * @return the clipped bitmap */ -fun Bitmap.clip(x:Int, y:Int, width: Int, height: Int):Bitmap +inline fun Bitmap.clip(x:Int, y:Int, width: Int, height: Int) = Bitmap.createBitmap(this, x, y, width, height) /** @@ -152,7 +149,7 @@ fun Bitmap.clip(x:Int, y:Int, width: Int, height: Int):Bitmap * @param py The y coordinate of the pivot point. * @return the skewed bitmap */ -fun Bitmap.skew(kx:Float, ky:Float, px:Float = 0f, py:Float = 0f):Bitmap +inline fun Bitmap.skew(kx:Float, ky:Float, px:Float = 0f, py:Float = 0f):Bitmap = createBitmap(this, 0, 0 , width, height, Matrix().apply { skew(kx, ky, px, py) }, true) /** @@ -163,5 +160,5 @@ fun Bitmap.skew(kx:Float, ky:Float, px:Float = 0f, py:Float = 0f):Bitmap * @param py The y coordinate of the pivot point. * @return the rotated bitmap */ -fun Bitmap.rotate(degrees:Int, px: Float, py: Float):Bitmap +inline fun Bitmap.rotate(degrees:Int, px: Float, py: Float) = createBitmap(this, 0, 0, width, height, Matrix().apply { rotate(degrees, px, py) }, true) \ No newline at end of file From d1f464d66bf72e6455835925f4ce6f4c6821a635 Mon Sep 17 00:00:00 2001 From: feiyanke Date: Mon, 12 Mar 2018 10:43:29 +0800 Subject: [PATCH 03/10] update docs --- src/main/java/androidx/graphics/Bitmap.kt | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/androidx/graphics/Bitmap.kt b/src/main/java/androidx/graphics/Bitmap.kt index ad272d34..b985dc5d 100644 --- a/src/main/java/androidx/graphics/Bitmap.kt +++ b/src/main/java/androidx/graphics/Bitmap.kt @@ -119,7 +119,8 @@ inline fun createBitmap( /** - * Bitmap to bytes. + * Returns ByteArray compressed from this bitmap with the specified [format] + * and [quality]. * * @param format The format of bitmap. * @param quality Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. @@ -129,7 +130,9 @@ inline fun Bitmap.toByteArray(format:CompressFormat = CompressFormat.JPEG, @IntR = ByteArrayOutputStream().also { compress(format, quality, it) }.toByteArray() /** - * Return the clipped bitmap. + * Creates a new bitmap, clipped from this bitmap. If the specified [x],[y], + * [width],[height] are the same as the current width and height of this bitmap, + * this bitmap is returned and no new bitmap is created. * * @param x The x coordinate of the first pixel. * @param y The y coordinate of the first pixel. @@ -141,7 +144,9 @@ inline fun Bitmap.clip(x:Int, y:Int, width: Int, height: Int) = Bitmap.createBitmap(this, x, y, width, height) /** - * Return the skewed bitmap. + * Creates a new bitmap, skewed from this bitmap by [kx] and [ky], + * with a pivot point at ([px], [py]). The pivot point is the + * coordinate that should remain unchanged by the specified transformation. * * @param kx The skew factor of x. * @param ky The skew factor of y. @@ -150,15 +155,17 @@ inline fun Bitmap.clip(x:Int, y:Int, width: Int, height: Int) * @return the skewed bitmap */ inline fun Bitmap.skew(kx:Float, ky:Float, px:Float = 0f, py:Float = 0f):Bitmap - = createBitmap(this, 0, 0 , width, height, Matrix().apply { skew(kx, ky, px, py) }, true) + = createBitmap(this, 0, 0 , width, height, Matrix().apply { setSkew(kx, ky, px, py) }, true) /** - * Return the rotated bitmap. + * Creates a new bitmap, rotated from this bitmap by [degrees] - the specified number of degrees, + * with a pivot point at ([px], [py]). The pivot point is the coordinate that should remain + * unchanged by the specified transformation. * * @param degrees The number of degrees. * @param px The x coordinate of the pivot point. * @param py The y coordinate of the pivot point. * @return the rotated bitmap */ -inline fun Bitmap.rotate(degrees:Int, px: Float, py: Float) - = createBitmap(this, 0, 0, width, height, Matrix().apply { rotate(degrees, px, py) }, true) \ No newline at end of file +inline fun Bitmap.rotate(degrees:Float, px: Float, py: Float) + = createBitmap(this, 0, 0, width, height, Matrix().apply { setRotate(degrees, px, py) }, true) \ No newline at end of file From 02fb22780930ed51142d6c9f95c73566aaab75e0 Mon Sep 17 00:00:00 2001 From: feiyanke Date: Mon, 12 Mar 2018 10:49:08 +0800 Subject: [PATCH 04/10] run updateApi --- api/current.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/api/current.txt b/api/current.txt index 6d0ac772..0ae754f0 100644 --- a/api/current.txt +++ b/api/current.txt @@ -23,6 +23,7 @@ package androidx.content { public final class ContextKt { ctor public ContextKt(); + method public static java.util.List getPermissions(android.content.Context); method public static void withStyledAttributes(android.content.Context, android.util.AttributeSet? set = "null", int[] attrs, @AttrRes int defStyleAttr = "0", @StyleRes int defStyleRes = "0", kotlin.jvm.functions.Function1 block); method public static void withStyledAttributes(android.content.Context, @StyleRes int resourceId, int[] attrs, kotlin.jvm.functions.Function1 block); } @@ -101,11 +102,15 @@ package androidx.graphics { public final class BitmapKt { ctor public BitmapKt(); method public static android.graphics.Bitmap applyCanvas(android.graphics.Bitmap, kotlin.jvm.functions.Function1 block); + method public static android.graphics.Bitmap! clip(android.graphics.Bitmap, int x, int y, int width, int height); method public static android.graphics.Bitmap createBitmap(int width, int height, android.graphics.Bitmap.Config config = "Bitmap.Config.ARGB_8888"); method @RequiresApi(26) public static android.graphics.Bitmap createBitmap(int width, int height, android.graphics.Bitmap.Config config = "Bitmap.Config.ARGB_8888", boolean hasAlpha = "true", android.graphics.ColorSpace colorSpace = "ColorSpace.get(ColorSpace.Named.SRGB)"); method public static operator int get(android.graphics.Bitmap, int x, int y); + method public static android.graphics.Bitmap! rotate(android.graphics.Bitmap, float degrees, float px, float py); method public static android.graphics.Bitmap scale(android.graphics.Bitmap, int width, int height, boolean filter = "true"); method public static operator void set(android.graphics.Bitmap, int x, int y, @ColorInt int color); + method public static android.graphics.Bitmap skew(android.graphics.Bitmap, float kx, float ky, float px = "0f", float py = "0f"); + method public static error.NonExistentClass toByteArray(android.graphics.Bitmap, android.graphics.Bitmap.CompressFormat format = "CompressFormat.JPEG", @IntRange(from=0L, to=100L) int quality = "100"); } public final class CanvasKt { From 8086a2d52bcb5983de7ca55741dd998401ccac00 Mon Sep 17 00:00:00 2001 From: feiyanke Date: Mon, 12 Mar 2018 11:43:12 +0800 Subject: [PATCH 05/10] all check pass --- api/current.txt | 1 - src/main/java/androidx/graphics/Bitmap.kt | 41 ++++++++++++----------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/api/current.txt b/api/current.txt index 0ae754f0..d918118c 100644 --- a/api/current.txt +++ b/api/current.txt @@ -23,7 +23,6 @@ package androidx.content { public final class ContextKt { ctor public ContextKt(); - method public static java.util.List getPermissions(android.content.Context); method public static void withStyledAttributes(android.content.Context, android.util.AttributeSet? set = "null", int[] attrs, @AttrRes int defStyleAttr = "0", @StyleRes int defStyleRes = "0", kotlin.jvm.functions.Function1 block); method public static void withStyledAttributes(android.content.Context, @StyleRes int resourceId, int[] attrs, kotlin.jvm.functions.Function1 block); } diff --git a/src/main/java/androidx/graphics/Bitmap.kt b/src/main/java/androidx/graphics/Bitmap.kt index b985dc5d..bc00891c 100644 --- a/src/main/java/androidx/graphics/Bitmap.kt +++ b/src/main/java/androidx/graphics/Bitmap.kt @@ -117,7 +117,6 @@ inline fun createBitmap( return Bitmap.createBitmap(width, height, config, hasAlpha, colorSpace) } - /** * Returns ByteArray compressed from this bitmap with the specified [format] * and [quality]. @@ -126,36 +125,38 @@ inline fun createBitmap( * @param quality Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. * @return ByteArray */ -inline fun Bitmap.toByteArray(format:CompressFormat = CompressFormat.JPEG, @IntRange(from = 0, to = 100) quality: Int=100) - = ByteArrayOutputStream().also { compress(format, quality, it) }.toByteArray() +inline fun Bitmap.toByteArray( + format: CompressFormat = CompressFormat.JPEG, + @IntRange(from = 0, to = 100) quality: Int = 100 +) = ByteArrayOutputStream().also { compress(format, quality, it) }.toByteArray() /** - * Creates a new bitmap, clipped from this bitmap. If the specified [x],[y], - * [width],[height] are the same as the current width and height of this bitmap, + * Creates a new bitmap, clipped from this bitmap. If the specified [x], [y], + * [width], [height] are the same as the current width and height of this bitmap, * this bitmap is returned and no new bitmap is created. * - * @param x The x coordinate of the first pixel. - * @param y The y coordinate of the first pixel. - * @param width The width. + * @param x The x coordinate of the first pixel. + * @param y The y coordinate of the first pixel. + * @param width The width. * @param height The height. * @return the clipped bitmap */ -inline fun Bitmap.clip(x:Int, y:Int, width: Int, height: Int) - = Bitmap.createBitmap(this, x, y, width, height) +inline fun Bitmap.clip(x: Int, y: Int, width: Int, height: Int) = + Bitmap.createBitmap(this, x, y, width, height) /** * Creates a new bitmap, skewed from this bitmap by [kx] and [ky], * with a pivot point at ([px], [py]). The pivot point is the * coordinate that should remain unchanged by the specified transformation. * - * @param kx The skew factor of x. - * @param ky The skew factor of y. - * @param px The x coordinate of the pivot point. - * @param py The y coordinate of the pivot point. + * @param kx The skew factor of x. + * @param ky The skew factor of y. + * @param px The x coordinate of the pivot point. + * @param py The y coordinate of the pivot point. * @return the skewed bitmap */ -inline fun Bitmap.skew(kx:Float, ky:Float, px:Float = 0f, py:Float = 0f):Bitmap - = createBitmap(this, 0, 0 , width, height, Matrix().apply { setSkew(kx, ky, px, py) }, true) +inline fun Bitmap.skew(kx: Float, ky: Float, px: Float = 0f, py: Float = 0f): Bitmap = + createBitmap(this, 0, 0, width, height, Matrix().apply { setSkew(kx, ky, px, py) }, true) /** * Creates a new bitmap, rotated from this bitmap by [degrees] - the specified number of degrees, @@ -163,9 +164,9 @@ inline fun Bitmap.skew(kx:Float, ky:Float, px:Float = 0f, py:Float = 0f):Bitmap * unchanged by the specified transformation. * * @param degrees The number of degrees. - * @param px The x coordinate of the pivot point. - * @param py The y coordinate of the pivot point. + * @param px The x coordinate of the pivot point. + * @param py The y coordinate of the pivot point. * @return the rotated bitmap */ -inline fun Bitmap.rotate(degrees:Float, px: Float, py: Float) - = createBitmap(this, 0, 0, width, height, Matrix().apply { setRotate(degrees, px, py) }, true) \ No newline at end of file +inline fun Bitmap.rotate(degrees: Float, px: Float, py: Float) = + createBitmap(this, 0, 0, width, height, Matrix().apply { setRotate(degrees, px, py) }, true) \ No newline at end of file From 4f57ec88e5500b6e43a1059c328dae3094c5c492 Mon Sep 17 00:00:00 2001 From: feiyanke Date: Mon, 12 Mar 2018 15:21:15 +0800 Subject: [PATCH 06/10] add test and do small change of api --- api/current.txt | 4 +- .../java/androidx/graphics/BitmapTest.kt | 39 +++++++++++++++++++ src/main/java/androidx/graphics/Bitmap.kt | 8 ++-- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/api/current.txt b/api/current.txt index d918118c..4537d586 100644 --- a/api/current.txt +++ b/api/current.txt @@ -105,10 +105,10 @@ package androidx.graphics { method public static android.graphics.Bitmap createBitmap(int width, int height, android.graphics.Bitmap.Config config = "Bitmap.Config.ARGB_8888"); method @RequiresApi(26) public static android.graphics.Bitmap createBitmap(int width, int height, android.graphics.Bitmap.Config config = "Bitmap.Config.ARGB_8888", boolean hasAlpha = "true", android.graphics.ColorSpace colorSpace = "ColorSpace.get(ColorSpace.Named.SRGB)"); method public static operator int get(android.graphics.Bitmap, int x, int y); - method public static android.graphics.Bitmap! rotate(android.graphics.Bitmap, float degrees, float px, float py); + method public static android.graphics.Bitmap! rotate(android.graphics.Bitmap, float degrees); method public static android.graphics.Bitmap scale(android.graphics.Bitmap, int width, int height, boolean filter = "true"); method public static operator void set(android.graphics.Bitmap, int x, int y, @ColorInt int color); - method public static android.graphics.Bitmap skew(android.graphics.Bitmap, float kx, float ky, float px = "0f", float py = "0f"); + method public static android.graphics.Bitmap! skew(android.graphics.Bitmap, float kx, float ky); method public static error.NonExistentClass toByteArray(android.graphics.Bitmap, android.graphics.Bitmap.CompressFormat format = "CompressFormat.JPEG", @IntRange(from=0L, to=100L) int quality = "100"); } diff --git a/src/androidTest/java/androidx/graphics/BitmapTest.kt b/src/androidTest/java/androidx/graphics/BitmapTest.kt index 2cf7ded1..8e31b982 100644 --- a/src/androidTest/java/androidx/graphics/BitmapTest.kt +++ b/src/androidTest/java/androidx/graphics/BitmapTest.kt @@ -17,6 +17,8 @@ package androidx.graphics import android.graphics.Bitmap +import android.graphics.Bitmap.CompressFormat.PNG +import android.graphics.BitmapFactory import android.graphics.ColorSpace import android.support.test.filters.SdkSuppress import org.junit.Assert.assertEquals @@ -68,4 +70,41 @@ class BitmapTest { b[1, 1] = 0x40302010 assertEquals(0x40302010, b[1, 1]) } + + @Test fun clip() { + val src = createBitmap(10, 10) + src[3, 5] = 0x40302010 + val res = src.clip(3, 5, 2, 2) + assertEquals(2, res.width) + assertEquals(2, res.height) + assertEquals(0x40302010, res[0, 0]) + } + + @Test fun skew() { + val src = createBitmap(10, 10) + val res = src.skew(0.5f, 2f) + assertEquals(15, res.width) + assertEquals(30, res.height) + } + + @Test fun rotate() { + val src = createBitmap(10, 10) + src[3, 5] = 0x40302010 + src[0, 0] = 0x10203010 + val res = src.rotate(90f) + assertEquals(10, res.width) + assertEquals(10, res.height) + assertEquals(0x10203010, res[9, 0]) + assertEquals(0x40302010, res[4, 3]) + } + + @Test fun toByteArray() { + val src = createBitmap(10, 10) + src[3, 5] = 0x40302010 + val bytes = src.toByteArray(PNG) + val back = BitmapFactory.decodeByteArray(bytes, 0, bytes.size) + assertEquals(10, back.width) + assertEquals(10, back.height) + assertEquals(0x40302010, back[3, 5]) + } } diff --git a/src/main/java/androidx/graphics/Bitmap.kt b/src/main/java/androidx/graphics/Bitmap.kt index bc00891c..adc43495 100644 --- a/src/main/java/androidx/graphics/Bitmap.kt +++ b/src/main/java/androidx/graphics/Bitmap.kt @@ -155,8 +155,8 @@ inline fun Bitmap.clip(x: Int, y: Int, width: Int, height: Int) = * @param py The y coordinate of the pivot point. * @return the skewed bitmap */ -inline fun Bitmap.skew(kx: Float, ky: Float, px: Float = 0f, py: Float = 0f): Bitmap = - createBitmap(this, 0, 0, width, height, Matrix().apply { setSkew(kx, ky, px, py) }, true) +inline fun Bitmap.skew(kx: Float, ky: Float) = + createBitmap(this, 0, 0, width, height, Matrix().apply { setSkew(kx, ky) }, true) /** * Creates a new bitmap, rotated from this bitmap by [degrees] - the specified number of degrees, @@ -168,5 +168,5 @@ inline fun Bitmap.skew(kx: Float, ky: Float, px: Float = 0f, py: Float = 0f): Bi * @param py The y coordinate of the pivot point. * @return the rotated bitmap */ -inline fun Bitmap.rotate(degrees: Float, px: Float, py: Float) = - createBitmap(this, 0, 0, width, height, Matrix().apply { setRotate(degrees, px, py) }, true) \ No newline at end of file +inline fun Bitmap.rotate(degrees: Float) = + createBitmap(this, 0, 0, width, height, Matrix().apply { setRotate(degrees) }, true) \ No newline at end of file From e421387d465717c2c45c5bbbeb0f0521caa9f82d Mon Sep 17 00:00:00 2001 From: feiyanke Date: Tue, 13 Mar 2018 10:36:59 +0800 Subject: [PATCH 07/10] remove skew() and modify toByteArray() to toStream() --- api/current.txt | 7 ++--- .../java/androidx/graphics/BitmapTest.kt | 13 ++------- src/main/java/androidx/graphics/Bitmap.kt | 28 ++++++------------- 3 files changed, 14 insertions(+), 34 deletions(-) diff --git a/api/current.txt b/api/current.txt index 4537d586..41a6b74b 100644 --- a/api/current.txt +++ b/api/current.txt @@ -101,15 +101,14 @@ package androidx.graphics { public final class BitmapKt { ctor public BitmapKt(); method public static android.graphics.Bitmap applyCanvas(android.graphics.Bitmap, kotlin.jvm.functions.Function1 block); - method public static android.graphics.Bitmap! clip(android.graphics.Bitmap, int x, int y, int width, int height); + method public static android.graphics.Bitmap clip(android.graphics.Bitmap, int x, int y, int width, int height); method public static android.graphics.Bitmap createBitmap(int width, int height, android.graphics.Bitmap.Config config = "Bitmap.Config.ARGB_8888"); method @RequiresApi(26) public static android.graphics.Bitmap createBitmap(int width, int height, android.graphics.Bitmap.Config config = "Bitmap.Config.ARGB_8888", boolean hasAlpha = "true", android.graphics.ColorSpace colorSpace = "ColorSpace.get(ColorSpace.Named.SRGB)"); method public static operator int get(android.graphics.Bitmap, int x, int y); - method public static android.graphics.Bitmap! rotate(android.graphics.Bitmap, float degrees); + method public static android.graphics.Bitmap rotate(android.graphics.Bitmap, float degrees); method public static android.graphics.Bitmap scale(android.graphics.Bitmap, int width, int height, boolean filter = "true"); method public static operator void set(android.graphics.Bitmap, int x, int y, @ColorInt int color); - method public static android.graphics.Bitmap! skew(android.graphics.Bitmap, float kx, float ky); - method public static error.NonExistentClass toByteArray(android.graphics.Bitmap, android.graphics.Bitmap.CompressFormat format = "CompressFormat.JPEG", @IntRange(from=0L, to=100L) int quality = "100"); + method public static java.io.ByteArrayInputStream toStream(android.graphics.Bitmap, android.graphics.Bitmap.CompressFormat format = "CompressFormat.JPEG", @IntRange(from=0L, to=100L) int quality = "100"); } public final class CanvasKt { diff --git a/src/androidTest/java/androidx/graphics/BitmapTest.kt b/src/androidTest/java/androidx/graphics/BitmapTest.kt index 8e31b982..25a53f0a 100644 --- a/src/androidTest/java/androidx/graphics/BitmapTest.kt +++ b/src/androidTest/java/androidx/graphics/BitmapTest.kt @@ -80,13 +80,6 @@ class BitmapTest { assertEquals(0x40302010, res[0, 0]) } - @Test fun skew() { - val src = createBitmap(10, 10) - val res = src.skew(0.5f, 2f) - assertEquals(15, res.width) - assertEquals(30, res.height) - } - @Test fun rotate() { val src = createBitmap(10, 10) src[3, 5] = 0x40302010 @@ -98,11 +91,11 @@ class BitmapTest { assertEquals(0x40302010, res[4, 3]) } - @Test fun toByteArray() { + @Test fun toStream() { val src = createBitmap(10, 10) src[3, 5] = 0x40302010 - val bytes = src.toByteArray(PNG) - val back = BitmapFactory.decodeByteArray(bytes, 0, bytes.size) + val stream = src.toStream(PNG) + val back = BitmapFactory.decodeStream(stream) assertEquals(10, back.width) assertEquals(10, back.height) assertEquals(0x40302010, back[3, 5]) diff --git a/src/main/java/androidx/graphics/Bitmap.kt b/src/main/java/androidx/graphics/Bitmap.kt index adc43495..3c2c5a3d 100644 --- a/src/main/java/androidx/graphics/Bitmap.kt +++ b/src/main/java/androidx/graphics/Bitmap.kt @@ -27,6 +27,7 @@ import android.graphics.Matrix import android.support.annotation.ColorInt import android.support.annotation.IntRange import android.support.annotation.RequiresApi +import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream /** @@ -118,17 +119,18 @@ inline fun createBitmap( } /** - * Returns ByteArray compressed from this bitmap with the specified [format] + * Returns ByteArrayInputStream compressed from this bitmap with the specified [format] * and [quality]. * * @param format The format of bitmap. * @param quality Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. - * @return ByteArray + * @return ByteArrayInputStream */ -inline fun Bitmap.toByteArray( +inline fun Bitmap.toStream( format: CompressFormat = CompressFormat.JPEG, @IntRange(from = 0, to = 100) quality: Int = 100 -) = ByteArrayOutputStream().also { compress(format, quality, it) }.toByteArray() +): ByteArrayInputStream = + ByteArrayOutputStream().also { compress(format, quality, it) }.toByteArray().inputStream() /** * Creates a new bitmap, clipped from this bitmap. If the specified [x], [y], @@ -141,23 +143,9 @@ inline fun Bitmap.toByteArray( * @param height The height. * @return the clipped bitmap */ -inline fun Bitmap.clip(x: Int, y: Int, width: Int, height: Int) = +inline fun Bitmap.clip(x: Int, y: Int, width: Int, height: Int): Bitmap = Bitmap.createBitmap(this, x, y, width, height) -/** - * Creates a new bitmap, skewed from this bitmap by [kx] and [ky], - * with a pivot point at ([px], [py]). The pivot point is the - * coordinate that should remain unchanged by the specified transformation. - * - * @param kx The skew factor of x. - * @param ky The skew factor of y. - * @param px The x coordinate of the pivot point. - * @param py The y coordinate of the pivot point. - * @return the skewed bitmap - */ -inline fun Bitmap.skew(kx: Float, ky: Float) = - createBitmap(this, 0, 0, width, height, Matrix().apply { setSkew(kx, ky) }, true) - /** * Creates a new bitmap, rotated from this bitmap by [degrees] - the specified number of degrees, * with a pivot point at ([px], [py]). The pivot point is the coordinate that should remain @@ -168,5 +156,5 @@ inline fun Bitmap.skew(kx: Float, ky: Float) = * @param py The y coordinate of the pivot point. * @return the rotated bitmap */ -inline fun Bitmap.rotate(degrees: Float) = +inline fun Bitmap.rotate(degrees: Float): Bitmap = createBitmap(this, 0, 0, width, height, Matrix().apply { setRotate(degrees) }, true) \ No newline at end of file From 4a224967704fee22e4c3e92cf6978761ba001e72 Mon Sep 17 00:00:00 2001 From: feiyanke Date: Wed, 14 Mar 2018 10:57:22 +0800 Subject: [PATCH 08/10] remove toStream(), rename clip() to crop() and remove the return type --- api/current.txt | 5 ++-- .../java/androidx/graphics/BitmapTest.kt | 16 ++---------- src/main/java/androidx/graphics/Bitmap.kt | 26 +++---------------- 3 files changed, 8 insertions(+), 39 deletions(-) diff --git a/api/current.txt b/api/current.txt index 41a6b74b..bacc149e 100644 --- a/api/current.txt +++ b/api/current.txt @@ -101,14 +101,13 @@ package androidx.graphics { public final class BitmapKt { ctor public BitmapKt(); method public static android.graphics.Bitmap applyCanvas(android.graphics.Bitmap, kotlin.jvm.functions.Function1 block); - method public static android.graphics.Bitmap clip(android.graphics.Bitmap, int x, int y, int width, int height); method public static android.graphics.Bitmap createBitmap(int width, int height, android.graphics.Bitmap.Config config = "Bitmap.Config.ARGB_8888"); method @RequiresApi(26) public static android.graphics.Bitmap createBitmap(int width, int height, android.graphics.Bitmap.Config config = "Bitmap.Config.ARGB_8888", boolean hasAlpha = "true", android.graphics.ColorSpace colorSpace = "ColorSpace.get(ColorSpace.Named.SRGB)"); + method public static android.graphics.Bitmap! crop(android.graphics.Bitmap, int x, int y, int width, int height); method public static operator int get(android.graphics.Bitmap, int x, int y); - method public static android.graphics.Bitmap rotate(android.graphics.Bitmap, float degrees); + method public static android.graphics.Bitmap! rotate(android.graphics.Bitmap, float degrees); method public static android.graphics.Bitmap scale(android.graphics.Bitmap, int width, int height, boolean filter = "true"); method public static operator void set(android.graphics.Bitmap, int x, int y, @ColorInt int color); - method public static java.io.ByteArrayInputStream toStream(android.graphics.Bitmap, android.graphics.Bitmap.CompressFormat format = "CompressFormat.JPEG", @IntRange(from=0L, to=100L) int quality = "100"); } public final class CanvasKt { diff --git a/src/androidTest/java/androidx/graphics/BitmapTest.kt b/src/androidTest/java/androidx/graphics/BitmapTest.kt index 25a53f0a..08875726 100644 --- a/src/androidTest/java/androidx/graphics/BitmapTest.kt +++ b/src/androidTest/java/androidx/graphics/BitmapTest.kt @@ -17,8 +17,6 @@ package androidx.graphics import android.graphics.Bitmap -import android.graphics.Bitmap.CompressFormat.PNG -import android.graphics.BitmapFactory import android.graphics.ColorSpace import android.support.test.filters.SdkSuppress import org.junit.Assert.assertEquals @@ -71,10 +69,10 @@ class BitmapTest { assertEquals(0x40302010, b[1, 1]) } - @Test fun clip() { + @Test fun crop() { val src = createBitmap(10, 10) src[3, 5] = 0x40302010 - val res = src.clip(3, 5, 2, 2) + val res = src.crop(3, 5, 2, 2) assertEquals(2, res.width) assertEquals(2, res.height) assertEquals(0x40302010, res[0, 0]) @@ -90,14 +88,4 @@ class BitmapTest { assertEquals(0x10203010, res[9, 0]) assertEquals(0x40302010, res[4, 3]) } - - @Test fun toStream() { - val src = createBitmap(10, 10) - src[3, 5] = 0x40302010 - val stream = src.toStream(PNG) - val back = BitmapFactory.decodeStream(stream) - assertEquals(10, back.width) - assertEquals(10, back.height) - assertEquals(0x40302010, back[3, 5]) - } } diff --git a/src/main/java/androidx/graphics/Bitmap.kt b/src/main/java/androidx/graphics/Bitmap.kt index 3c2c5a3d..356db0a3 100644 --- a/src/main/java/androidx/graphics/Bitmap.kt +++ b/src/main/java/androidx/graphics/Bitmap.kt @@ -19,16 +19,12 @@ package androidx.graphics import android.graphics.Bitmap -import android.graphics.Bitmap.CompressFormat import android.graphics.Bitmap.createBitmap import android.graphics.Canvas import android.graphics.ColorSpace import android.graphics.Matrix import android.support.annotation.ColorInt -import android.support.annotation.IntRange import android.support.annotation.RequiresApi -import java.io.ByteArrayInputStream -import java.io.ByteArrayOutputStream /** * Creates a new [Canvas] to draw on this bitmap and executes the specified @@ -119,21 +115,7 @@ inline fun createBitmap( } /** - * Returns ByteArrayInputStream compressed from this bitmap with the specified [format] - * and [quality]. - * - * @param format The format of bitmap. - * @param quality Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. - * @return ByteArrayInputStream - */ -inline fun Bitmap.toStream( - format: CompressFormat = CompressFormat.JPEG, - @IntRange(from = 0, to = 100) quality: Int = 100 -): ByteArrayInputStream = - ByteArrayOutputStream().also { compress(format, quality, it) }.toByteArray().inputStream() - -/** - * Creates a new bitmap, clipped from this bitmap. If the specified [x], [y], + * Creates a new bitmap, cropped from this bitmap. If the specified [x], [y], * [width], [height] are the same as the current width and height of this bitmap, * this bitmap is returned and no new bitmap is created. * @@ -141,9 +123,9 @@ inline fun Bitmap.toStream( * @param y The y coordinate of the first pixel. * @param width The width. * @param height The height. - * @return the clipped bitmap + * @return the cropped bitmap */ -inline fun Bitmap.clip(x: Int, y: Int, width: Int, height: Int): Bitmap = +inline fun Bitmap.crop(x: Int, y: Int, width: Int, height: Int) = Bitmap.createBitmap(this, x, y, width, height) /** @@ -156,5 +138,5 @@ inline fun Bitmap.clip(x: Int, y: Int, width: Int, height: Int): Bitmap = * @param py The y coordinate of the pivot point. * @return the rotated bitmap */ -inline fun Bitmap.rotate(degrees: Float): Bitmap = +inline fun Bitmap.rotate(degrees: Float) = createBitmap(this, 0, 0, width, height, Matrix().apply { setRotate(degrees) }, true) \ No newline at end of file From 3517ae46b4395d24d2ada4faeb73ef5a2942e27b Mon Sep 17 00:00:00 2001 From: feiyanke Date: Mon, 21 May 2018 18:00:49 +0800 Subject: [PATCH 09/10] add pivot point parameters for rotate method --- api/current.txt | 2 +- src/androidTest/java/androidx/graphics/BitmapTest.kt | 2 +- src/main/java/androidx/graphics/Bitmap.kt | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/api/current.txt b/api/current.txt index bacc149e..73e3ddbc 100644 --- a/api/current.txt +++ b/api/current.txt @@ -105,7 +105,7 @@ package androidx.graphics { method @RequiresApi(26) public static android.graphics.Bitmap createBitmap(int width, int height, android.graphics.Bitmap.Config config = "Bitmap.Config.ARGB_8888", boolean hasAlpha = "true", android.graphics.ColorSpace colorSpace = "ColorSpace.get(ColorSpace.Named.SRGB)"); method public static android.graphics.Bitmap! crop(android.graphics.Bitmap, int x, int y, int width, int height); method public static operator int get(android.graphics.Bitmap, int x, int y); - method public static android.graphics.Bitmap! rotate(android.graphics.Bitmap, float degrees); + method public static android.graphics.Bitmap! rotate(android.graphics.Bitmap, float degrees, float px, float py); method public static android.graphics.Bitmap scale(android.graphics.Bitmap, int width, int height, boolean filter = "true"); method public static operator void set(android.graphics.Bitmap, int x, int y, @ColorInt int color); } diff --git a/src/androidTest/java/androidx/graphics/BitmapTest.kt b/src/androidTest/java/androidx/graphics/BitmapTest.kt index 08875726..4f15ab52 100644 --- a/src/androidTest/java/androidx/graphics/BitmapTest.kt +++ b/src/androidTest/java/androidx/graphics/BitmapTest.kt @@ -82,7 +82,7 @@ class BitmapTest { val src = createBitmap(10, 10) src[3, 5] = 0x40302010 src[0, 0] = 0x10203010 - val res = src.rotate(90f) + val res = src.rotate(90f, 0f, 0f) assertEquals(10, res.width) assertEquals(10, res.height) assertEquals(0x10203010, res[9, 0]) diff --git a/src/main/java/androidx/graphics/Bitmap.kt b/src/main/java/androidx/graphics/Bitmap.kt index 356db0a3..e02d7ed0 100644 --- a/src/main/java/androidx/graphics/Bitmap.kt +++ b/src/main/java/androidx/graphics/Bitmap.kt @@ -138,5 +138,5 @@ inline fun Bitmap.crop(x: Int, y: Int, width: Int, height: Int) = * @param py The y coordinate of the pivot point. * @return the rotated bitmap */ -inline fun Bitmap.rotate(degrees: Float) = - createBitmap(this, 0, 0, width, height, Matrix().apply { setRotate(degrees) }, true) \ No newline at end of file +inline fun Bitmap.rotate(degrees: Float, px: Float, py: Float) = + createBitmap(this, 0, 0, width, height, Matrix().apply { setRotate(degrees, px, py) }, true) \ No newline at end of file From b855f4cf7ac0a37f32c69e9fe5a0e612148afb28 Mon Sep 17 00:00:00 2001 From: feiyanke Date: Mon, 11 Jun 2018 09:45:01 +0800 Subject: [PATCH 10/10] add default pivot point for rotate method --- api/current.txt | 2 +- src/main/java/androidx/graphics/Bitmap.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/api/current.txt b/api/current.txt index 73e3ddbc..911274a9 100644 --- a/api/current.txt +++ b/api/current.txt @@ -105,7 +105,7 @@ package androidx.graphics { method @RequiresApi(26) public static android.graphics.Bitmap createBitmap(int width, int height, android.graphics.Bitmap.Config config = "Bitmap.Config.ARGB_8888", boolean hasAlpha = "true", android.graphics.ColorSpace colorSpace = "ColorSpace.get(ColorSpace.Named.SRGB)"); method public static android.graphics.Bitmap! crop(android.graphics.Bitmap, int x, int y, int width, int height); method public static operator int get(android.graphics.Bitmap, int x, int y); - method public static android.graphics.Bitmap! rotate(android.graphics.Bitmap, float degrees, float px, float py); + method public static android.graphics.Bitmap! rotate(android.graphics.Bitmap, float degrees, float px = "width / 2.0f", float py = "height / 2.0f"); method public static android.graphics.Bitmap scale(android.graphics.Bitmap, int width, int height, boolean filter = "true"); method public static operator void set(android.graphics.Bitmap, int x, int y, @ColorInt int color); } diff --git a/src/main/java/androidx/graphics/Bitmap.kt b/src/main/java/androidx/graphics/Bitmap.kt index e02d7ed0..df164976 100644 --- a/src/main/java/androidx/graphics/Bitmap.kt +++ b/src/main/java/androidx/graphics/Bitmap.kt @@ -138,5 +138,5 @@ inline fun Bitmap.crop(x: Int, y: Int, width: Int, height: Int) = * @param py The y coordinate of the pivot point. * @return the rotated bitmap */ -inline fun Bitmap.rotate(degrees: Float, px: Float, py: Float) = +inline fun Bitmap.rotate(degrees: Float, px: Float = width / 2.0f, py: Float = height / 2.0f) = createBitmap(this, 0, 0, width, height, Matrix().apply { setRotate(degrees, px, py) }, true) \ No newline at end of file