Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android.media.ImageWriter produces green screen screen on every second posted Image frame

Posted on November 11, 2022 By

Solution 1 :

Based on library creator’s comment:
“Image rowStribe and width are not equal, so size of data per row is different between Image and I420Buffer, resulting in image misalignment.

If Buffer is created from Image, rowStribe of Image is used.”

val image = imageWriter.dequeueInputImage()
yuvBuffer.convertTo(image.toI420Buffer())
imageWriter.queueInputImage(image)

Problem :

I have a sample app to generate YUV images out of predefined jpeg test data and post it to the ImageWriter in order to mock the Camera API for end2end tests: https://github.com/adjorno/Bitmap2YUV

package com.example.bitmap2yuv

import android.graphics.*
import android.media.ImageWriter
import android.os.Bundle
import android.view.Surface
import android.view.TextureView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.scale
import androidx.lifecycle.lifecycleScope
import io.github.crow_misia.libyuv.AbgrBuffer
import io.github.crow_misia.libyuv.I420Buffer
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity(), TextureView.SurfaceTextureListener {
    private lateinit var textureView: TextureView
    private var bitmap: Bitmap? = null
    private var imageWriter: ImageWriter? = null
    private var yuvBuffer: I420Buffer? = null
    private var running = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        textureView = TextureView(this)
        textureView.surfaceTextureListener = this
        setContentView(textureView)
    }

    private fun createImageWriter(surface: Surface, width: Int, height: Int) {
        yuvBuffer = I420Buffer.allocate(width, height)
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).apply {
            eraseColor(Color.BLUE)
        }

        val argbBuffer = AbgrBuffer.allocate(width, height)
        bitmap?.copyPixelsToBuffer(argbBuffer.asBuffer())
        argbBuffer.convertTo(yuvBuffer!!)
        imageWriter = ImageWriter.newInstance(surface, 1, ImageFormat.YUV_420_888)
        startRendering()
    }

    private fun startRendering() {
        imageWriter?.let { imageWriter ->
            yuvBuffer?.let { yuvBuffer ->
                running = true
                lifecycleScope.launch(Dispatchers.IO) {
                    while (running) {
                        delay(1000)
                        val image = imageWriter.dequeueInputImage()
                        image.planes[0].buffer.put(yuvBuffer.planeY.buffer)
                        image.planes[1].buffer.put(yuvBuffer.planeU.buffer)
                        image.planes[2].buffer.put(yuvBuffer.planeV.buffer)
                        imageWriter.queueInputImage(image)
                    }
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        startRendering()
    }

    override fun onStop() {
        super.onStop()
        running = false
    }

    override fun onSurfaceTextureAvailable(surfaceTexture: SurfaceTexture, width: Int, height: Int) {
        createImageWriter(Surface(surfaceTexture), width, height)
    }

    override fun onSurfaceTextureSizeChanged(p0: SurfaceTexture, p1: Int, p2: Int) {
    }

    override fun onSurfaceTextureDestroyed(p0: SurfaceTexture): Boolean {
        return true
    }

    override fun onSurfaceTextureUpdated(p0: SurfaceTexture) {
    }
}

I am expecting to see the blue screen all the time (with 1 fps), but instead I see that every second frame is replaced with a green screen. How to get rid of it?

Blue/Green screen

Comments

Comment posted by edit

Your answer could be improved with additional supporting information. Please

READ  [FIXED] android - How to delete recyclerview items by id?
Powered by Inline Related Posts

Android Tags:android, android-textureview, media, surface, yuv

Post navigation

Previous Post: [FIXED] android – How to detect when third party app’s dialog box appears in React native?
Next Post: [FIXED] android – Get the list of user SIM cards and select it – flutter

Related Posts

[FIXED] react native – How to display action buttons in Android notification when app is closed? Android
[FIXED] android – Statusbar not showing for other screens in Flutter if Splashscreen is added Android
[FIXED] android – androidx ViewModel MutableLiveData LiveData Android
[FIXED] How to change Android icon everywhere? Android
[FIXED] android – Is there a CalendarView equivalent in flutter? Android
[FIXED] java – Problems with zip function in rxjava2 Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme