Android CameraX

Android Jetpack CameraX

특징

최소 요구 사항

UseCase

CameraX 장점


그래들 의존성 추가

    def camerax_version = "1.0.0-alpha01"
    implementation "androidx.camera:camera-core:${camerax_version}"
    implementation "androidx.camera:camera-camera2:${camerax_version}"

화면 미리보기

    val previewConfig = PreviewConfig.Builder().build()
    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)

이미지 분석

    // configure image analysis
    // set resolution
    val imageAnalysisConfig = ImageAnalysisConfig.Builder()
    .setTargetResolution(Size(1280, 720))
    .build()
    val imageAnalysis = ImageAnalysis(imageAnalysisConfig)
    // attach output
    imageAnalysis.setAnalyzer({ image : ImageProxy, rotationDegrees : Int ->
    val cropRect = image.cropRect
    // insert your code here
    })
    CameraX.bindToLifecycle(this as LifecycleOwner, imageAnalysis, preview)

이미지 촬영

    // configure image capture
    val imageCaptureConfig = ImageCaptureConfig.Builder()
    .setTargetRotation(windowManager.defaultDisplay.rotation)
    .build()
    val imageCapture = ImageCapture(imageCaptureConfig)
    CameraX.bindToLifecycle(this as LifecycleOwner, imageCapture, imageAnalysis, preview)

이미지 저장

    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
    }
    })

참조

Getting Started with CameraX 찰스의 안드로이드-Android CameraX 라이브러리 미리보기

Comments