Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android CameraX |颜色检测_Android_Kotlin_Android Camera_Android Camerax_Color Detection - Fatal编程技术网

Android CameraX |颜色检测

Android CameraX |颜色检测,android,kotlin,android-camera,android-camerax,color-detection,Android,Kotlin,Android Camera,Android Camerax,Color Detection,我正在安卓上使用新的CameraX 我做了一个基本的应用程序(类似于“入门”),其中我有一个相机预览和一个亮度分析器。每秒钟我都会在文本视图中显示我的LUMONITY 现在,按照CameraX指南,我想做颜色检测。每隔一秒钟左右,我想让屏幕中央像素的颜色 事实上,我不知道如何按照与光度分析器相同的结构进行颜色检测 光度分析器类别: class LuminosityAnalyzer : ImageAnalysis.Analyzer { private var lastTimeStamp = 0L

我正在安卓上使用新的CameraX

我做了一个基本的应用程序(类似于“入门”),其中我有一个相机预览和一个亮度分析器。每秒钟我都会在文本视图中显示我的LUMONITY

现在,按照CameraX指南,我想做颜色检测。每隔一秒钟左右,我想让屏幕中央像素的颜色

事实上,我不知道如何按照与光度分析器相同的结构进行颜色检测

光度分析器类别:

class LuminosityAnalyzer : ImageAnalysis.Analyzer {

private var lastTimeStamp = 0L
private val TAG = this.javaClass.simpleName
var luma = BehaviorSubject.create<Double>()

override fun analyze(image: ImageProxy, rotationDegrees: Int) {
    val currentTimeStamp = System.currentTimeMillis()
    val intervalInSeconds = TimeUnit.SECONDS.toMillis(1)
    val deltaTime = currentTimeStamp - lastTimeStamp
    if(deltaTime >= intervalInSeconds) {
        val buffer = image.planes[0].buffer
        val data = buffer.toByteArray()
        val pixels = data.map { it.toInt() and 0xFF }
        luma.onNext(pixels.average())
        lastTimeStamp = currentTimeStamp
        Log.d(TAG, "Average luminosity: ${luma.value}")
    }


private fun ByteBuffer.toByteArray(): ByteArray {
    rewind()
    val data = ByteArray(remaining())
    get(data)
    return data
}
}

作为一名颜色分析器,我如何做一些等效的事情呢?

所以我自己想出了办法

颜色分析器类别:

/* display the luminosity */
private fun createLuminosityAnalyzer(): ImageAnalysis{
    val analyzerConfig = ImageAnalysisConfig.Builder().apply {
        setLensFacing(lensFacing)
        setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
    }.build()

    val analyzer = ImageAnalysis(analyzerConfig).apply {
        val luminosityAnalyzer = LuminosityAnalyzer()
        luminosityAnalyzer.luma
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
            // success
            luminosity.text = it.toString()
        },{
            // error
            Log.d(TAG, "Can not get luminosity :(")
        })
        setAnalyzer(executor, luminosityAnalyzer)
    }
    return analyzer
}
class ColorAnalyzer : ImageAnalysis.Analyzer {

private var lastTimeStamp = 0L
private val TAG = this.javaClass.simpleName
var hexColor = BehaviorSubject.create<Any>()

/* every 100ms, analyze the image we receive from camera */
override fun analyze(image: ImageProxy, rotationDegrees: Int) {
    val currentTimeStamp = System.currentTimeMillis()
    val intervalInMilliSeconds = TimeUnit.MILLISECONDS.toMillis(100)
    val deltaTime = currentTimeStamp - lastTimeStamp
    if(deltaTime >= intervalInMilliSeconds) {

        val imageBitmap = image.image?.toBitmap()
        val pixel = imageBitmap!!.getPixel((imageBitmap.width/2), (imageBitmap.height/2))
        val red = Color.red(pixel)
        val blue = Color.blue(pixel)
        val green = Color.green(pixel)
        hexColor.onNext(String.format("#%02x%02x%02x", red, green, blue))
        Log.d(TAG, "Color: ${hexColor.value}")

        lastTimeStamp = currentTimeStamp
    }
}

// convert the image into a bitmap
private fun Image.toBitmap(): Bitmap {
    val yBuffer = planes[0].buffer // Y
    val uBuffer = planes[1].buffer // U
    val vBuffer = planes[2].buffer // V

    val ySize = yBuffer.remaining()
    val uSize = uBuffer.remaining()
    val vSize = vBuffer.remaining()

    val nv21 = ByteArray(ySize + uSize + vSize)

    yBuffer.get(nv21, 0, ySize)
    vBuffer.get(nv21, ySize, vSize)
    uBuffer.get(nv21, ySize + vSize, uSize)

    val yuvImage = YuvImage(nv21, ImageFormat.NV21, this.width, this.height, null)
    val out = ByteArrayOutputStream()
    yuvImage.compressToJpeg(Rect(0, 0, yuvImage.width, yuvImage.height), 50, out)
    val imageBytes = out.toByteArray()
    return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
}
}
 /* Get the color from Color Analyzer Class */
private fun createColorAnalyzer(): ImageAnalysis{
    val analyzerConfig = ImageAnalysisConfig.Builder().apply {
        setLensFacing(lensFacing)
        setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
    }.build()

    val analyzer = ImageAnalysis(analyzerConfig).apply {
        val colorAnalyzer = ColorAnalyzer()
        colorAnalyzer.hexColor
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                // success
                colorName.text = it.toString() //hexa code in the textView
                colorName.setBackgroundColor(Color.parseColor(it.toString())) //background color of the textView
                (sight.drawable as GradientDrawable).setStroke(10, Color.parseColor(it.toString())) //border color of the sight in the middle of the screen
            },{
                // error
                Log.d(TAG, "Can not get color :(")
            })
        setAnalyzer(executor, colorAnalyzer)
    }
    return analyzer
}
class ColorAnalyzer : ImageAnalysis.Analyzer {

private var lastAnalyzedTimestamp = 0L


private fun ByteBuffer.toByteArray(): ByteArray {
    rewind()    // Rewind the buffer to zero
    val data = ByteArray(remaining())
    get(data)   // Copy the buffer into a byte array
    return data // Return the byte array
}


private fun getRGBfromYUV(image: ImageProxy): Triple<Double, Double, Double> {
    val planes = image.planes

    val height = image.height
    val width = image.width

    // Y
    val yArr = planes[0].buffer
    val yArrByteArray = yArr.toByteArray()
    val yPixelStride = planes[0].pixelStride
    val yRowStride = planes[0].rowStride

    // U
    val uArr = planes[1].buffer
    val uArrByteArray =uArr.toByteArray()
    val uPixelStride = planes[1].pixelStride
    val uRowStride = planes[1].rowStride

    // V
    val vArr = planes[2].buffer
    val vArrByteArray = vArr.toByteArray()
    val vPixelStride = planes[2].pixelStride
    val vRowStride = planes[2].rowStride

    val y = yArrByteArray[(height * yRowStride + width * yPixelStride) / 2].toInt() and 255
    val u = (uArrByteArray[(height * uRowStride + width * uPixelStride) / 4].toInt() and 255) - 128
    val v = (vArrByteArray[(height * vRowStride + width * vPixelStride) / 4].toInt() and 255) - 128

    val r = y + (1.370705 * v)
    val g = y - (0.698001 * v) - (0.337633 * u)
    val b = y + (1.732446 * u)

    return Triple(r,g,b)
}


// analyze the color
override fun analyze(image: ImageProxy, rotationDegrees: Int) {
    val currentTimestamp = System.currentTimeMillis()
    if (currentTimestamp - lastAnalyzedTimestamp >= TimeUnit.MILLISECONDS.toMillis(100)) {

        val colors = getRGBfromYUV(image)
        var hexColor = String.format("#%02x%02x%02x", colors.first.toInt(), colors.second.toInt(), colors.third.toInt())
        Log.d("test", "hexColor: $hexColor")

        lastAnalyzedTimestamp = currentTimestamp
    }

}
}
希望对某人有用;)

编辑:

/* display the luminosity */
private fun createLuminosityAnalyzer(): ImageAnalysis{
    val analyzerConfig = ImageAnalysisConfig.Builder().apply {
        setLensFacing(lensFacing)
        setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
    }.build()

    val analyzer = ImageAnalysis(analyzerConfig).apply {
        val luminosityAnalyzer = LuminosityAnalyzer()
        luminosityAnalyzer.luma
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
            // success
            luminosity.text = it.toString()
        },{
            // error
            Log.d(TAG, "Can not get luminosity :(")
        })
        setAnalyzer(executor, luminosityAnalyzer)
    }
    return analyzer
}
class ColorAnalyzer : ImageAnalysis.Analyzer {

private var lastTimeStamp = 0L
private val TAG = this.javaClass.simpleName
var hexColor = BehaviorSubject.create<Any>()

/* every 100ms, analyze the image we receive from camera */
override fun analyze(image: ImageProxy, rotationDegrees: Int) {
    val currentTimeStamp = System.currentTimeMillis()
    val intervalInMilliSeconds = TimeUnit.MILLISECONDS.toMillis(100)
    val deltaTime = currentTimeStamp - lastTimeStamp
    if(deltaTime >= intervalInMilliSeconds) {

        val imageBitmap = image.image?.toBitmap()
        val pixel = imageBitmap!!.getPixel((imageBitmap.width/2), (imageBitmap.height/2))
        val red = Color.red(pixel)
        val blue = Color.blue(pixel)
        val green = Color.green(pixel)
        hexColor.onNext(String.format("#%02x%02x%02x", red, green, blue))
        Log.d(TAG, "Color: ${hexColor.value}")

        lastTimeStamp = currentTimeStamp
    }
}

// convert the image into a bitmap
private fun Image.toBitmap(): Bitmap {
    val yBuffer = planes[0].buffer // Y
    val uBuffer = planes[1].buffer // U
    val vBuffer = planes[2].buffer // V

    val ySize = yBuffer.remaining()
    val uSize = uBuffer.remaining()
    val vSize = vBuffer.remaining()

    val nv21 = ByteArray(ySize + uSize + vSize)

    yBuffer.get(nv21, 0, ySize)
    vBuffer.get(nv21, ySize, vSize)
    uBuffer.get(nv21, ySize + vSize, uSize)

    val yuvImage = YuvImage(nv21, ImageFormat.NV21, this.width, this.height, null)
    val out = ByteArrayOutputStream()
    yuvImage.compressToJpeg(Rect(0, 0, yuvImage.width, yuvImage.height), 50, out)
    val imageBytes = out.toByteArray()
    return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
}
}
 /* Get the color from Color Analyzer Class */
private fun createColorAnalyzer(): ImageAnalysis{
    val analyzerConfig = ImageAnalysisConfig.Builder().apply {
        setLensFacing(lensFacing)
        setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
    }.build()

    val analyzer = ImageAnalysis(analyzerConfig).apply {
        val colorAnalyzer = ColorAnalyzer()
        colorAnalyzer.hexColor
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                // success
                colorName.text = it.toString() //hexa code in the textView
                colorName.setBackgroundColor(Color.parseColor(it.toString())) //background color of the textView
                (sight.drawable as GradientDrawable).setStroke(10, Color.parseColor(it.toString())) //border color of the sight in the middle of the screen
            },{
                // error
                Log.d(TAG, "Can not get color :(")
            })
        setAnalyzer(executor, colorAnalyzer)
    }
    return analyzer
}
class ColorAnalyzer : ImageAnalysis.Analyzer {

private var lastAnalyzedTimestamp = 0L


private fun ByteBuffer.toByteArray(): ByteArray {
    rewind()    // Rewind the buffer to zero
    val data = ByteArray(remaining())
    get(data)   // Copy the buffer into a byte array
    return data // Return the byte array
}


private fun getRGBfromYUV(image: ImageProxy): Triple<Double, Double, Double> {
    val planes = image.planes

    val height = image.height
    val width = image.width

    // Y
    val yArr = planes[0].buffer
    val yArrByteArray = yArr.toByteArray()
    val yPixelStride = planes[0].pixelStride
    val yRowStride = planes[0].rowStride

    // U
    val uArr = planes[1].buffer
    val uArrByteArray =uArr.toByteArray()
    val uPixelStride = planes[1].pixelStride
    val uRowStride = planes[1].rowStride

    // V
    val vArr = planes[2].buffer
    val vArrByteArray = vArr.toByteArray()
    val vPixelStride = planes[2].pixelStride
    val vRowStride = planes[2].rowStride

    val y = yArrByteArray[(height * yRowStride + width * yPixelStride) / 2].toInt() and 255
    val u = (uArrByteArray[(height * uRowStride + width * uPixelStride) / 4].toInt() and 255) - 128
    val v = (vArrByteArray[(height * vRowStride + width * vPixelStride) / 4].toInt() and 255) - 128

    val r = y + (1.370705 * v)
    val g = y - (0.698001 * v) - (0.337633 * u)
    val b = y + (1.732446 * u)

    return Triple(r,g,b)
}


// analyze the color
override fun analyze(image: ImageProxy, rotationDegrees: Int) {
    val currentTimestamp = System.currentTimeMillis()
    if (currentTimestamp - lastAnalyzedTimestamp >= TimeUnit.MILLISECONDS.toMillis(100)) {

        val colors = getRGBfromYUV(image)
        var hexColor = String.format("#%02x%02x%02x", colors.first.toInt(), colors.second.toInt(), colors.third.toInt())
        Log.d("test", "hexColor: $hexColor")

        lastAnalyzedTimestamp = currentTimestamp
    }

}
}
如果您阅读@Minhaz答案,那么通过执行image->bitmap->getPixel()获取颜色不是很有效。最有效的方法是做图像->RGB

这是Minhaz与Kotlin合作的答案

颜色分析器类别:

/* display the luminosity */
private fun createLuminosityAnalyzer(): ImageAnalysis{
    val analyzerConfig = ImageAnalysisConfig.Builder().apply {
        setLensFacing(lensFacing)
        setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
    }.build()

    val analyzer = ImageAnalysis(analyzerConfig).apply {
        val luminosityAnalyzer = LuminosityAnalyzer()
        luminosityAnalyzer.luma
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
            // success
            luminosity.text = it.toString()
        },{
            // error
            Log.d(TAG, "Can not get luminosity :(")
        })
        setAnalyzer(executor, luminosityAnalyzer)
    }
    return analyzer
}
class ColorAnalyzer : ImageAnalysis.Analyzer {

private var lastTimeStamp = 0L
private val TAG = this.javaClass.simpleName
var hexColor = BehaviorSubject.create<Any>()

/* every 100ms, analyze the image we receive from camera */
override fun analyze(image: ImageProxy, rotationDegrees: Int) {
    val currentTimeStamp = System.currentTimeMillis()
    val intervalInMilliSeconds = TimeUnit.MILLISECONDS.toMillis(100)
    val deltaTime = currentTimeStamp - lastTimeStamp
    if(deltaTime >= intervalInMilliSeconds) {

        val imageBitmap = image.image?.toBitmap()
        val pixel = imageBitmap!!.getPixel((imageBitmap.width/2), (imageBitmap.height/2))
        val red = Color.red(pixel)
        val blue = Color.blue(pixel)
        val green = Color.green(pixel)
        hexColor.onNext(String.format("#%02x%02x%02x", red, green, blue))
        Log.d(TAG, "Color: ${hexColor.value}")

        lastTimeStamp = currentTimeStamp
    }
}

// convert the image into a bitmap
private fun Image.toBitmap(): Bitmap {
    val yBuffer = planes[0].buffer // Y
    val uBuffer = planes[1].buffer // U
    val vBuffer = planes[2].buffer // V

    val ySize = yBuffer.remaining()
    val uSize = uBuffer.remaining()
    val vSize = vBuffer.remaining()

    val nv21 = ByteArray(ySize + uSize + vSize)

    yBuffer.get(nv21, 0, ySize)
    vBuffer.get(nv21, ySize, vSize)
    uBuffer.get(nv21, ySize + vSize, uSize)

    val yuvImage = YuvImage(nv21, ImageFormat.NV21, this.width, this.height, null)
    val out = ByteArrayOutputStream()
    yuvImage.compressToJpeg(Rect(0, 0, yuvImage.width, yuvImage.height), 50, out)
    val imageBytes = out.toByteArray()
    return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
}
}
 /* Get the color from Color Analyzer Class */
