Android 手机截屏

来源:互联网 发布:echart数组 编辑:程序博客网 时间:2024/04/28 05:13

Android截屏

最近由于公司项目需要实现手机的截屏,在实现过程中真的可以说是历经千辛万苦,各种问题层出不穷。现写这篇博客作为见证。

通过上网搜索截屏主要有如下几种方式

使用View.getDrawingCache()方式

通过该方法可以获取到当前activity的页面的bitmap,然后进行保存,可以说是最简单的实习方式。优点是不需要root,不过缺点也比较明显只能获取当前运行的activity,无法获取其他应用,也不能用到service后台截屏。

View view = activity.getWindow().getDecorView();view.setDrawingCacheEnabled(true);view.buildDrawingCache();Bitmap b1 = view.getDrawingCache();//获取状态栏高度Rect frame = new Rect();activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);int statusBarHeight = frame.top;System.out.println(statusBarHeight);//获取屏幕长和高int width = activity.getWindowManager().getDefaultDisplay().getWidth();int height = activity.getWindowManager().getDefaultDisplay().getHeight();//去掉标题栏Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);view.destroyDrawingCache();

bufferframe读取fb0

在手机的/dev/graphics目录下的fb0文件是负责屏幕渲染的帧缓存,网上有一些教程讲如何用c将手机中的fb0转换成bmp格式的图片,但是并不死所有的手机都支持,还与Android版本有关,而且手机必须要有root权限。

参考

android上用C语言读取fb0实现截屏,并保存为rgb565的bmp

一个完整的android framebuffer截图程序 v0.3

因为对c与c++不是很了解,我尝试着做了,但任然会有很多的问题,我也试过用java代码读取fb0,保存bitmap图片,但都失败了,保存的图片全部都是黑色的,真的是很悲伤。

反射

通过查看Android的源码,sdk是有截屏的代码,但是隐藏的,无法调用。在frameworks/base/packages/SystemUI/src/com/android/systemui/screenshot下面有两个文件,GlobalScreenshot与TakeScreenshotService。

