forked from FoKE-Developers/FourCutTogether
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement internal camera functionality with capture and previe…
…w use cases [FoKE-Developers#45]
- Loading branch information
Showing
8 changed files
with
193 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
domain/src/main/java/com/foke/together/domain/interactor/CaptureWithInternalCameraUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.foke.together.domain.interactor | ||
|
||
import android.content.Context | ||
import com.foke.together.domain.output.ImageRepositoryInterface | ||
import com.foke.together.domain.output.InternalCameraRepositoryInterface | ||
import com.foke.together.util.AppLog | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import javax.inject.Inject | ||
|
||
class CaptureWithInternalCameraUseCase @Inject constructor( | ||
private val internalCameraRepository: InternalCameraRepositoryInterface, | ||
private val imageRepository: ImageRepositoryInterface | ||
) { | ||
suspend operator fun invoke( | ||
context: Context, | ||
fileName: String | ||
): Result<Unit>{ | ||
internalCameraRepository.capture(context) | ||
.onSuccess { | ||
AppLog.i(TAG, "capture", "success: $it") | ||
imageRepository.cachingImage(it, fileName) | ||
return Result.success(Unit) | ||
} | ||
.onFailure { | ||
AppLog.i(TAG, "capture", "failure: $it") | ||
return Result.failure(it) | ||
} | ||
return Result.failure(Exception("Unknown error")) | ||
} | ||
|
||
companion object { | ||
private val TAG = CaptureWithInternalCameraUseCase::class.java.simpleName | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
domain/src/main/java/com/foke/together/domain/interactor/GetInternalCameraPreviewUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.foke.together.domain.interactor | ||
|
||
import androidx.camera.view.PreviewView | ||
import androidx.lifecycle.LifecycleOwner | ||
import com.foke.together.domain.output.ExternalCameraRepositoryInterface | ||
import com.foke.together.domain.output.InternalCameraRepositoryInterface | ||
import javax.inject.Inject | ||
|
||
class GetInternalCameraPreviewUseCase @Inject constructor( | ||
private val internalCameraRepository: InternalCameraRepositoryInterface | ||
) { | ||
suspend operator fun invoke( | ||
previewView: PreviewView, | ||
lifecycleOwner: LifecycleOwner | ||
) = internalCameraRepository.showCameraPreview( | ||
previewView, | ||
lifecycleOwner | ||
) | ||
} |
14 changes: 14 additions & 0 deletions
14
domain/src/main/java/com/foke/together/domain/output/InternalCameraRepositoryInterface.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.foke.together.domain.output | ||
|
||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import androidx.camera.view.PreviewView | ||
import androidx.lifecycle.LifecycleOwner | ||
|
||
interface InternalCameraRepositoryInterface { | ||
suspend fun capture(context: Context): Result<Bitmap> | ||
suspend fun showCameraPreview( | ||
previewView: PreviewView, | ||
lifecycleOwner: LifecycleOwner | ||
) | ||
} |
5 changes: 0 additions & 5 deletions
5
external/src/main/java/com/foke/together/external/camera/internal/AndroidCameraDataSource.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
external/src/main/java/com/foke/together/external/repository/InternalCameraRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.foke.together.external.repository | ||
|
||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import androidx.camera.core.CameraSelector | ||
import androidx.camera.core.ImageAnalysis | ||
import androidx.camera.core.ImageCapture | ||
import androidx.camera.core.ImageCaptureException | ||
import androidx.camera.core.ImageProxy | ||
import androidx.camera.core.Preview | ||
import androidx.camera.lifecycle.ProcessCameraProvider | ||
import androidx.camera.view.PreviewView | ||
import androidx.core.content.ContextCompat | ||
import androidx.lifecycle.LifecycleOwner | ||
import com.foke.together.domain.output.InternalCameraRepositoryInterface | ||
import com.foke.together.util.AppLog | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import javax.inject.Inject | ||
|
||
class InternalCameraRepository @Inject constructor( | ||
@ApplicationContext private val context: Context, | ||
private val cameraProvider: ProcessCameraProvider, | ||
private val selector : CameraSelector, | ||
private val preview: Preview, | ||
private val imageAnalysis: ImageAnalysis, | ||
private val imageCapture: ImageCapture | ||
|
||
): InternalCameraRepositoryInterface{ | ||
|
||
override suspend fun capture(context: Context): Result<Bitmap> { | ||
var imageBitmap : Bitmap? = null | ||
imageCapture.takePicture( | ||
ContextCompat.getMainExecutor(context), | ||
object : ImageCapture.OnImageCapturedCallback() { | ||
override fun onCaptureSuccess(imageProxy: ImageProxy) { | ||
imageBitmap = imageProxy.toBitmap() | ||
} | ||
|
||
override fun onError(exception: ImageCaptureException) { | ||
imageBitmap = null | ||
} | ||
} | ||
) | ||
return if(imageBitmap != null){ | ||
Result.success(imageBitmap!!) | ||
} else{ | ||
Result.failure(Exception("Unknown error")) | ||
} | ||
} | ||
|
||
override suspend fun showCameraPreview( | ||
previewView: PreviewView, | ||
lifecycleOwner: LifecycleOwner | ||
) { | ||
try{ | ||
cameraProvider.unbindAll() | ||
cameraProvider.bindToLifecycle( | ||
lifecycleOwner, | ||
selector, | ||
preview, | ||
imageAnalysis, | ||
imageCapture | ||
) | ||
} | ||
catch (e: Exception){ | ||
AppLog.e(TAG,"showCameraPreview", e.message!!) | ||
} | ||
} | ||
|
||
companion object { | ||
private val TAG = InternalCameraRepository::class.java.simpleName | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters