-
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 2 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,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 | ||
|
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. I'm not sure this method is necessary. It doesn't save much over the
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.
|
||
| = 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 | ||
|
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. This is not a common usage, please remove.
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 perfer add this method in order to let operations of bitmap more complete |
||
| = 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 | ||
|
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. I'm not entirely convinced by how common this use case is. (Also same comments that I've mentioned on |
||
| = createBitmap(this, 0, 0, width, height, Matrix().apply { rotate(degrees, px, py) }, 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.
Compressing a bitmap can be done in many ways and compressing into a byte array is just a highly specific use case. I'd rather remove this method.