Skip to content

Instantly share code, notes, and snippets.

@DanishAmjad12
Last active September 16, 2019 11:48
Show Gist options
  • Save DanishAmjad12/09d1992b90cc395f6d6a9d3f0a639599 to your computer and use it in GitHub Desktop.
Save DanishAmjad12/09d1992b90cc395f6d6a9d3f0a639599 to your computer and use it in GitHub Desktop.
class MainActivity: AppCompatActivity() {
private var lensFacing = CameraX.LensFacing.BACK
private val TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
view_finder.post { startCamera() }
// Every time the provided texture view changes, recompute layout
texture.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
updateTransform()
}
}
private fun startCamera() {
val metrics = DisplayMetrics().also { texture.display.getRealMetrics(it) }
val screenSize = Size(metrics.widthPixels, metrics.heightPixels)
val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
val previewConfig = PreviewConfig.Builder().apply {
setLensFacing(lensFacing)
setTargetResolution(screenSize)
setTargetAspectRatio(screenAspectRatio)
setTargetRotation(windowManager.defaultDisplay.rotation)
setTargetRotation(texture.display.rotation)
}.build()
val preview = Preview(previewConfig)
preview.setOnPreviewOutputUpdateListener {
texture.surfaceTexture = it.surfaceTexture
updateTransform()
}
// Create configuration object for the image capture use case
val imageCaptureConfig = ImageCaptureConfig.Builder()
.apply {
setLensFacing(lensFacing)
setTargetAspectRatio(screenAspectRatio)
setTargetRotation(texture.display.rotation)
setCaptureMode(ImageCapture.CaptureMode.MAX_QUALITY)
}.build()
// Build the image capture use case and attach button click listener
val imageCapture = ImageCapture(imageCaptureConfig)
btn_take_picture.setOnClickListener {
val file = File(
Environment.getExternalStorageDirectory().toString() +
"${MainActivity.folderPath}${System.currentTimeMillis()}.jpg"
)
imageCapture.takePicture(file,
object : ImageCapture.OnImageSavedListener {
override fun onError(
error: ImageCapture.UseCaseError,
message: String, exc: Throwable?
) {
val msg = "Photo capture failed: $message"
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
}
override fun onImageSaved(file: File) {
val msg = "Photo capture successfully: ${file.absolutePath}"
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
}
})
}
CameraX.bindToLifecycle(this, preview, imageCapture)
}
private fun updateTransform() {
val matrix = Matrix()
val centerX = texture.width / 2f
val centerY = texture.height / 2f
val rotationDegrees = when (texture.display.rotation) {
Surface.ROTATION_0 -> 0
Surface.ROTATION_90 -> 90
Surface.ROTATION_180 -> 180
Surface.ROTATION_270 -> 270
else -> return
}
matrix.postRotate(-rotationDegrees.toFloat(), centerX, centerY)
texture.setTransform(matrix)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment