readPixels

fun readPixels(bitmap: Bitmap, srcX: Int, srcY: Int): Boolean

Copies Rect of pixels from Canvas into bitmap. Matrix and clip are ignored.

Source Rect corners are (srcX, srcY) and (imageInfo().width(), imageInfo().height()). Destination Rect corners are (0, 0) and (bitmap.width(), bitmap.height()). Copies each readable pixel intersecting both rectangles, without scaling, converting to bitmap.colorType() and bitmap.alphaType() if required.

Pixels are readable when BaseDevice is raster, or backed by a GPU. Pixels are not readable when Canvas is returned by Document::beginPage, returned by PictureRecorder::beginRecording, or Canvas is the base of a utility class like DebugCanvas.

Caller must allocate pixel storage in bitmap if needed.

SkBitmap values are converted only if ColorType and AlphaType do not match. Only pixels within both source and destination rectangles are copied. Bitmap pixels outside Rect intersection are unchanged.

Pass negative values for srcX or srcY to offset pixels across or down bitmap.

Does not copy, and returns false if:

  • Source and destination rectangles do not intersect.

  • SkCanvas pixels could not be converted to bitmap.colorType() or bitmap.alphaType().

  • SkCanvas pixels are not readable; for instance, Canvas is document-based.

  • bitmap pixels could not be allocated.

  • bitmap.rowBytes() is too small to contain one row of pixels.

Return

true if pixels were copied

Parameters

bitmap

storage for pixels copied from Canvas

srcX

offset into readable pixels on x-axis; may be negative

srcY

offset into readable pixels on y-axis; may be negative

See also