-
Notifications
You must be signed in to change notification settings - Fork 560
Bitmap extensions #418
base: master
Are you sure you want to change the base?
Bitmap extensions #418
Changes from 9 commits
5717646
acfc8b4
1e6bbfa
29b2a18
d1f464d
02fb227
8086a2d
4f57ec8
e421387
4a22496
3517ae4
b855f4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,10 +19,16 @@ | |
| 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 | ||
|
|
@@ -111,3 +117,44 @@ inline fun createBitmap( | |
| ): Bitmap { | ||
| return Bitmap.createBitmap(width, height, config, hasAlpha, colorSpace) | ||
| } | ||
|
|
||
| /** | ||
| * 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], | ||
| * [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 height The height. | ||
| * @return the clipped bitmap | ||
| */ | ||
| inline fun Bitmap.clip(x: Int, y: Int, width: Int, height: Int): Bitmap = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type is not needed. Please rename this method to |
||
| Bitmap.createBitmap(this, x, y, width, height) | ||
|
|
||
| /** | ||
| * 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no pivot point in the API
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry for that, i added it.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the change. The pivot point should probably be set to width/2.0f and height/2.0f by default.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the default pivot point. |
||
| * 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: Float): Bitmap = | ||
| createBitmap(this, 0, 0, width, height, Matrix().apply { setRotate(degrees) }, true) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is not what I meant. What I meant is that there shouldn't be an extension on
Bitmapto compress to a byte array or a stream, etc.Bitmap.compress()already accepts anOuputStreamwhich is generic enough to cover all use cases. It shouldn't be specialized here.