網(wǎng)站首頁 編程語言 正文
1. 第一步:調(diào)起系統(tǒng)捕獲屏幕的Intent
MainActivity:
public void goCaptureIntent() {
//第一步.調(diào)起系統(tǒng)捕獲屏幕的Intent
mMediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
Intent captureIntent = mMediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(captureIntent, SCREEN_SHOT_CODE);
}
2. 第二步:通過startForegroundService來獲取mediaProjection
注:sdk 28以及以下可以直接在Activity中獲取,29以及以上在Activity中獲取會(huì)報(bào)錯(cuò)
MainActivity:
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==SCREEN_SHOT_CODE && resultCode == RESULT_OK){
//第二步通過startForegroundService來獲取mediaProjection
Intent service = new Intent(this, ScreenShootService.class);
service.putExtra("code", resultCode);
service.putExtra("data", data);
service.putExtra("width", getScreenWidth(this));
service.putExtra("height", getScreenHeight(this));
startForegroundService(service);
}
}
3.?第三步:獲取mediaProjection
注:AndroidManifest.xml中注冊的service要加上以下屬性
?第三步的代碼:
ScreenShootService:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int resultCode = intent.getIntExtra("code", -1);
Intent data = intent.getParcelableExtra("data");
width = intent.getIntExtra("width",1080);
height = intent.getIntExtra("height",1920);
notification();
//第三步:獲取mediaProjection
MediaProjectionManager mMediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
MediaProjection mediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data);
if (mediaProjection == null) {
Log.e(TAG, "media projection is null");
}
Bitmap bitmap = screenShot(mediaProjection);
EventBus.getDefault().post(bitmap);
return super.onStartCommand(intent, flags, startId);
}
?4.?第四步:通過mediaProjection截圖獲取Image
ScreenShootService:
//第四步;通過mediaProjection截圖獲取Image
public Bitmap screenShot(MediaProjection mediaProjection){
Objects.requireNonNull(mediaProjection);
@SuppressLint("WrongConstant")
ImageReader imageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 60);
VirtualDisplay virtualDisplay = mediaProjection.createVirtualDisplay("screen", width, height, 1, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,imageReader.getSurface(), null, null);
//取現(xiàn)在最新的圖片
SystemClock.sleep(1000);
//取最新的圖片
Image image = imageReader.acquireLatestImage();
// Image image = imageReader.acquireNextImage();
//釋放 virtualDisplay,不釋放會(huì)報(bào)錯(cuò)
virtualDisplay.release();
return image2Bitmap(image);
}
5. 第五步:將Image轉(zhuǎn)為Bitmap
ScreenShootService:
//第五步:將Image轉(zhuǎn)為Bitmap
public static Bitmap image2Bitmap(Image image) {
if (image == null) {
System.out.println("image 為空");
return null;
}
int width = image.getWidth();
int height = image.getHeight();
final Image.Plane[] planes = image.getPlanes();
final ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * width;
Bitmap bitmap = Bitmap.createBitmap(width+ rowPadding / pixelStride , height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
//截取圖片
// Bitmap cutBitmap = Bitmap.createBitmap(bitmap,0,0,width/2,height/2);
//壓縮圖片
// Matrix matrix = new Matrix();
// matrix.setScale(0.5F, 0.5F);
// System.out.println(bitmap.isMutable());
// bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
image.close();
return bitmap;
}
6.?第六步:展示截圖結(jié)果
這里是通過EventBust從Service傳到了Activity
MainActivity:
//接收service返回的bitmap圖片數(shù)據(jù)
@Subscribe(threadMode = ThreadMode.MAIN)//在ui線程執(zhí)行
public void onMoonEvent(Bitmap bitmap){
//第六步:展示截圖結(jié)果
imgShowImage.setBackground(new BitmapDrawable(bitmap));
}
異常處理1.?E/BufferQueueProducer: [ImageReader-2340x1080f1m1-24598-0](id:601600000001,api:1,p:1080,c:24598) dequeueBuffer: BufferQueue has been abandoned
分析:異常:BufferQueue已經(jīng)被丟棄了,還在使用
說明有在可能是截圖中使用到的某個(gè)對(duì)象被GC了,BufferQueue的對(duì)象大概有三個(gè)ImageReader 、VirtualDisplay 和 Image,Image在截圖萬之后已經(jīng)close掉了,然后嘗試截圖后分別對(duì)ImageReader和VirtualDisplay進(jìn)行釋放,ImageReader釋放問題依然存在,VirtualDisplay釋放解決了該問題。
VirtualDisplay virtualDisplay = mediaProjection.createVirtualDisplay("screen", width, height, 1, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,imageReader.getSurface(), null, null);
...
使用完Image之后...
...
virtualDisplay.release();
?異常處理2:Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
sdk版本產(chǎn)生的適配問題
兩種解決方案:
方案一:將sdk降低到28或者以下
?方案二:即需要使用 startForegroundService
也就是上面demo中使用到的方法
Activity:
public void goCaptureIntent() {
//第一步.調(diào)起系統(tǒng)捕獲屏幕的Intent
mMediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
Intent captureIntent = mMediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(captureIntent, SCREEN_SHOT_CODE);
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==SCREEN_SHOT_CODE && resultCode == RESULT_OK){
//第二步通過startForegroundService來獲取mediaProjection
Intent service = new Intent(this, ScreenShootService.class);
service.putExtra("code", resultCode);
service.putExtra("data", data);
service.putExtra("width", getScreenWidth(this));
service.putExtra("height", getScreenHeight(this));
startForegroundService(service);
}
}
Service:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int resultCode = intent.getIntExtra("code", -1);
Intent data = intent.getParcelableExtra("data");
width = intent.getIntExtra("width",1080);
height = intent.getIntExtra("height",1920);
notification();
//第二步:創(chuàng)建mediaProjection
MediaProjectionManager mMediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
MediaProjection mediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data);
if (mediaProjection == null) {
Log.e(TAG, "media projection is null");
}
Bitmap bitmap = screenShot(mediaProjection);
EventBus.getDefault().post(bitmap);
return super.onStartCommand(intent, flags, startId);
}
AndroidManifest.xml:
?最終效果圖:
需要源碼的可以點(diǎn)個(gè)贊,點(diǎn)關(guān)注,然后評(píng)論里留下郵箱
原文鏈接:https://blog.csdn.net/qq_37980878/article/details/124698652
相關(guān)推薦
- 2023-02-10 docker的鏡像存放地址在哪里_docker
- 2022-05-31 k8s部署Ingress并創(chuàng)建規(guī)則的詳細(xì)介紹_云其它
- 2022-07-15 服務(wù)器間如何實(shí)現(xiàn)文件共享_服務(wù)器其它
- 2022-09-19 Android基于OkHttp實(shí)現(xiàn)文件上傳功能_Android
- 2023-09-12 如何升級(jí)spring boot中spring框架的版本
- 2022-08-20 Oracle刪除歸檔日志及添加定時(shí)任務(wù)_oracle
- 2022-03-22 詳解jQuery的核心函數(shù)和事件處理_jquery
- 2022-06-06 ASP.NET的Core?AD域登錄過程示例_ASP.NET
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支