private fun createColorAnalyzer(): ImageAnalysis{
    val analyzerConfig = ImageAnalysisConfig.Builder().apply {
        setLensFacing(lensFacing)
        setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
    }.build()

    val analyzer = ImageAnalysis(analyzerConfig).apply {
        val colorAnalyzer = ColorAnalyzer()
        colorAnalyzer.hexColor
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                // success
                colorName.text = it.toString() //hexa code in the textView
                colorName.setBackgroundColor(Color.parseColor(it.toString())) //background color of the textView
                (sight.drawable as GradientDrawable).setStroke(10, Color.parseColor(it.toString())) //border color of the sight in the middle of the screen
            },{
                // error
                Log.d(TAG, "Can not get color :(")
            })
        setAnalyzer(executor, colorAnalyzer)
    }
    return analyzer
}
class ColorAnalyzer : ImageAnalysis.Analyzer {

private var lastAnalyzedTimestamp = 0L


private fun ByteBuffer.toByteArray(): ByteArray {
    rewind()    // Rewind the buffer to zero
    val data = ByteArray(remaining())
    get(data)   // Copy the buffer into a byte array
    return data // Return the byte array
}


private fun getRGBfromYUV(image: ImageProxy): Triple<Double, Double, Double> {
    val planes = image.planes

    val height = image.height
    val width = image.width

    // Y
    val yArr = planes[0].buffer
    val yArrByteArray = yArr.toByteArray()
    val yPixelStride = planes[0].pixelStride
    val yRowStride = planes[0].rowStride

    // U
    val uArr = planes[1].buffer
    val uArrByteArray =uArr.toByteArray()
    val uPixelStride = planes[1].pixelStride
    val uRowStride = planes[1].rowStride

    // V
    val vArr = planes[2].buffer
    val vArrByteArray = vArr.toByteArray()
    val vPixelStride = planes[2].pixelStride
    val vRowStride = planes[2].rowStride

    val y = yArrByteArray[(height * yRowStride + width * yPixelStride) / 2].toInt() and 255
    val u = (uArrByteArray[(height * uRowStride + width * uPixelStride) / 4].toInt() and 255) - 128
    val v = (vArrByteArray[(height * vRowStride + width * vPixelStride) / 4].toInt() and 255) - 128

    val r = y + (1.370705 * v)
    val g = y - (0.698001 * v) - (0.337633 * u)
    val b = y + (1.732446 * u)

    return Triple(r,g,b)
}


// analyze the color
override fun analyze(image: ImageProxy, rotationDegrees: Int) {
    val currentTimestamp = System.currentTimeMillis()
    if (currentTimestamp - lastAnalyzedTimestamp >= TimeUnit.MILLISECONDS.toMillis(100)) {

        val colors = getRGBfromYUV(image)
        var hexColor = String.format("#%02x%02x%02x", colors.first.toInt(), colors.second.toInt(), colors.third.toInt())
        Log.d("test", "hexColor: $hexColor")

        lastAnalyzedTimestamp = currentTimestamp
    }

}
}
class ColorAnalyzer:ImageAnalysis.Analyzer{
私有变量lastAnalyzedTimestamp=0L
私人娱乐ByteBuffer.toByteArray():ByteArray{
倒带()//将缓冲区倒带到零
val data=ByteArray(剩余()
get(data)//将缓冲区复制到字节数组中
返回数据//返回字节数组
}
私人娱乐getRGBfromYUV(图像:ImageProxy):三重{
val平面=image.planes
val height=image.height
val width=image.width
//Y
val yArr=平面[0]。缓冲区
val yArrByteArray=yArr.toByteArray()
val-yPixelStride=平面[0]。pixelStride
val yRowStride=平面[0]。行步幅
//U
val uArr=平面[1]。缓冲区
val uArrByteArray=uArr.toByteArray()
val-uPixelStride=平面[1]。像素步长
val uRowStride=平面[1]。行步幅
//五
val vArr=平面[2]。缓冲区
val vArrByteArray=vArr.toByteArray()
val vPixelStride=平面[2]。像素步长
val vRowStride=平面[2]。行
val y=yArrByteArray[(高度*yRowStride+宽度*yPixelStride)/2].toInt()和255
val u=(uArrByteArray[(高度*uRowStride+宽度*uPixelStride)/4].toInt()和255)-128
val v=(vArrByteArray[(高度*vRowStride+宽度*vPixelStride)/4].toInt()和255)-128
val r=y+(1.370705*v)
val g=y-(0.698001*v)-(0.337633*u)
val b=y+(1.732446*u)
返回三元组(r、g、b)
}
//分析颜色
覆盖乐趣分析(图像:ImageProxy,旋转度:Int){
val currentTimestamp=System.currentTimeMillis()
如果(currentTimestamp-lastAnalyzedTimestamp>=TimeUnit.millis.toMillis(100)){
val colors=getRGBfromYUV(图像)
var hexColor=String.format(“#%02x%02x%02x”,colors.first.toInt(),colors.second.toInt(),colors.third.toInt())
Log.d(“测试”,“hexColor:$hexColor”)
lastAnalyzedTimestamp=当前时间戳
}
}
}

如评论中所述,如果您的目标是仅获取中心像素颜色,则将整个YUV图像转换为位图,然后分析中心值的逻辑可能非常低效。您可以通过瞄准正确的像素直接查看YUV图像中的颜色。在YUV图像中有三个平面,一个用于Y(每像素1字节)和U&V平面(.5字节每像素,交错)。忽略此时的旋转,因为无论旋转如何,中心像素都应相同(放弃高度或宽度奇数值的可能性)。获取中心像素rgb值的有效逻辑如下所示:

planes = imageProxy.getPlanes()

val height = imageProxy.getHeight()
val width = imageProxy.getWidth()

// You may have to find the logic to get array from ByteBuffer
// Y
val yArr = planes[0].buffer.array()
val yPixelStride = planes[0].getPixelStride()
val yRowStride = planes[0].getRowStride()

// U
val uArr = planes[1].buffer.array()
val uPixelStride = planes[1].getPixelStride()
val uRowStride = planes[1].getRowStride()

// V
val vArr = planes[2].buffer.array()
val vPixelStride = planes[2].getPixelStride()
val vRowStride = planes[2].getRowStride()

val y = yArr[(height * yRowStride + width * yPixelStride) / 2] & 255 
val u = (uArr[(height * uRowStride + width * uPixelStride) / 4] & 255) - 128
val v = (vArr[(height * vRowStride + width * vPixelStride) / 4] & 255) - 128 

val r = y + (1.370705 * v);
val g = y - (0.698001 * v) - (0.337633 * u);
val b = y + (1.732446 * u);
参考魔法值:


请尝试在Kotlin代码中使用此逻辑,以查看它是否正常工作,是否能够快速执行实时操作。这肯定会将
O(高度*宽度)
操作降低到恒定的时间复杂度。

如果将每个YUV帧转换为位图,然后从中获取一两个像素的颜色,则效率非常低。YUV数据本身有三个平面,一个带有Y缓冲区(亮度),另外两个缓冲区指示色度值U和V缓冲区。虽然每个像素有一个Y字节,但U&V作为交错值可用。使用YUV数据本身以及像素步长和行步长可以获得任何像素的颜色值,而不是将整个图像转换为位图(顺便说一句,位图比内存中的YUV大约8/3倍),如果您正在寻找有效解决方案的代码示例,让我知道-我会尽量在这里想出一个。你的解决方案是非常受欢迎的。另一方面,我将总共有16个像素区来分析颜色,因此将整个图像转换为位图的效率会降低。我想,又是我,我真的需要你所说的高效解决方案。我试着这么做,但我正努力从YUV飞机上直接获得颜色。谢谢大家!@Minhaz将逻辑添加为注释。