SurfaceView 截屏方案

文章介绍了两种在Android中截取SurfaceView屏幕的方法:一是使用PixelCopy类,适用于API26及以上版本;二是利用MediaProjection服务,这是一个系统级别的截屏方式,需要创建前台服务并申请权限。PixelCopy使用简单,而MediaProjection更灵活但设置过程较繁琐。
摘要由CSDN通过智能技术生成

1.PixelCopy这个类是安卓提供的截取SurfaceView 的一个类:

        val surfaceView: SurfaceView = binding.realTimeMap.getBinding()
        val bitmap = Bitmap.createBitmap(surfaceView.width, surfaceView.height, Bitmap.Config.ARGB_8888)

        val surfaceHolder = surfaceView.holder

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val copyResult = PixelCopy.request(
                surfaceHolder.surface,
                bitmap,
                PixelCopy.OnPixelCopyFinishedListener { copyResult ->
                    if (copyResult == PixelCopy.SUCCESS) {
                        // 截图成功,可以在这里处理Bitmap对象
                        binding.ivMaxSpeed.setImageBitmap(bitmap)

                    } else {
                        // 截图失败,根据需要进行处理
                    }
                },
                Handler() // 可选的Handler对象,用于指定回调在哪个线程执行,如果不指定,默认在主线程执行
            )
        }

2.使用MediaProjection ,这个是系统级的截屏方法,但是比较繁琐:
a.建立前台服务;
//启动MediaService服务

    fun startMediaService() {
        if (isstartservice) {
            startService(Intent(this, MediaService::class.java))
            isstartservice = false
        }
    }

***

## MediaSercice:

***:
package com.allynav.iefa.service

import android.R
import android.app.*
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import android.os.IBinder
import androidx.core.app.NotificationCompat
 

class MediaService : Service() {
    private val NOTIFICATION_CHANNEL_ID = "com.tencent.trtc.apiexample.MediaService"
    private val NOTIFICATION_CHANNEL_NAME = "com.tencent.trtc.apiexample.channel_name"
    private val NOTIFICATION_CHANNEL_DESC = "com.tencent.trtc.apiexample.channel_desc"
    override fun onCreate() {
        super.onCreate()
        startNotification()
    }

    fun startNotification() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //Call Start foreground with notification
            val notificationIntent = Intent(this, MediaService::class.java)
            val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0)
            val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                    .setLargeIcon(
                        BitmapFactory.decodeResource(
                            getResources(),
                            R.drawable.alert_dark_frame
                        )
                    )
                    .setSmallIcon(R.drawable.alert_dark_frame)
                    .setContentTitle("Starting Service")
                    .setContentText("Starting monitoring service")
                    .setContentIntent(pendingIntent)
            val notification: Notification = notificationBuilder.build()
            val channel = NotificationChannel(
                NOTIFICATION_CHANNEL_ID,
                NOTIFICATION_CHANNEL_NAME,
                NotificationManager.IMPORTANCE_DEFAULT
            )
            channel.description = NOTIFICATION_CHANNEL_DESC
            val notificationManager =
                getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
            startForeground(
                1,
                notification
            ) //必须使用此方法显示通知,不能使用notificationManager.notify,否则还是会报上面的错误
        }
    }

    override fun onBind(intent: Intent?): IBinder {
        throw UnsupportedOperationException("Not yet implemented")
    }
}

b.// 申请截屏权限
    private fun getScreenShotPower() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mediaProjectionManager =
                getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
            if (mediaProjectionManager != null) {
                val intent = mediaProjectionManager!!.createScreenCaptureIntent()
                startActivityForResult(intent, REQUEST_MEDIA_PROJECTION)
            }
        }
    }

c.  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    override fun onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_MEDIA_PROJECTION && data != null) {
//            mediaProjection = mediaProjectionManager!!.getMediaProjection(RESULT_OK, data)
            val mediaPro = mediaProjectionManager!!.getMediaProjection(RESULT_OK, data)

            val bitmap = screenShot(mediaPro)      //截屏
            val bs = getBitmapByte(bitmap)
            //对图片进行处理逻辑  例如可以保存本地、进行图片分享等
            Log.e("WWW", "EEE" + bitmap)
            if (bitmap != null) {
                shareTipsPop.setShare(bitmap)
                shareTipsPop.showPopupWindow()
            }

        }
    }
