Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CameraX使ってみた

 CameraX使ってみた

Takuji Nishibayashi

July 31, 2024
Tweet

More Decks by Takuji Nishibayashi

Other Decks in Technology

Transcript

  1. 自己紹介 西林 拓志 (にしばやし たくじ ) Twitter/GitHub takuji31 Sansan 株式会社

    技術本部 Mobile Application グループ Eight Android テックリード Android (2009〜 ) Kotlin (2014〜 ) 1
  2. libs.versions.toml [versions] camerax = "1.4.0-beta02" [libraries] androidx-camera-camera2 = { module

    = "androidx.camera:camera-camera2", version.ref = "camerax" } androidx-camera-lifecycle = { module = "androidx.camera:camera-lifecycle", version.ref = "camerax" } androidx-camera-view = { module = "androidx.camera:camera-view", version.ref = "camerax" } androidx-camera-extensions = { module = "androidx.camera:camera-extensions", version.ref = "camerax" } 12
  3. 使い方 val controller = LifecycleCameraController(this) controller.bindToLifecycle(this) controller.cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA setContent

    { AndroidView( factory = { context -> PreviewView(context).apply { layoutParams = ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT ) setController(controller) } }, onRelease = { controller.unbind() } ) } 15
  4. 16

  5. 撮影 val values = ContentValues().apply { put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg") } val

    imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values) controller.takePicture( ImageCapture .OutputFileOptions .Builder( contentResolver.openOutputStream(imageUri!!)!! ).build(), ContextCompat.getMainExecutor(this), object : ImageCapture.OnImageSavedCallback { override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) { Toast.makeText(this, "Image captured", Toast.LENGTH_SHORT).show() } override fun onError(exception: ImageCaptureException) { Toast.makeText(this, "Image capture failed", Toast.LENGTH_SHORT).show() } }) 20
  6. 拡張機能 val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA val mainExecutor = ContextCompat.getMainExecutor(this) val

    cameraProviderFuture = ProcessCameraProvider.getInstance(this) val imageCapture = ImageCapture.Builder().build() val preview = CameraXPreview.Builder().build() 23
  7. 拡張機能 extensionsManagerFuture.addListener({ val extensionsManager = extensionsManagerFuture.get() if (extensionsManager.isExtensionAvailable(cameraSelector, ExtensionMode.NIGHT)) {

    val newSelector = extensionsManager.getExtensionEnabledCameraSelector( cameraSelector, ExtensionMode.NIGHT ) preview.surfaceProvider = findViewById<PreviewView>(R.id.previewView).surfaceProvider cameraProvider.bindToLifecycle(this, newSelector, imageCapture, preview) } }, mainExecutor) 25