关于安卓SurfaceView截屏

2015.12.9号是一个值得记得日子,开始拖着背包从学校出来,挤上火车,做着接近30小时的硬座,来到了美丽的成都,那时候赵雷的<成都>还没火,‘走到玉林路的尽头 坐在小酒馆的门口 ‘,多么诗情画意的生活啊。来不及享受这安逸之都的生活,便匆匆的开始实习生涯。
时至今日,也已经毕业大半年了,也从实习生正式成了职场人。身边大神很多,公司也是藏龙卧虎,也感觉应该养成写作的习惯,一来可以总结一下,以便之后自己查看。同时,因为写作也会去查很多资料,也可以加深对问题的认识。最后,只是也是一种分享的过程,网上东西良莠不齐,能够对一些自己熟悉的领域写好,帮助别人也是一种快乐吧。总不至于自己每天在各种bug中享受暗无天日的生活吧,一度开始怀疑人生。找点有意义的事情做,总是好的。
下面来谈谈SurfaceView截屏的问题,SufaceView视图原理我就不再赘述,网上一搜大把,下面直接将贴代码吧。
1.root情况下,读取Framebuff上面的像素,通过像素得到Bitmap,再将Bitmap以图片形式输出。

private static final String DEVICE_NAME = "/dev/graphics/fb0";
@SuppressWarnings("deprecation")public static Bitmap acquireScreenshot(Context context) {WindowManager mWinManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);DisplayMetrics metrics = new DisplayMetrics();Display display = mWinManager.getDefaultDisplay();display.getMetrics(metrics);// 屏幕高int height = metrics.heightPixels;// 屏幕的宽int width = metrics.widthPixels;int pixelformat = display.getPixelFormat();PixelFormat localPixelFormat1 = new PixelFormat();PixelFormat.getPixelFormatInfo(pixelformat, localPixelFormat1);// 位深int deepth = localPixelFormat1.bytesPerPixel;byte[] arrayOfByte = new byte[height * width * deepth];try {// 读取设备缓存,获取屏幕图像流InputStream localInputStream = readAsRoot();DataInputStream localDataInputStream = new DataInputStream(localInputStream);localDataInputStream.readFully(arrayOfByte);localInputStream.close();int[] tmpColor = new int[width * height];int r, g, b;for (int j = 0; j < width * height * deepth; j += deepth) {b = arrayOfByte[j] & 0xff;g = arrayOfByte[j + 1] & 0xff;r = arrayOfByte[j + 2] & 0xff;tmpColor[j / deepth] = (r << 16) | (g << 8) | b | (0xff000000);}// 构建bitmapBitmap scrBitmap = Bitmap.createBitmap(tmpColor, width, height,Bitmap.Config.ARGB_8888);return scrBitmap;} catch (Exception e) {Log.d(TAG, "#### 读取屏幕截图失败");e.printStackTrace();}return null;}
/*** @Title: readAsRoot* @Description: 以root权限读取屏幕截图* @throws Exception* @throws*/public static InputStream readAsRoot() throws Exception {File deviceFile = new File(DEVICE_NAME);Process localProcess = Runtime.getRuntime().exec("su");String str = "cat " + deviceFile.getAbsolutePath() + "\n";localProcess.getOutputStream().write(str.getBytes());return localProcess.getInputStream();}//将读取的bitmap输出图片到SD卡上
public static void shoot(Activity activity, File filePath) {if (filePath == null) {return;}if (!filePath.getParentFile().exists()) {filePath.getParentFile().mkdirs();}FileOutputStream fos = null;try {fos = new FileOutputStream(filePath);if (null != fos) {takeScreenShot(activity).compress(Bitmap.CompressFormat.PNG, 100, fos);}} catch (FileNotFoundException e) {e.printStackTrace();} finally {if (fos != null) {try {fos.flush();fos.close();} catch (IOException e) {e.printStackTrace();}}}}

2.非root 直接在SurfaceView中实时输出bitmap,然后将bitmap输出成图片保存下来。

    /*** 将实时bitmap输出到指定sd卡。*/public void saveScreenshot() {if (ensureSDCardAccess()) {Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);doDraw(1, canvas);File file = new File(mScreenshotPath + "/" + System.currentTimeMillis() + ".jpg");FileOutputStream fos;try {fos = new FileOutputStream(file);bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);fos.close();} catch (FileNotFoundException e) {Log.e("xxx", "FileNotFoundException", e);} catch (IOException e) {Log.e("xxx", "IOEception", e);}}}/*** * 指定路径文件是否存在*/private boolean ensureSDCardAccess() {File file = new File(mScreenshotPath);if (file.exists()) {return true;} else if (file.mkdirs()) {return true;}return false;}

二.附上普通view截图方式。