d.处理图片的方法:
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    fun screenShot(mediaProjection: MediaProjection): Bitmap? {
        val wm1 = this.windowManager
        val width = wm1.defaultDisplay.width
        val height = wm1.defaultDisplay.height
        Objects.requireNonNull(mediaProjection)
        @SuppressLint("WrongConstant") val imageReader: ImageReader =
            ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 60)
        var virtualDisplay: VirtualDisplay? = null
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            virtualDisplay = mediaProjection.createVirtualDisplay(
                "screen",
                width,
                height,
                1,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                imageReader.getSurface(),
                null,
                null
            )
        }
        SystemClock.sleep(1000)
        //取最新的图片
        val image: Image = imageReader.acquireLatestImage()
        // Image image = imageReader.acquireNextImage();
        //释放 virtualDisplay,不释放会报错
        virtualDisplay!!.release()
        return image2Bitmap(image)
    }
 
 
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    fun image2Bitmap(image: Image?): Bitmap? {
        if (image == null) {
            println("image 为空")
            return null
        }
        val width: Int = image.getWidth()
        val height: Int = image.getHeight()
        val planes: Array<Image.Plane> = image.planes
        val buffer: ByteBuffer = planes[0].buffer
        val pixelStride: Int = planes[0].pixelStride
        val rowStride: Int = planes[0].rowStride
        val rowPadding = rowStride - pixelStride * width
        val 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
    }

3.使用 holder.lockCanvas():

// 获取SurfaceView
SurfaceView surfaceView = findViewById(R.id.surface_view);
// 获取SurfaceView的Holder对象
SurfaceHolder holder = surfaceView.getHolder();
// 创建Canvas对象
Canvas canvas = holder.lockCanvas();
// 绘制你想要的内容
// ...
// 将Canvas中的内容转换为Bitmap
Bitmap bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
// 解锁Canvas并刷新SurfaceView
holder.unlockCanvasAndPost(canvas);
// 至此,你已经成功获取SurfaceView的图片了

或者:
// 1. 创建一个与 SurfaceView 大小相同的 Bitmap 对象
val surfaceView: SurfaceView = findViewById(R.id.your_surface_view_id)
val bitmap = Bitmap.createBitmap(surfaceView.width, surfaceView.height, Bitmap.Config.ARGB_8888)

// 2. 获取 SurfaceView 的 SurfaceHolder
val surfaceHolder = surfaceView.holder

// 3. 锁定 Surface,并将其绘制到 Bitmap 中
var canvas: Canvas? = null
try {
    canvas = surfaceHolder.lockCanvas(null)
    canvas?.drawBitmap(bitmap, 0f, 0f, null)
} finally {
    if (canvas != null) {
        surfaceHolder.unlockCanvasAndPost(canvas)
    }
}

// 现在,bitmap 中就包含了 SurfaceView 的内容

其他方法不做表述。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android中,可以使用SurfaceView来进行截图操作。SurfaceView是一个可以在后台线程中进行绘制操作的View,它可以用来显示相机预览、视频播放等。 要实现SurfaceView截图,可以采用以下步骤: 1. 首先,需要在布局文件中添加SurfaceView控件,用来显示需要截图的内容。 2. 在Activity中,获取SurfaceView实例,并设置其回调函数。 3. 在SurfaceView的回调函数中,实现绘制功能的代码。可以通过Canvas对象来进行绘制操作,即在SurfaceView上绘制需要截图的内容。 4. 实现截图的方法,可以通过将Canvas对象绘制的内容保存为Bitmap对象,从而得到截图结果。 具体的实现代码如下所示: ``` // 1.在布局文件中添加SurfaceView控件 <SurfaceView android:id="@+id/surfaceView" android:layout_width="match_parent" android:layout_height="match_parent" /> // 2.在Activity中获取SurfaceView实例,并设置回调函数 SurfaceView surfaceView = findViewById(R.id.surfaceView); surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { // 初始化SurfaceView并进行绘制操作 drawOnSurface(holder); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // SurfaceView大小改变的处理操作 } @Override public void surfaceDestroyed(SurfaceHolder holder) { // SurfaceView销毁的处理操作 } }); // 3.绘制操作的代码 private void drawOnSurface(SurfaceHolder holder) { Canvas canvas = holder.lockCanvas(); // 获取Canvas对象 // 在Canvas上进行绘制操作 // ... holder.unlockCanvasAndPost(canvas); // 解锁Canvas并显示绘制结果 } // 4.实现截图的方法 private Bitmap takeScreenshot() { SurfaceView surfaceView = findViewById(R.id.surfaceView); Bitmap bitmap = Bitmap.createBitmap(surfaceView.getWidth(), surfaceView.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); surfaceView.draw(canvas); return bitmap; } ``` 通过以上步骤,我们可以实现在SurfaceView上进行绘制,并通过截图方法获取绘制的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迪霸LZTXDY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值