我们主要查看GlobalScreenshot找到takeScreenshot方法

 void takeScreenshot(Runnable finisher, boolean statusBarVisible, boolean navBarVisible) {         // We need to orient the screenshot correctly (and the Surface api seems to take screenshots         // only in the natural orientation of the device :!)         mDisplay.getRealMetrics(mDisplayMetrics);         float[] dims = {mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels};         float degrees = getDegreesForRotation(mDisplay.getRotation());         boolean requiresRotation = (degrees > 0);         if (requiresRotation) {             // Get the dimensions of the device in its native orientation             mDisplayMatrix.reset();             mDisplayMatrix.preRotate(-degrees);             mDisplayMatrix.mapPoints(dims);             dims[0] = Math.abs(dims[0]);             dims[1] = Math.abs(dims[1]);         }         // Take the screenshot         mScreenBitmap = SurfaceControl.screenshot((int) dims[0], (int) dims[1]);         if (mScreenBitmap == null) {             notifyScreenshotError(mContext, mNotificationManager);             finisher.run();             return;         }         if (requiresRotation) {             // Rotate the screenshot to the current orientation             Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels,                     mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888);             Canvas c = new Canvas(ss);             c.translate(ss.getWidth() / 2, ss.getHeight() / 2);             c.rotate(degrees);             c.translate(-dims[0] / 2, -dims[1] / 2);             c.drawBitmap(mScreenBitmap, 0, 0, null);             c.setBitmap(null);             // Recycle the previous bitmap             mScreenBitmap.recycle();             mScreenBitmap = ss;         }         // Optimizations         mScreenBitmap.setHasAlpha(false);         mScreenBitmap.prepareToDraw();         // Start the post-screenshot animation         startAnimation(finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,                 statusBarVisible, navBarVisible);     }  

关键是

mScreenBitmap = SurfaceControl.screenshot((int) dims[0], (int) dims[1]);  

这个就是截屏的主要代码,这个地方在不同的Android版本中有一些差别
有Surface与SurfaceControl。

因此我们通过反射机制的代码

wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);                mDisplay = wm.getDefaultDisplay();                mDisplayMatrix = new Matrix();                mDisplayMetrics = new DisplayMetrics();                // We need to orient the screenshot correctly (and the Surface api seems to take screenshots                // only in the natural orientation of the device :!)                mDisplay.getRealMetrics(mDisplayMetrics);                float[] dims =                {                        mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels                };                float degrees = getDegreesForRotation(mDisplay.getRotation());                boolean requiresRotation = (degrees > 0);                if (requiresRotation)                {                    // Get the dimensions of the device in its native orientation                    mDisplayMatrix.reset();                    mDisplayMatrix.preRotate(-degrees);                    mDisplayMatrix.mapPoints(dims);                    dims[0] = Math.abs(dims[0]);                    dims[1] = Math.abs(dims[1]);                }                Bitmap mScreenBitmap = screenShot((int) dims[0], (int) dims[1]);                if (requiresRotation)                {                    // Rotate the screenshot to the current orientation                    Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,                            Bitmap.Config.ARGB_8888);                    Canvas c = new Canvas(ss);                    c.translate(ss.getWidth() / 2, ss.getHeight() / 2);                    c.rotate(degrees);                    c.translate(-dims[0] / 2, -dims[1] / 2);                    c.drawBitmap(mScreenBitmap, 0, 0, null);                    c.setBitmap(null);                    mScreenBitmap = ss;                    if (ss != null && !ss.isRecycled())                    {                        ss.recycle();                    }                }                // If we couldn't take the screenshot, notify the user                if (mScreenBitmap == null)                {                    Toast.makeText(context, "screen shot fail", Toast.LENGTH_SHORT).show();                }                // Optimizations                mScreenBitmap.setHasAlpha(false);                mScreenBitmap.prepareToDraw();
private Bitmap screenShot(int width, int height)    {        Log.i(TAG, "android.os.Build.VERSION.SDK : " + android.os.Build.VERSION.SDK_INT);        Class<?> surfaceClass = null;        Method method = null;        try        {            Log.i(TAG, "width : " + width);            Log.i(TAG, "height : " + height);            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2)            {                surfaceClass = Class.forName("android.view.SurfaceControl");            }            else            {                surfaceClass = Class.forName("android.view.Surface");            }            method = surfaceClass.getDeclaredMethod(METHOD_NAME, int.class, int.class);            method.setAccessible(true);            return (Bitmap) method.invoke(null, width, height);        }        catch (NoSuchMethodException e)        {            Log.e(TAG, e.toString());        }        catch (IllegalArgumentException e)        {            Log.e(TAG, e.toString());        }        catch (IllegalAccessException e)        {            Log.e(TAG, e.toString());        }        catch (InvocationTargetException e)        {            Log.e(TAG, e.toString());        }        catch (ClassNotFoundException e)        {            Log.e(TAG, e.toString());        }        return null;    }

遗憾的是通过测试发现mScreenBitmap是null,也就是获取截屏失败了,原因可能是因为该SurfaceControl或Surface是隐藏的,虽然编译没报错,但是截屏会失败。

重新编译Android的sdk

这个是简单又粗暴的方法,我们可以将SurfaceControl或Surface类的隐藏去掉,生成我们自己的sdk。这是一个漫长的过程,可能会出现各种错误,我自己没有尝试。

通过adb命令

截屏命令主要有两个adb shell screencap -p xxx.png 或 adb shell screenshot xxx.png

screencap是从Android 2.3开始提供的一个系统级的截图工具,通过源码可以了解到screencap的实现方式,默认会从底层UI Surface去获取屏幕截图,如果失败则从linux kernel层的display framebuffer(/dev/graphics/fb0)去获取屏幕截图。

screenshot是从Android 4.0开始提供的另一个截图的工具, 通过源码可以发现screenshot则是直接读取/dev/graphics/fb0去获取屏幕的图像数据。

Process process = Runtime.getRuntime().exec("/system/bin/screencap -p "+ fileFullPath)
Process process = Runtime.getRuntime().exec("/system/bin/screencap "+ fileFullPath)

该adb命令是需要root权限的,因此手机没有权限截屏也是不能成功的。

最后


由于公司项目是盒子开发,所以说天生有root权限,因此实现截屏功能来说还是较为容易的,经历了从自带–>bufferframe–>adb 的过程。

感谢

android4.3 截屏功能的尝试与失败分析

AndroidScreenShotService

Android中使用代码截图的各种方法总结

android上用C语言读取fb0实现截屏,并保存为rgb565的bmp

一个完整的android framebuffer截图程序 v0.3

0 0
原创粉丝点击