728x90

 

Title: Android Jetpack: Understand the CameraX Camera-Support Library (Google I/O'19)

Speaker(s): Vinit Modi, James Fung, Franklin Wu, Trevor McGuire

 

 

 

1. 카메라 개발은 어렵습니다. 이유는?

- 앱에서 다양한 OS별로 신경써야함

- 저가형 entry부터 고가의 flagship까지 일관성이 있어야함

- 카메라 API의 복잡성

 

 

2. (testlab에서 테스트 다 하면서 개발했다는) CamaraX

 - 현 시장의 90%에 가까운 기기에서 호환됨

 - target SDK가 API 21 이상이면 사용 가능

 - Camera1 Api 및 Camera2 Legay Layer와 일관되게 제공됨.

 - 사용하기 쉬워짐

 - Lifecycle aware: Preview, Image Analysis, Caputre 기능 -> 각 개별 thread 필요 없어짐

 - 

CameraX 이전, CameraX 이후 동작

 

3. 사용 예제

 

1) Preview

/*display preview on screen*/

//configure preview
val previewConfig = PreviewConfig.Builder().build()

//create preview
val preview = Preview(previewConfig)

preview.setOnPreviewOutputUpdateListener {
    PreviewOutput : Preview.PreviewOutput? ->
    // your code here. e.g. use previewOutput?.surfaceTexture
    // and post to a GL renderer.
}

//attach preview to lifecycle
CameraX.bindToLifecycle(this as LifecycleOwner, preview)

 

2) Image Analysis

 

//configure image analysis

//set resolution
val imageAnalysisConfig = ImageAnalysisConfig.Builder()
    .setTargetResolution(Size(1280, 720))
    .build()

//create image analysis
val imageAnalysis = ImageAnalysis(imageAnalysisConfig)

//attach output
imageAnalysis.setAnalyzer({ image : ImageProxy, rotationDegrees : Int ->
    val cropRect = image.cropRect
    //insert your code here
})

//attach image analysis & preview to lifecycle
CameraX.bindToLifecycle(this as LifecycleOwner, imageAnalysis, preview)

 

 

 

 3) Image Capture

 

/*configure image capture*/

//manage rotation
val imageCaptureConfig = ImageCaptureConfig.Builder()
    .setTargetRotation(windowManager.defaultDisplay.rotation)
    .build()

//create image capture
val imageCapture = ImageCapture(imageCaptureConfig)

//bind all use cases
CameraX.bindToLifecycle(this as LifecycleOwner, imageCapture, imageAnalysis, preview)

 

//on user action save a picture
fun onClick() {
    val file = File(...)
    imageCapture.takePicture(file,
        object : ImageCapture.OnImageSavedListener {
            override fun onError(error: ImageCapture.UserCaseError,
                                message: String, exc: Throwable?){
                // insert your code here 
            }
            override fun onImageSaved(file: File) {
                // insert your code here
            }
        })
}

 

 

 

Reference

https://www.youtube.com/watch?v=kuv8uK-5CLY

 

728x90

+ Recent posts