android 截图,网络发送,只需1.8秒

本文介绍了如何在Android系统中实现OSD层和Video层的截图,并提供了详细的编码原理分析,包括通过反射调用SurfaceControl类的方法进行截图。此外,还提及了截图可能存在的倒置问题及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【功能说明】

此功能,不但可以截安卓OSD层,还可以截取Video层。代码可以直接用,不懂得可以留言,谢谢!


【编码前的原理分析】

1.通过类名获取类:

Class SurfaceControl= Class.forName("android.view.SurfaceControl");

参数1:类的包名


2.获取方法:

Method method = SurfaceControl.getMethod("screenshot", Integer.class);   

参数1:类的方法

参数2:参数列表 -- 参数是按声明顺序标识,该方法形参类型的Class 对象的一个数组。

    如果 parameterTypes 为 null,则按空数组处理。


3.调用方法:

method.invoke(SurfaceControl, 1000,1000);

参数1:类实例对象

参数2:方法参数1

参数3:方法参数2

(注意:invoke的返回值是class,那么,根据自己所调用的方法的返回值的类型,对class返回值进行强制类型转换一下,就可以了!

例如:object obj =Method.Invoke(..)方法得到返回值是object,如何实现转换呢? ---- (string[])obj 


【本人项目代码】

1.截图工具类

package com.harison.terminalMonitoring;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
public class ScreentShotUtil{
	private static final String TAG = "ScreentShotUtil";
	private static final String CLASS1_NAME = "android.view.SurfaceControl";
	private static final String CLASS2_NAME = "android.view.Surface";
	private static final String METHOD_NAME = "screenshot";
	private static ScreentShotUtil instance;
	private Display mDisplay;
	private DisplayMetrics mDisplayMetrics;
	private Matrix mDisplayMatrix;
	private WindowManager wm;
	private SimpleDateFormat format;
	private ScreentShotUtil()
	{

	}

	public static ScreentShotUtil getInstance()
	{
		synchronized (ScreentShotUtil.class)
		{
			if (instance == null)
			{
				instance = new ScreentShotUtil();
			}
		}
		return instance;
	}

	public Bitmap screenShot(int width, int height){
		
		Method method = null;
		try
		{
			if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2){
				
				surfaceClass = Class.forName(CLASS1_NAME);
			}
			else{
				surfaceClass = Class.forName(CLASS2_NAME);
			}
			surfaceClass = Class.forName(CLASS1_NAME);
			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;
	}

}
2.代码的调用

   Bitmap bmp = ScreentShotUtil.getInstance().screenShot(500, 500);   //这个在不影响显示的情况下,尽量小  ---- 可以节省时间
3.Bitmap 转 byte[],进行网络发送数据准备  --- 因为本人网络通讯,数据都是转换为   byte[]

public static byte[] Bitmap2Bytes(Bitmap bm) {
		      ByteArrayOutputStream baos = new ByteArrayOutputStream();
		      bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		      return baos.toByteArray();
     }

【温馨提示】

有时候,截取的图片,可能是倒的,需要进行旋转,这里贴出相对应的接口。

public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
	    Bitmap returnBm = null;
	    // 根据旋转角度,生成旋转矩阵
	    Matrix matrix = new Matrix();
	    matrix.postRotate(degree);
	    try {
	        // 将原始图片按照旋转矩阵进行旋转,并得到新的图片
	        returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
	    } catch (OutOfMemoryError e) {
	    }
	    if (returnBm == null) {
	        returnBm = bm;
	    }
	    if (bm != returnBm) {
	        bm.recycle();
	    }
	    return returnBm;
	}










评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值