Skip to content

Instantly share code, notes, and snippets.

@LiewJunTung
Created July 9, 2019 17:31
Show Gist options
  • Save LiewJunTung/fa1326017fcf5ed34c32f5f1b3e0c4e8 to your computer and use it in GitHub Desktop.
Save LiewJunTung/fa1326017fcf5ed34c32f5f1b3e0c4e8 to your computer and use it in GitHub Desktop.
package com.liewjuntung.cameraxfacetracking
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.os.Bundle
import android.os.Handler
import android.os.HandlerThread
import android.renderscript.RenderScript
import android.util.Rational
import android.util.Size
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.CameraX
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageAnalysisConfig
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_yuv_to_rgb.*
import java.util.concurrent.TimeUnit
class YuvToRgbActivity : AppCompatActivity() {
private var lastAnalyzedTimestamp = 0L
private lateinit var renderScript: RenderScript
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
renderScript = RenderScript.create(this)
setContentView(R.layout.activity_yuv_to_rgb)
if (allPermissionsGranted()) {
startCamera()
} else {
ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
}
}
private fun startCamera() {
val width = 1920
val height = 1080
//val imageReader = ImageReader.newInstance(width, height, ImageFormat.YUV_420_888, 1)
val imageReadConfig = ImageAnalysisConfig.Builder().apply {
// Use a worker thread for image analysis to prevent glitches
setLensFacing((CameraX.LensFacing.FRONT))
setTargetAspectRatio(Rational(16, 9))
setTargetResolution(Size(width, height))
val analyzerThread = HandlerThread("Analyser").apply { start() }
setCallbackHandler(Handler(analyzerThread.looper))
setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
}.build()
CameraX.bindToLifecycle(this, imageReadConfig.yuvToRgbAnalyser())
}
private fun ImageAnalysisConfig.yuvToRgbAnalyser(): ImageAnalysis {
return ImageAnalysis(this).apply {
val yuvToRgb = RsYuvToRgb(renderScript)
analyzer = ImageAnalysis.Analyzer { image, _ ->
val currentTimestamp = System.currentTimeMillis()
// Calculate the average luma no more often than every second
if (currentTimestamp - lastAnalyzedTimestamp >=
TimeUnit.SECONDS.toMillis(3)
) {
val planes = image.planes
val iWidth = image.width
val iHeight = image.height
// Log.d(TAG, "W: $iWidth H: $iHeight")
val rgbBuffer = yuvToRgb.yuvToRgb(planes[0].buffer,
planes[1].buffer,
planes[2].buffer, iWidth, iHeight)
imageview.post {
val bitmapImage = Bitmap.createBitmap(iHeight, iWidth, Bitmap.Config.ARGB_8888)
rgbBuffer.rewind()
bitmapImage.copyPixelsFromBuffer(rgbBuffer)
imageview.setImageBitmap(bitmapImage)
}
}
}
}
}
/**
* Check if all permission specified in the manifest have been granted
*/
private fun allPermissionsGranted(): Boolean {
for (permission in REQUIRED_PERMISSIONS) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
return false
}
}
return true
}
/**
* Process result from permission request dialog box, has the request
* been granted? If yes, start Camera. Otherwise display a toast
*/
override fun onRequestPermissionsResult(
requestCode: Int, permissions: Array<String>, grantResults: IntArray
) {
if (requestCode == REQUEST_CODE_PERMISSIONS) {
if (allPermissionsGranted()) {
imageview.post { startCamera() }
} else {
Toast.makeText(
this,
"Permissions not granted by the user.",
Toast.LENGTH_SHORT
).show()
finish()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment