记录安卓开发截屏踩坑
  G2Dk7rY4MbER 2023年11月02日 20 0

通过一下方法无法截取到控件中带视频播放器的内容 截屏出来是白屏状态

// 开始截屏 private static byte[] screenshotView() { View view = getWindow().getDecorView(); // view.setDrawingCacheEnabled(true); // 设置缓存,可用于实时截图 Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); // view.setDrawingCacheEnabled(false); // 清空缓存,可用于实时截图 byte[] drawByte = getBitmapByte(bitmap); // 位图转为 Byte return drawByte; }

// 位图转 Base64 String private static String getBitmapString(Bitmap bitmap) { String result = null; ByteArrayOutputStream out = null; try { if (bitmap != null) { out = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);

out.flush();
  out.close();

  byte[] bitmapBytes = out.toByteArray();
  result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
}

} catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) { out.flush(); out.close(); } } catch (IOException e) { e.printStackTrace(); } } return result; }

// 位图转 Byte private static byte[] getBitmapByte(Bitmap bitmap){ ByteArrayOutputStream out = new ByteArrayOutputStream(); // 参数1转换类型,参数2压缩质量,参数3字节流资源 bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); try { out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } return out.toByteArray(); }


需要截屏到视频画面 就需要使用系统截屏方法MediaProjection()

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

上一篇: Openharmony的启动流程 下一篇: rust ios android
  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

G2Dk7rY4MbER
作者其他文章 更多