Merge pull request #122 from PauloMelo/#121

Android takeScreenshot does not work properly.
This commit is contained in:
Lorenzo Pichilli 2019-10-24 12:25:02 +02:00 committed by GitHub
commit 996ec1e69d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 4 deletions

View File

@ -298,19 +298,27 @@ public class InAppWebView extends WebView {
} }
public byte[] takeScreenshot() { public byte[] takeScreenshot() {
Picture picture = capturePicture(); float scale = getScale();
int height = (int) (getContentHeight() * scale + 0.5);
Bitmap b = Bitmap.createBitmap( getWidth(), Bitmap b = Bitmap.createBitmap( getWidth(),
getHeight(), Bitmap.Config.ARGB_8888); height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b); Canvas c = new Canvas(b);
picture.draw(c); draw(c);
int scrollOffset = (getScrollY() + getMeasuredHeight() > b.getHeight())
? b.getHeight() : getScrollY();
Bitmap resized = Bitmap.createBitmap(
b, 0, scrollOffset, b.getWidth(), getMeasuredHeight());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); resized.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
try { try {
byteArrayOutputStream.close(); byteArrayOutputStream.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
resized.recycle();
return byteArrayOutputStream.toByteArray(); return byteArrayOutputStream.toByteArray();
} }