Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video is corrupted and green #12

Open
sarimmehdi opened this issue Jul 19, 2023 · 0 comments
Open

Video is corrupted and green #12

sarimmehdi opened this issue Jul 19, 2023 · 0 comments

Comments

@sarimmehdi
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant