模仿豌豆荚以及某些刷机软件获取手机屏幕图片

来源:互联网 发布:vue.js 实战之组件篇 编辑:程序博客网 时间:2024/05/27 09:44

昨天花了一下午研究了下系统截屏,可行的方案有俩:

1.手机端:读取/dev/graphics/fb0文件,该文件存储了当前屏幕画面的数据,将此数据转换成图片存储在手机端即可。但美中不足的是需要获取系统root权限。

2.pc端:通过ddms连接手机,以IDevice的getScreenshot()方法获取手机屏幕信息。但缺点是手机需要开启adb调试。

目前应用市场中只有一款ScreenshotIt无需root便能使用,但使用之前经过繁琐的安装步骤,可能也是获取了手机系统root权限.

除此之外,相关软件都必须获取root权限才能截图。红米可以一键截图,但那是定制的rom自带的,PhoneWindowManager中调用系统截图(追到底是:Surface.screenshot(int,int)方法)即可轻松实现。


pc端实现功能相对容易,只需要一个java文件+ddmuilib.jar即可,代码如下:

import com.android.ddmlib.AdbCommandRejectedException;import com.android.ddmlib.AndroidDebugBridge;import com.android.ddmlib.IDevice;import com.android.ddmlib.RawImage;import com.android.ddmlib.TimeoutException;import java.awt.image.BufferedImage;import java.awt.image.RenderedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;public class DDMScreen {    /**     * @param args     */    public static void main(String[] args) {        IDevice device;        AndroidDebugBridge.init(false);        AndroidDebugBridge bridge = AndroidDebugBridge.createBridge();        waitDeviceList(bridge);        IDevice devices[] = bridge.getDevices();        device = devices[0];        try {            RawImage rawScreen = device.getScreenshot();            if (rawScreen != null) {                boolean landscape = false;                BufferedImage image = null;                int width2 = landscape ? rawScreen.height : rawScreen.width;                int height2 = landscape ? rawScreen.width : rawScreen.height;                image = new BufferedImage(width2, height2,                            BufferedImage.TYPE_INT_RGB);                int index = 0;                int indexInc = rawScreen.bpp >> 3;                for (int y = 0; y < rawScreen.height; y++) {                    for (int x = 0; x < rawScreen.width; x++, index += indexInc) {                        int value = rawScreen.getARGB(index);                        if (landscape)                            image.setRGB(y, rawScreen.width - x - 1, value);                        else                            image.setRGB(x, y, value);                    }                }                ImageIO.write((RenderedImage) image, "PNG", new File("C:\\Users\\sjyBing\\Desktop\\123.png"));            }        } catch (TimeoutException e) {            e.printStackTrace();        } catch (AdbCommandRejectedException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }    private static void waitDeviceList(AndroidDebugBridge bridge) {        int count = 0;        while (bridge.hasInitialDeviceList() == false) {            try {                Thread.sleep(100); // 如果没有获得设备列表,则等待                count++;            } catch (InterruptedException e) {            }            if (count > 300) {                // 设定时间超过300×100 ms的时候为连接超时                System.err.print("Time out");                break;            }        }    }}


还有更好的方法需要再努力发掘。


PC端获取手机屏幕图片代码下载地址(包含jar包):http://download.csdn.net/detail/singleton1900/6543671


原创粉丝点击