从Image对象中获取指定区域(x=500,y=20,w=1000,h=500)的YUV数据
var cropX = 500
var cropY = 200
var cropW = 1000
var cropH = 500
fun imageCrop2YUV(image: Image):ByteArray?{
var width = image.cropRect.width()
var height = image.cropRect.height()
val yBuffer = image.planes[0].buffer
val uBuffer = image.planes[1].buffer
val vBuffer = image.planes[2].buffer
var index = 0
val yuvBytes = ByteArray(cropW* cropH*3/2)
for (i in cropY until cropY+ cropH){
for (j in i*width+ cropX until i*width+ cropX+ cropW){
yuvBytes[index] = yBuffer[j]
index++
}
}
val uStep = image.planes[1].pixelStride
val vStep = image.planes[2].pixelStride
for (i in cropY until cropY+ cropH step 2){
for (j in i/2*width + cropX until i/2*width + cropX + cropW step uStep){
yuvBytes[index] = uBuffer[j]
index++
}
}
for (i in cropY until cropY+ cropH step 2){
for (j in i/2*width + cropX until i/2*width + cropX + cropW step vStep){
yuvBytes[index] = vBuffer[j]
index++
}
}
return yuvBytes
}