| 278 | } |
| 279 | } |
| 280 | |
| 281 | @Keep |
| 282 | fun snapshot(callback: ValdiFunction) { |
| 283 | var bitmap: BitmapHandler? = null |
| 284 | |
| 285 | fun handleError() { |
| 286 | ValdiMarshaller.use { |
| 287 | it.pushUndefined() |
| 288 | callback.perform(it) |
| 289 | } |
| 290 | bitmap?.release() |
| 291 | } |
| 292 | |
| 293 | val view = get() ?: return handleError() |
| 294 | |
| 295 | val width = view.width |
| 296 | val height = view.height |
| 297 | |
| 298 | if (width < 1 || height < 1) { |
| 299 | return handleError() |
| 300 | } |
| 301 | |
| 302 | try { |
| 303 | val renderCanvas = this.support.getRenderCanvas() |
| 304 | |
| 305 | bitmap = this.support.bitmapPool.get(width, height) ?: return handleError() |
| 306 | |
| 307 | drawViewInCanvas(renderCanvas, bitmap.getBitmap(), view, null, true) |
| 308 | |
| 309 | val output = ByteArrayOutputStream() |
| 310 | |
| 311 | this.support.getSnapshotExecutor().submit { |
| 312 | bitmap.getBitmap().compress(Bitmap.CompressFormat.PNG, 0, output) |
| 313 | ValdiMarshaller.use { |
| 314 | it.pushByteArray(output.toByteArray()) |
| 315 | callback.perform(it) |
| 316 | } |
| 317 | bitmap?.release() |
| 318 | } |
| 319 | } catch (exc: Exception) { |
| 320 | this.support.logger.log(LogLevel.ERROR, exc, "Failed to take Snapshot of view with size ${width}x${height}") |
| 321 | handleError() |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | @Keep |
no test coverage detected