需求:对于无显示设备的android设备,如何获取到当前设备画面

条件:首先在android设备中搭建一个web服务器,可以参考基于前面介绍的ijetty服务器

实现:

方法1:

参考系统hide代码,android.view.Surface.java

 public boolean takeScreenShot(String imagePath){                  if(imagePath.equals("" )){                      imagePath = Environment.getExternalStorageDirectory()+File. separator+"Screenshot.png" ;             }                               Bitmap mScreenBitmap;          WindowManager mWindowManager;          DisplayMetrics mDisplayMetrics;          Display mDisplay;                            mWindowManager = (WindowManager) mcontext.getSystemService(Context.WINDOW_SERVICE);          mDisplay = mWindowManager.getDefaultDisplay();          mDisplayMetrics = new DisplayMetrics();          mDisplay.getRealMetrics(mDisplayMetrics);                                           float[] dims = {mDisplayMetrics.widthPixels , mDisplayMetrics.heightPixels };          mScreenBitmap = Surface. screenshot((int) dims[0], ( int) dims[1]);                               if (mScreenBitmap == null) {                   return false ;          }                         try {          FileOutputStream out = new FileOutputStream(imagePath);          mScreenBitmap.compress(Bitmap.CompressFormat. PNG, 100, out);                     } catch (Exception e) {                                          return false ;        }                                          return true ;}

我们要利用web服务器把图像传出来,可以拿到上面代码中的outputStream,然后拷贝到HttpServletResponse的输出流中。
这个screenshot函数还需要一些其他数据,例如display,metrics,dims等,我们参考Surface类中的实现,得出的可用类代码如下:

public class ScreenshotNative {    private Context mContext;    private WindowManager mWindowManager;    private Display mDisplay;    private DisplayMetrics mDisplayMetrics;    private Matrix mDisplayMatrix;    private Bitmap mScreenBitmap;    private static final Object sLock = new Object();    public ScreenshotNative(Context context) {        mContext = context;        mDisplayMatrix = new Matrix();        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);        mDisplay = mWindowManager.getDefaultDisplay();        mDisplayMetrics = new DisplayMetrics();        mDisplay.getRealMetrics(mDisplayMetrics);    }    private float getDegreesForRotation(int value) {        switch (value) {            case Surface.ROTATION_90:                return 360f - 90f;            case Surface.ROTATION_180:                return 360f - 180f;            case Surface.ROTATION_270:                return 360f - 270f;        }        return 0f;    }    public void takeScreenshot(OutputStream out) throws IOException {        // 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]);        }        Log.d("takeScreenshot", "takeScreenshot, dims, w-h: " + dims[0] + "-" + dims[1] + "; " +                "dm w-h: " + mDisplayMetrics.widthPixels + mDisplayMetrics.heightPixels +                "Thread=" + Thread.currentThread().getName());        // Take the screenshot        synchronized (sLock) {            mScreenBitmap = SurfaceControl.screenshot((int) dims[0], (int) dims[1]);            if (mScreenBitmap == null) {                throw new IOException("Bitmap is null after taking native screenshot!");            }            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();            mScreenBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);            Log.d("takeScreenshot","Thread=" + Thread.currentThread().getName());            mScreenBitmap.recycle();            // Clear any references to the bitmap            mScreenBitmap = null;        }    }}

使用方法如下:

    private void screenshotNative(HttpServletResponse response){        try {            ServletOutputStream outputStream = response.getOutputStream();            new ScreenshotNative(mContext).takeScreenshot(outputStream);            outputStream.flush();            outputStream.close();        } catch (IOException e) {            e.printStackTrace();        }    }

这样在GET请求就能拿到对应的流了 然后存在本地即可。

方法2:

使用screencap工具

public void takeScreenShot(){    String mSavedPath = "/sdcard/" + "screenshot.png" ;    try {                               Process p = Runtime. getRuntime().exec("screencap -p " + mSavedPath);           p.waitFor();    } catch (Exception e) {           e.printStackTrace();}

代码执行后,再把文件拷到servlet的输出流中就好了。screencap只支持png格式的存储。

以上两种方法都需要添加权限:

并使用system uid

更多相关文章

  1. [转]]Android 应用签名提权方法
  2. 【Android】android开发---实现屏幕旋转的两种方法
  3. Android-缓存数据保存-通用方法
  4. android闹钟――原代码
  5. Android 中 ListView Adapter getView 被多次调用问题 解决方法
  6. Android访问服务器出现W/System.err(9302): java.io.FileNotFoun
  7. Android监听来电和去电的实现方法
  8. Android 屏幕的旋转 onConfigurationChanged方法

随机推荐

  1. [转] How to detect incoming calls in a
  2. 2011.07.13——— android remote servic
  3. Android 常用工作命令mmm,mm,m,croot,cgr
  4. Android中shape 横线+竖线~虚线
  5. Android TextUtils类常用方法
  6. Android 9.0 System.loadLibrary 的源码
  7. Windows下编译使用Android(安卓)NDK,调用S
  8. Android 中屏蔽 Home 键
  9. 列出sdcard里所有.mp3文件,并且可以点击
  10. [Android][MediaRecorder] Android(安卓)