Skip to content

Video is corrupted and green #12

@sarimmehdi

Description

@sarimmehdi

Hello. I am not sure what I am doing wrong here. The below method is what I use to get the bitmap from a custom SurfaceView class:

private fun getSnapshot(): Bitmap? {
    val surfaceBitmap = Bitmap.createBitmap(800, 600, Bitmap.Config.ARGB_8888)
    val lock = Object()
    val success = AtomicBoolean(false)
    val thread = HandlerThread("PixelCopyHelper")
    thread.start()
    val sHandler = Handler(thread.looper)
    val listener = PixelCopy.OnPixelCopyFinishedListener { copyResult ->
        success.set(copyResult == PixelCopy.SUCCESS)
        synchronized (lock) {
            lock.notify()
        }
    }
    synchronized (lock) {
        PixelCopy.request(video_stream_view.holder.surface, surfaceBitmap, listener, sHandler)
        lock.wait()
    }
    thread.quitSafely()
    return if (success.get()) surfaceBitmap else null
}

video_stream_view is a view that receives a live video feed from an RTSP camera. I then use the below piece of code to save the bitmap using the encoder:

val bitmap = getSnapshot()
bitmap?.let {
    val m = Matrix()
    m.preScale(-1f, -1f)
    val dst = Bitmap.createBitmap(it, 0, 0, it.width, it.height, m, false)
    dst.density = DisplayMetrics.DENSITY_DEFAULT
    if (recordVideo) {
        encoder.addFrame(dst)
    }

The bitmap extracted from the SurfaceView is upside down so I apply some operations to get it upright. When I reapply the bitmap to the SurfaceView from where I extracted it, it looks just fine:

withContext(Dispatchers.Main) {
    video_stream_view.background = BitmapDrawable(resources, dst)
}

But the recorded video looks like this:
Screenshot from 2023-07-19 12-20-29

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions