Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.
54 changes: 54 additions & 0 deletions src/main/java/androidx/graphics/Bitmap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Contributor

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.

= 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 Bitmap.createBitmap() call for an operation that might not be very common. At least it should be inline, better documented and the return type doesn't need to be specified.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clip is of more clear meaning than createBitmap for bitmap operation. I added inline for all methods and updated docs.

= 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a common usage, please remove.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 clip() apply here).

= createBitmap(this, 0, 0, width, height, Matrix().apply { rotate(degrees, px, py) }, true)