public class ScreenShot {public static void shoot(Activity activity, File filePath) {if (filePath == null) {return;}if (!filePath.getParentFile().exists()) {filePath.getParentFile().mkdirs();}FileOutputStream fos = null;try {fos = new FileOutputStream(filePath);if (null != fos) {takeScreenShot(activity).compress(Bitmap.CompressFormat.PNG, 100, fos);}} catch (FileNotFoundException e) {e.printStackTrace();} finally {if (fos != null) {try {fos.flush();fos.close();} catch (IOException e) {e.printStackTrace();}}}}@SuppressWarnings("deprecation")private static Bitmap takeScreenShot(Activity activity) {View view = activity.getWindow().getDecorView();view.setDrawingCacheEnabled(true);view.buildDrawingCache();Bitmap bitmap = view.getDrawingCache();Rect frame = new Rect();activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);int statusBarHeight = frame.top;int width = activity.getWindowManager().getDefaultDisplay().getWidth();int height = activity.getWindowManager().getDefaultDisplay().getHeight();// Bitmap b = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width,height - statusBarHeight);view.destroyDrawingCache();return b;}}

安卓SurfaceView截屏相关推荐

  1. 安卓java录屏_安卓实现截屏以及录屏功能Demo

    [实例简介]安卓实现截屏以及录屏功能Demo 安卓实现截屏以及录屏功能Demo [实例截图] [核心代码] package com.dzjin.screen.screenshotandrecordde ...

  2. 酷派android手机怎么截屏,酷派手机怎么截屏啊第一页:安卓怎么截屏,截图在哪?安卓手机截...

    酷派手机怎么截屏啊 第一页:安卓怎么截屏,截图在哪?安卓手机截图快捷键功能详解第二页:三星手机怎么截图_三星手机截图快捷键组合第三页:华为手机怎么截图_华为截图方法2第四页:Htcone(M7)_Ht ...

  3. uni-app 实现安卓防截屏(整个应用或某个页面)

    uni-app 实现安卓防截屏 如果想要在App中全局禁止截屏,那么可以在App.vue中调用. 如果想要某个页面防截屏就在某个页面调用,但是在离开这个页面的时候要恢复截屏,否则全局还是禁止截屏的状态 ...

  4. SurfaceView 截屏方案

    1.PixelCopy这个类是安卓提供的截取SurfaceView 的一个类: val surfaceView: SurfaceView = binding.realTimeMap.getBindin ...

  5. uni-app安卓禁止截屏,一行代码

    let osname = plus.os.name console.log(osname) if (osname == 'Android') {//禁止截屏var activity = plus.an ...

  6. 安卓adb截屏java_Android 截屏的各种骚操作

    本文公众号「AndroidTraveler」首发. 背景 在实际的应用场景中,Android 手机的截屏其实是很普遍的. 比如说 PPT 演示,比如说技术博客图文并茂讲解. 因此懂得 Android ...

  7. uniapp 安卓 ios 截屏 保存到相册

    //有一部分是借鉴saveImageToPhotos() {// 当前当前设备是ios还是安卓let type = uni.getSystemInfoSync().platform;let heigh ...

  8. android如何截屏快捷键是什么手机,安卓截屏快捷键是什么?截屏方法总结 - Android教程 - 安卓中文网...

    现如今,使用安卓手机的机友们是越来越多,但是对于新手们来说,大家对安卓手机又有多了解呢?相信大家在使用手机的过程中一定有过这样的经历,经常聊QQ以及一些其他的聊天工具,有时微博想截个屏与大家分享都无从 ...

  9. 安卓手机如何截屏呢?

    安卓手机截屏是指将手机当前的页面截取下来并保存为图片的功能,截屏的方法有以下几种: 1.通过快捷键截屏:同时按住Home键+电源键(部分早期机器需要同时按住返回键和Home键). 2.助理菜单截屏:设 ...

最新文章

  1. [Warning] TIMESTAMP with implicit DEFAULT value is
  2. opencv-python 使用掩模抠图
  3. 除了Postman之外,居然还有个Postwoman...
  4. postman设置带token的请求
  5. SAP Spartacus scss里的--cx-color-primary
  6. Java 非阻塞 IO 和异步 IO
  7. 026_lsof命令经验总结
  8. 基于短文本的食源性疾病事件探测技术
  9. python小练习—名片管理系统(增、删、改、查、数据本地保存)
  10. redhat kvm 虚拟机U盘不识别的解决办法
  11. Tomcat 映射虚拟目录
  12. 如何在window电脑和Ipad之间互传文件之优雅的解放Ipad生产力
  13. element表格样式优化
  14. React pdf 电子书
  15. 谷歌seo快速排名优化方法
  16. window10 更新提示 0x80073712错误
  17. 效用最大化问题中的三个函数——需求函数、间接效用函数、支出函数
  18. 电路板中的常见电子元器件种类汇总
  19. 2021年新手做seo怎么做,几大绝招快速上排名收录
  20. 使用Windows自带“录音机”录制音乐(转)

热门文章

  1. 电子电器架构开发-功能分配开发内容
  2. 在线算法与竞争度分析
  3. Android 模拟器屏幕定制(修改控制器大小)
  4. multism中ui和uo应该怎么表示_如何求该运算放大电路uo和ui的关系表达式?
  5. vs2010 按运行时不自动编译已经改变的直接运行先前的项目
  6. 微信小程序-屏幕高度分析详解
  7. TELNET 链接收藏
  8. iPhone,iPad横屏竖屏的判断
  9. 微信短链接服务器,微信短网址w.url.cn 接口api开发文档
  10. AI | 浅谈AI技术以及其今后发展