欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > Android 原生层SurfaceView截屏

Android 原生层SurfaceView截屏

2025/2/21 3:15:33 来源:https://blog.csdn.net/qq_38779672/article/details/145618685  浏览:    关键词:Android 原生层SurfaceView截屏

背景:flutter嵌入原生view时,原生view使用的surfaveview,导致下面两种方法无法正常使用。

  1. 导致flutter无法通过id找到RenderRepaintBoundarytoImage来抓取widget
  2. 原生层无法通过view去获取Bitmap

方案:使用PixelCopy方法抓取屏幕像素数据,并且复制到Bitmap

import android.os.Looper;
import android.graphics.Bitmap;
import android.view.PixelCopy;
import android.view.SurfaceView;
import java.io.ByteArrayOutputStream;// surfaceView 为所需要截图的组件private void getSurfaceViewBitmap() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {int width = surfaceView.getWidth();int height = surfaceView.getHeight();final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);PixelCopy.request(surfaceView, bitmap, copyResult -> {if (copyResult == PixelCopy.SUCCESS) {ByteArrayOutputStream stream = new ByteArrayOutputStream();// 此处为了flutter层使用,实际纯原生可以不做这个转换bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);byte[] imageInByte = stream.toByteArray();HashMap<String, Object> map = new HashMap<>();map.put("bytes", imageInByte);Log.d("jingluo", "success")} else {Log.d("jingluo", "failed")}}, new Handler(Looper.getMainLooper()));}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词