-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
ViewShot.java
671 lines (565 loc) · 22.7 KB
/
ViewShot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
package versioned.host.exp.exponent.modules.api.viewshot;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.StringDef;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.util.Base64;
import android.util.Log;
import android.view.PixelCopy;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.NativeViewHierarchyManager;
import com.facebook.react.uimanager.UIBlock;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.zip.Deflater;
import javax.annotation.Nullable;
import static android.view.View.VISIBLE;
/**
* Snapshot utility class allow to screenshot a view.
*/
public class ViewShot implements UIBlock, LifecycleEventListener {
//region Constants
/**
* Tag fort Class logs.
*/
private static final String TAG = ViewShot.class.getSimpleName();
/**
* Error code that we return to RN.
*/
public static final String ERROR_UNABLE_TO_SNAPSHOT = "E_UNABLE_TO_SNAPSHOT";
/**
* pre-allocated output stream size for screenshot. In real life example it will eb around 7Mb.
*/
private static final int PREALLOCATE_SIZE = 64 * 1024;
/**
* ARGB size in bytes.
*/
private static final int ARGB_SIZE = 4;
/**
* Wait timeout for surface view capture.
*/
private static final int SURFACE_VIEW_READ_PIXELS_TIMEOUT = 5;
private HandlerThread mBgThread;
private Handler mBgHandler;
@Override
public void onHostResume() {
}
@Override
public void onHostPause() {
}
@Override
public void onHostDestroy() {
this.reactContext.removeLifecycleEventListener(this);
mBgHandler.post(new Runnable() {
@Override
public void run() {
cleanup();
}
});
}
@SuppressWarnings("WeakerAccess")
@IntDef({Formats.JPEG, Formats.PNG, Formats.WEBP, Formats.RAW})
public @interface Formats {
int JPEG = 0; // Bitmap.CompressFormat.JPEG.ordinal();
int PNG = 1; // Bitmap.CompressFormat.PNG.ordinal();
int WEBP = 2; // Bitmap.CompressFormat.WEBP.ordinal();
int RAW = -1;
Bitmap.CompressFormat[] mapping = {
Bitmap.CompressFormat.JPEG,
Bitmap.CompressFormat.PNG,
Bitmap.CompressFormat.WEBP
};
}
/**
* Supported Output results.
*/
@StringDef({Results.BASE_64, Results.DATA_URI, Results.TEMP_FILE, Results.ZIP_BASE_64})
public @interface Results {
/**
* Save screenshot as temp file on device.
*/
String TEMP_FILE = "tmpfile";
/**
* Base 64 encoded image.
*/
String BASE_64 = "base64";
/**
* Zipped RAW image in base 64 encoding.
*/
String ZIP_BASE_64 = "zip-base64";
/**
* Base64 data uri.
*/
String DATA_URI = "data-uri";
}
//endregion
//region Static members
/**
* Image output buffer used as a source for base64 encoding
*/
private static byte[] outputBuffer = new byte[PREALLOCATE_SIZE];
//endregion
//region Class members
private final int tag;
private final String extension;
@Formats
private final int format;
private final double quality;
private final Integer width;
private final Integer height;
private final File output;
@Results
private final String result;
private final Promise promise;
private final Boolean snapshotContentContainer;
@SuppressWarnings({"unused", "FieldCanBeLocal"})
private final ReactApplicationContext reactContext;
private final boolean handleGLSurfaceView;
private final Activity currentActivity;
//endregion
//region Constructors
@SuppressWarnings("WeakerAccess")
public ViewShot(
final int tag,
final String extension,
@Formats final int format,
final double quality,
@Nullable Integer width,
@Nullable Integer height,
final File output,
@Results final String result,
final Boolean snapshotContentContainer,
final ReactApplicationContext reactContext,
final Activity currentActivity,
final boolean handleGLSurfaceView,
final Promise promise) {
this.tag = tag;
this.extension = extension;
this.format = format;
this.quality = quality;
this.width = width;
this.height = height;
this.output = output;
this.result = result;
this.snapshotContentContainer = snapshotContentContainer;
this.reactContext = reactContext;
this.currentActivity = currentActivity;
this.handleGLSurfaceView = handleGLSurfaceView;
this.promise = promise;
reactContext.addLifecycleEventListener(this);
// bg hanadler for non UI heavy work
mBgThread = new HandlerThread("RNViewShot-Handler-Thread");
mBgThread.start();
mBgHandler = new Handler(mBgThread.getLooper());
}
//endregion
private void cleanup() {
if (mBgThread != null) {
if (Build.VERSION.SDK_INT < 18) {
mBgThread.quit();
} else {
mBgThread.quitSafely();
}
mBgThread = null;
}
}
//region Overrides
@Override
public void execute(final NativeViewHierarchyManager nativeViewHierarchyManager) {
mBgHandler.post(new Runnable() {
@Override
public void run() {
final View view;
if (tag == -1) {
view = currentActivity.getWindow().getDecorView().findViewById(android.R.id.content);
} else {
view = nativeViewHierarchyManager.resolveView(tag);
}
if (view == null) {
Log.e(TAG, "No view found with reactTag: " + tag, new AssertionError());
promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "No view found with reactTag: " + tag);
return;
}
try {
final ReusableByteArrayOutputStream stream = new ReusableByteArrayOutputStream(outputBuffer);
stream.setSize(proposeSize(view));
outputBuffer = stream.innerBuffer();
if (Results.TEMP_FILE.equals(result) && Formats.RAW == format) {
saveToRawFileOnDevice(view);
} else if (Results.TEMP_FILE.equals(result) && Formats.RAW != format) {
saveToTempFileOnDevice(view);
} else if (Results.BASE_64.equals(result) || Results.ZIP_BASE_64.equals(result)) {
saveToBase64String(view);
} else if (Results.DATA_URI.equals(result)) {
saveToDataUriString(view);
}
} catch (final Throwable ex) {
Log.e(TAG, "Failed to capture view snapshot", ex);
promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Failed to capture view snapshot");
}
}
});
}
//endregion
//region Implementation
private void saveToTempFileOnDevice(@NonNull final View view) throws IOException {
final FileOutputStream fos = new FileOutputStream(output);
captureView(view, fos);
promise.resolve(Uri.fromFile(output).toString());
}
private void saveToRawFileOnDevice(@NonNull final View view) throws IOException {
final String uri = Uri.fromFile(output).toString();
final FileOutputStream fos = new FileOutputStream(output);
final ReusableByteArrayOutputStream os = new ReusableByteArrayOutputStream(outputBuffer);
final Point size = captureView(view, os);
// in case of buffer grow that will be a new array with bigger size
outputBuffer = os.innerBuffer();
final int length = os.size();
final String resolution = String.format(Locale.US, "%d:%d|", size.x, size.y);
fos.write(resolution.getBytes(Charset.forName("US-ASCII")));
fos.write(outputBuffer, 0, length);
fos.close();
promise.resolve(uri);
}
private void saveToDataUriString(@NonNull final View view) throws IOException {
final ReusableByteArrayOutputStream os = new ReusableByteArrayOutputStream(outputBuffer);
captureView(view, os);
outputBuffer = os.innerBuffer();
final int length = os.size();
final String data = Base64.encodeToString(outputBuffer, 0, length, Base64.NO_WRAP);
// correct the extension if JPG
final String imageFormat = "jpg".equals(extension) ? "jpeg" : extension;
promise.resolve("data:image/" + imageFormat + ";base64," + data);
}
private void saveToBase64String(@NonNull final View view) throws IOException {
final boolean isRaw = Formats.RAW == this.format;
final boolean isZippedBase64 = Results.ZIP_BASE_64.equals(this.result);
final ReusableByteArrayOutputStream os = new ReusableByteArrayOutputStream(outputBuffer);
final Point size = captureView(view, os);
// in case of buffer grow that will be a new array with bigger size
outputBuffer = os.innerBuffer();
final int length = os.size();
final String resolution = String.format(Locale.US, "%d:%d|", size.x, size.y);
final String header = (isRaw ? resolution : "");
final String data;
if (isZippedBase64) {
final Deflater deflater = new Deflater();
deflater.setInput(outputBuffer, 0, length);
deflater.finish();
final ReusableByteArrayOutputStream zipped = new ReusableByteArrayOutputStream(new byte[32]);
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int count = deflater.deflate(buffer); // returns the generated code... index
zipped.write(buffer, 0, count);
}
data = header + Base64.encodeToString(zipped.innerBuffer(), 0, zipped.size(), Base64.NO_WRAP);
} else {
data = header + Base64.encodeToString(outputBuffer, 0, length, Base64.NO_WRAP);
}
promise.resolve(data);
}
@NonNull
private List<View> getAllChildren(@NonNull final View v) {
if (!(v instanceof ViewGroup)) {
final ArrayList<View> viewArrayList = new ArrayList<>();
viewArrayList.add(v);
return viewArrayList;
}
final ArrayList<View> result = new ArrayList<>();
ViewGroup viewGroup = (ViewGroup) v;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
//Do not add any parents, just add child elements
result.addAll(getAllChildren(child));
}
return result;
}
/**
* Wrap {@link #captureViewImpl(View, OutputStream)} call and on end close output stream.
*/
private Point captureView(@NonNull final View view, @NonNull final OutputStream os) throws IOException {
try {
DebugViews.longDebug(TAG, DebugViews.logViewHierarchy(this.currentActivity));
return captureViewImpl(view, os);
} finally {
os.close();
}
}
/**
* Screenshot a view and return the captured bitmap.
*
* @param view the view to capture
* @return screenshot resolution, Width * Height
*/
private Point captureViewImpl(@NonNull final View view, @NonNull final OutputStream os) {
int w = view.getWidth();
int h = view.getHeight();
if (w <= 0 || h <= 0) {
throw new RuntimeException("Impossible to snapshot the view: view is invalid");
}
// evaluate real height
if (snapshotContentContainer) {
h = 0;
ScrollView scrollView = (ScrollView) view;
for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
}
}
final Point resolution = new Point(w, h);
Bitmap bitmap = getBitmapForScreenshot(w, h);
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
// Uncomment next line if you want to wait attached android studio debugger:
// Debug.waitForDebugger();
final Canvas c = new Canvas(bitmap);
view.draw(c);
//after view is drawn, go through children
final List<View> childrenList = getAllChildren(view);
for (final View child : childrenList) {
// skip any child that we don't know how to process
if (child instanceof TextureView) {
// skip all invisible to user child views
if (child.getVisibility() != VISIBLE) continue;
final TextureView tvChild = (TextureView) child;
tvChild.setOpaque(false); // <-- switch off background fill
// NOTE (olku): get re-usable bitmap. TextureView should use bitmaps with matching size,
// otherwise content of the TextureView will be scaled to provided bitmap dimensions
final Bitmap childBitmapBuffer = tvChild.getBitmap(getExactBitmapForScreenshot(child.getWidth(), child.getHeight()));
final int countCanvasSave = c.save();
applyTransformations(c, view, child);
// due to re-use of bitmaps for screenshot, we can get bitmap that is bigger in size than requested
c.drawBitmap(childBitmapBuffer, 0, 0, paint);
c.restoreToCount(countCanvasSave);
recycleBitmap(childBitmapBuffer);
} else if (child instanceof SurfaceView && handleGLSurfaceView) {
final SurfaceView svChild = (SurfaceView)child;
final CountDownLatch latch = new CountDownLatch(1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
final Bitmap childBitmapBuffer = getExactBitmapForScreenshot(child.getWidth(), child.getHeight());
try {
PixelCopy.request(svChild, childBitmapBuffer, new PixelCopy.OnPixelCopyFinishedListener() {
@Override
public void onPixelCopyFinished(int copyResult) {
final int countCanvasSave = c.save();
applyTransformations(c, view, child);
c.drawBitmap(childBitmapBuffer, 0, 0, paint);
c.restoreToCount(countCanvasSave);
recycleBitmap(childBitmapBuffer);
latch.countDown();
}
}, new Handler(Looper.getMainLooper()));
latch.await(SURFACE_VIEW_READ_PIXELS_TIMEOUT, TimeUnit.SECONDS);
} catch (Exception e) {
Log.e(TAG, "Cannot PixelCopy for " + svChild, e);
}
} else {
Bitmap cache = svChild.getDrawingCache();
if (cache != null) {
c.drawBitmap(svChild.getDrawingCache(), 0, 0, paint);
}
}
}
}
if (width != null && height != null && (width != w || height != h)) {
final Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
recycleBitmap(bitmap);
bitmap = scaledBitmap;
}
// special case, just save RAW ARGB array without any compression
if (Formats.RAW == this.format && os instanceof ReusableByteArrayOutputStream) {
final int total = w * h * ARGB_SIZE;
final ReusableByteArrayOutputStream rbaos = cast(os);
bitmap.copyPixelsToBuffer(rbaos.asBuffer(total));
rbaos.setSize(total);
} else {
final Bitmap.CompressFormat cf = Formats.mapping[this.format];
bitmap.compress(cf, (int) (100.0 * quality), os);
}
recycleBitmap(bitmap);
return resolution; // return image width and height
}
/**
* Concat all the transformation matrix's from parent to child.
*/
@NonNull
@SuppressWarnings("UnusedReturnValue")
private Matrix applyTransformations(final Canvas c, @NonNull final View root, @NonNull final View child) {
final Matrix transform = new Matrix();
final LinkedList<View> ms = new LinkedList<>();
// find all parents of the child view
View iterator = child;
do {
ms.add(iterator);
iterator = (View) iterator.getParent();
} while (iterator != root);
// apply transformations from parent --> child order
Collections.reverse(ms);
for (final View v : ms) {
c.save();
// apply each view transformations, so each child will be affected by them
final float dx = v.getLeft() + ((v != child) ? v.getPaddingLeft() : 0) + v.getTranslationX();
final float dy = v.getTop() + ((v != child) ? v.getPaddingTop() : 0) + v.getTranslationY();
c.translate(dx, dy);
c.rotate(v.getRotation(), v.getPivotX(), v.getPivotY());
c.scale(v.getScaleX(), v.getScaleY());
// compute the matrix just for any future use
transform.postTranslate(dx, dy);
transform.postRotate(v.getRotation(), v.getPivotX(), v.getPivotY());
transform.postScale(v.getScaleX(), v.getScaleY());
}
return transform;
}
@SuppressWarnings("unchecked")
private static <T extends A, A> T cast(final A instance) {
return (T) instance;
}
//endregion
//region Cache re-usable bitmaps
/**
* Synchronization guard.
*/
private static final Object guardBitmaps = new Object();
/**
* Reusable bitmaps for screenshots.
*/
private static final Set<Bitmap> weakBitmaps = Collections.newSetFromMap(new WeakHashMap<Bitmap, Boolean>());
/**
* Propose allocation size of the array output stream.
*/
private static int proposeSize(@NonNull final View view) {
final int w = view.getWidth();
final int h = view.getHeight();
return Math.min(w * h * ARGB_SIZE, 32);
}
/**
* Return bitmap to set of available.
*/
private static void recycleBitmap(@NonNull final Bitmap bitmap) {
synchronized (guardBitmaps) {
weakBitmaps.add(bitmap);
}
}
/**
* Try to find a bitmap for screenshot in reusable set and if not found create a new one.
*/
@NonNull
private static Bitmap getBitmapForScreenshot(final int width, final int height) {
synchronized (guardBitmaps) {
for (final Bitmap bmp : weakBitmaps) {
if (bmp.getWidth() == width && bmp.getHeight() == height) {
weakBitmaps.remove(bmp);
bmp.eraseColor(Color.TRANSPARENT);
return bmp;
}
}
}
return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
}
/**
* Try to find a bitmap with exact width and height for screenshot in reusable set and if
* not found create a new one.
*/
@NonNull
private static Bitmap getExactBitmapForScreenshot(final int width, final int height) {
synchronized (guardBitmaps) {
for (final Bitmap bmp : weakBitmaps) {
if (bmp.getWidth() == width && bmp.getHeight() == height) {
weakBitmaps.remove(bmp);
bmp.eraseColor(Color.TRANSPARENT);
return bmp;
}
}
}
return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
}
//endregion
//region Nested declarations
/**
* Stream that can re-use pre-allocated buffer.
*/
@SuppressWarnings("WeakerAccess")
public static class ReusableByteArrayOutputStream extends ByteArrayOutputStream {
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
public ReusableByteArrayOutputStream(@NonNull final byte[] buffer) {
super(0);
this.buf = buffer;
}
/**
* Get access to inner buffer without any memory copy operations.
*/
public byte[] innerBuffer() {
return this.buf;
}
@NonNull
public ByteBuffer asBuffer(final int size) {
if (this.buf.length < size) {
grow(size);
}
return ByteBuffer.wrap(this.buf);
}
public void setSize(final int size) {
this.count = size;
}
/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
protected void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = buf.length;
int newCapacity = oldCapacity << 1;
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
buf = Arrays.copyOf(buf, newCapacity);
}
protected static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
}
//endregion
}