Skip to content

Instantly share code, notes, and snippets.

@weiancheng
Last active November 1, 2019 10:47
Show Gist options
  • Save weiancheng/77057dc5bb692c5b216292701de59f78 to your computer and use it in GitHub Desktop.
Save weiancheng/77057dc5bb692c5b216292701de59f78 to your computer and use it in GitHub Desktop.
[Camera] #android #jetpack #camera #camerax #media
dependencies {
// CameraX core library
def camerax_version = "1.0.0-alpha06"
// CameraX view library
def camerax_view_version = "1.0.0-alpha03"
// CameraX extensions library
def camerax_ext_version = "1.0.0-alpha03"
implementation "androidx.camera:camera-core:$camerax_version"
// If you want to use Camera2 extensions
implementation "androidx.camera:camera-camera2:$camerax_version"
// If you to use the Camera View class
implementation "androidx.camera:camera-view:$camerax_view_version"
// If you to use Camera Extensions
implementation "androidx.camera:camera-extensions:$camerax_ext_version"
}
fun buildImageAnalysis(context: Context): ImageAnalysis {
val imageAnalysisConfig = ImageAnalysisConfig.Builder().apply {
setTargetAspectRatio(aspectRatio)
setTargetRotation(rotation)
setTargetResolution(getResolution(context))
setImageReaderMode(readerMode)
setImageQueueDepth(queueDepth)
}.build()
val imageAnalysis = ImageAnalysis(imageAnalysisConfig)
imageAnalysis.setAnalyzer {
image, rotationDegrees ->
val rect = image.cropRect
val format = image.format
val width = image.width
val height = image.height
val planes = image.planes
}
return imageAnalysis
}
private fun getResolution(context: Context): Size {
val displayMetrics = DisplayMetrics()
val windowManager = applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
windowManager.defaultDisplay.getMetrics(displayMetrics)
val screenWidth = displayMetrics.widthPixels
val screenHeight = displayMetrics.heightPixels
return Size(screenWidth, screenHeight)
}
fun buildImageCapture(context: Context): ImageCapture {
val imageCaptureConfig = ImageCaptureConfig.Builder().apply {
setTargetAspectRatio(aspectRatio)
setTargetRotation(rotation)
setTargetResolution(getResolution(context))
setFlashMode(flashMode)
setCaptureMode(captureMode)
}.build()
val imageCapture = ImageCapture(imageCaptureConfig)
cameraCaptureButton.setOnClickListener {
val fileName = System.currentTimeMillis().toString()
val fileFormat = ".jpg"
val imageFile = createTempFile(fileName, fileFormat)
// Store captured image in the temporary file
capture.takePicture(imageFile, object : ImageCapture.OnImageSavedListener {
override fun onImageSaved(file: File) {
// You may display the image for example using its path file.absolutePath
}
override fun onError(useCaseError: ImageCapture.UseCaseError, message: String, cause: Throwable?) {
// Display error message
}
})
}
return imageCapture
}
private fun getResolution(context: Context): Size {
val displayMetrics = DisplayMetrics()
val windowManager = applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
windowManager.defaultDisplay.getMetrics(displayMetrics)
val screenWidth = displayMetrics.widthPixels
val screenHeight = displayMetrics.heightPixels
return Size(screenWidth, screenHeight)
}
fun buildPreview(context: Context): Preview {
val previewConfig = PreviewConfig.Builder().apply {
setLensFacing(CameraX.LensFacing.BACK)
setTargetResolution(getResolution(context))
setTargetAspectRatio(aspectRatio)
setTargetRotation(rotation)
}.build()
val preview = Preview(previewConfig)
preview.setOnPreviewOutputUpdateListener {
previewOutput ->
val parent = cameraTextureView.parent as ViewGroup
parent.removeView(cameraTextureView)
parent.addView(cameraTextureView, 0)
cameraTextureView.surfaceTexture = previewOutput.surfaceTexture
updateTransform()
}
return preview
}
private fun updateTransform() {
val matrix = Matrix()
val width = cameraTextureView.measuredWidth
val height = cameraTextureView.measuredHeight
val centerX = width.toFloat() / 2
val centerY = height.toFloat() / 2
val rotationDegree = when (cameraTextureView.rotation.toInt()) {
Surface.ROTATION_0 -> 0f
Surface.ROTATION_90 -> 90f
Surface.ROTATION_180 -> 180f
Surface.ROTATION_270 -> 270f
else -> return
}
matrix.postRotate(rotationDegree, centerX, centerY)
cameraTextureView.setTransform(matrix)
}
private fun getResolution(context: Context): Size {
val displayMetrics = DisplayMetrics()
val windowManager = applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
windowManager.defaultDisplay.getMetrics(displayMetrics)
val screenWidth = displayMetrics.widthPixels
val screenHeight = displayMetrics.heightPixels
return Size(screenWidth, screenHeight)
}
private fun startCamera(context: Context): Boolean {
CameraX.unbindAll()
val preview = buildPreview(context)
val imageCapture = buildImageCapture(context)
val imageAnalysis = buildImageAnalysis(context)
CameraX.bindToLifecycle(context, preview, imageCapture, imageAnalysis)
// CameraX.bindToLifecycle(context, preview, imageCapture) no analysis
// CameraX.bindToLifecycle(context, imageCapture, imageAnalysis) no preview
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment