Android 手机截屏

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

### 回答1: 用Kotlin语言来编写Android手机截屏代码,可以参考以下代码: val bitmap = getScreenShot() val imagePath = saveBitmap(bitmap) fun getScreenShot(): Bitmap { val view = getWindow().getDecorView() view.isDrawingCacheEnabled = true val bitmap = Bitmap.createBitmap(view.getDrawingCache()) view.isDrawingCacheEnabled = false return bitmap } fun saveBitmap(bitmap: Bitmap): String { val fileName = "${System.currentTimeMillis()}.png" val imagePath = getExternalFilesDir(Environment.DIRECTORY_PICTURES).toString() + File.separator + fileName val file = File(imagePath) val outputStream = FileOutputStream(file) bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream) outputStream.flush() outputStream.close() return imagePath } ### 回答2: 在Kotlin中编写Android手机截屏代码可以使用`MediaProjectionManager`和`ImageReader`来实现。以下是一个简单的示例代码: ```kotlin // 在Activity中创建MediaProjectionManager和ImageReader的实例 val mediaProjectionManager = getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager val imageReader = ImageReader.newInstance(screenWidth, screenHeight, PixelFormat.RGBA_8888, 2) // 请求屏幕捕获权限 startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), REQUEST_CODE) // 在onActivityResult中获取屏幕捕获权限 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) { val mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data) // 创建一个VirtualDisplay来捕获屏幕内容 val virtualDisplay = mediaProjection.createVirtualDisplay( "ScreenCapture", screenWidth, screenHeight, resources.displayMetrics.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, imageReader.surface, null, null ) // 创建一个ImageReader.OnImageAvailableListener来获取截屏图像 imageReader.setOnImageAvailableListener({ reader -> val image = reader.acquireLatestImage() // 处理截屏图像 // ... image?.close() }, null) // 停止屏幕捕获 virtualDisplay.release() mediaProjection.stop() } } ``` 上述代码监听了`ImageReader`的`setOnImageAvailableListener`,当有新的图像可用时会调用该方法,并在其中获取并处理截屏图像。需要注意的是截屏图像的处理在主线程中进行,如果图像处理较为耗时,建议将处理逻辑放在异步线程中执行。 ### 回答3: 在Kotlin中编写Android手机截屏代码可以通过以下步骤: 1. 首先,在AndroidManifest.xml文件中添加必要的权限: ``` <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 2. 创建一个名为ScreenCaptureUtil的辅助类,并添加一个名为captureScreen的静态方法: ```kotlin object ScreenCaptureUtil { @JvmStatic fun captureScreen(activity: Activity): Bitmap? { // 获取屏幕视图 val view = activity.window.decorView view.isDrawingCacheEnabled = true // 创建Bitmap并从视图绘制上面 val screenshot = Bitmap.createBitmap(view.drawingCache) view.isDrawingCacheEnabled = false return screenshot } } ``` 3. 在需要截屏的Activity中调用ScreenCaptureUtil的captureScreen方法: ```kotlin val screenshot = ScreenCaptureUtil.captureScreen(this) if (screenshot != null) { // 将截图写入文件存储 val file = File(Environment.getExternalStorageDirectory().toString() + "/screenshot.jpg") try { val fos = FileOutputStream(file) screenshot.compress(Bitmap.CompressFormat.JPEG, 100, fos) fos.flush() fos.close() // 在本地保存截图 MediaStore.Images.Media.insertImage(contentResolver, file.absolutePath, "screenshot", "screenshot") } catch (e: Exception) { e.printStackTrace() } } ``` 上述代码首先获取当前Activity的视图对象,开启视图的绘图缓存,并将视图绘制到一个新的Bitmap对象中。然后,将Bitmap对象保存为JPEG格式的文件,并将其插入到系统的媒体库中,以便在图库或其他应用程序中查看截图。 请注意,为了确保应用程序具有写入外部存储的权限,还需要在Manifest文件中添加相应的权限。+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值