-
Notifications
You must be signed in to change notification settings - Fork 0
fix: 시스템 권한 팝업에서 허용 시 카메라 프리뷰 안보이는 문제 수정 #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -34,7 +34,10 @@ import androidx.compose.material3.Text | |
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.DisposableEffect | ||
| import androidx.compose.runtime.LaunchedEffect | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.graphics.Color | ||
|
|
@@ -89,11 +92,19 @@ private fun CameraPreview( | |
| val lifecycleOwner = LocalLifecycleOwner.current | ||
| val permission = android.Manifest.permission.CAMERA | ||
|
|
||
| // UI에서 항상 권한 최신 상태 확인 | ||
| val isGranted = ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED | ||
| val launcher = rememberLauncherForActivityResult( | ||
| /** | ||
| * Camera Permission Request | ||
| */ | ||
| var isGranted by remember { | ||
| mutableStateOf( | ||
| ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED, | ||
| ) | ||
| } | ||
| val permissionLauncher = rememberLauncherForActivityResult( | ||
| contract = ActivityResultContracts.RequestPermission(), | ||
| ) { granted -> | ||
| isGranted = granted | ||
|
|
||
| if (!granted) { | ||
| state.eventSink(OcrUiEvent.OnShowPermissionDialog) | ||
| } | ||
|
|
@@ -102,45 +113,20 @@ private fun CameraPreview( | |
| contract = ActivityResultContracts.StartActivityForResult(), | ||
| ) { _ -> } | ||
|
|
||
| val cameraController = remember { LifecycleCameraController(context) } | ||
| val imageAnalyzer = remember { | ||
| ImageAnalysis.Analyzer { imageProxy -> | ||
| state.eventSink(OcrUiEvent.OnFrameReceived(imageProxy)) | ||
| } | ||
| } | ||
|
|
||
| val systemUiController = rememberSystemUiController() | ||
|
|
||
| DisposableEffect(systemUiController) { | ||
| systemUiController.setSystemBarsColor( | ||
| color = Color.Transparent, | ||
| darkIcons = false, | ||
| isNavigationBarContrastEnforced = false, | ||
| ) | ||
|
|
||
| onDispose { | ||
| systemUiController.setSystemBarsColor( | ||
| color = Color.Transparent, | ||
| darkIcons = true, | ||
| isNavigationBarContrastEnforced = false, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // 최초 진입 시 권한 요청 | ||
| LaunchedEffect(Unit) { | ||
| if (!isGranted) { | ||
| state.eventSink(OcrUiEvent.OnHidePermissionDialog) | ||
| launcher.launch(permission) | ||
| permissionLauncher.launch(permission) | ||
| } | ||
| } | ||
|
|
||
| // 앱이 포그라운드로 북귀할 때 OS 권한 체크 | ||
| // 앱이 포그라운드로 북귀할 때 OS 권한 동기화 | ||
| DisposableEffect(Unit) { | ||
| val observer = LifecycleEventObserver { _, event -> | ||
| if (event == Lifecycle.Event.ON_RESUME) { | ||
| val currentGrant = ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED | ||
| if (currentGrant) { | ||
| isGranted = ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED | ||
| if (isGranted) { | ||
| state.eventSink(OcrUiEvent.OnHidePermissionDialog) | ||
| } else { | ||
| state.eventSink(OcrUiEvent.OnShowPermissionDialog) | ||
|
|
@@ -151,18 +137,52 @@ private fun CameraPreview( | |
| onDispose { lifecycleOwner.lifecycle.removeObserver(observer) } | ||
| } | ||
|
|
||
| DisposableEffect(lifecycleOwner, cameraController) { | ||
| cameraController.bindToLifecycle(lifecycleOwner) | ||
| cameraController.setImageAnalysisAnalyzer( | ||
| ContextCompat.getMainExecutor(context), | ||
| imageAnalyzer, | ||
| ) | ||
| /** | ||
| * Camera Controller & ImageAnalyzer | ||
| */ | ||
| val cameraController = remember { LifecycleCameraController(context) } | ||
| val imageAnalyzer = remember { | ||
| ImageAnalysis.Analyzer { imageProxy -> | ||
| state.eventSink(OcrUiEvent.OnFrameReceived(imageProxy)) | ||
| } | ||
| } | ||
|
|
||
| DisposableEffect(isGranted, lifecycleOwner, cameraController) { | ||
| if (isGranted) { | ||
| cameraController.bindToLifecycle(lifecycleOwner) | ||
| cameraController.setImageAnalysisAnalyzer( | ||
| ContextCompat.getMainExecutor(context), | ||
| imageAnalyzer, | ||
| ) | ||
| } | ||
|
Comment on lines
+150
to
+157
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 이미지 분석을 메인 스레드가 아닌 전용 Executor에서 실행하세요 현재 MainExecutor로 분석하면 OCR 로직이 UI 스레드를 점유해 프레임 드랍 가능성이 큽니다. 전용 싱글 스레드 Executor 사용을 권장합니다. 이 변경으로 권한 허용 직후에도 안정적으로 프리뷰+분석이 동작합니다. 적용 diff(실행 스레드 전환): @@
- DisposableEffect(isGranted, lifecycleOwner, cameraController) {
+ DisposableEffect(isGranted, lifecycleOwner, cameraController) {
if (isGranted) {
- cameraController.bindToLifecycle(lifecycleOwner)
- cameraController.setImageAnalysisAnalyzer(
- ContextCompat.getMainExecutor(context),
- imageAnalyzer,
- )
+ cameraController.bindToLifecycle(lifecycleOwner)
+ // 전용 Executor 사용 권장 (아래 cameraExecutor 참고)
+ cameraController.setImageAnalysisAnalyzer(
+ cameraExecutor,
+ imageAnalyzer,
+ )
}추가 코드(해당 블록 위/아래에 배치): // 상단 import 추가
import java.util.concurrent.Executors
// controller 바로 아래에 배치
val cameraExecutor = remember { Executors.newSingleThreadExecutor() }
// onDispose 블록 내 자원 정리
onDispose {
cameraController.unbind()
cameraController.clearImageAnalysisAnalyzer()
cameraExecutor.shutdown()
}원하시면 위 변경을 반영한 전체 패치를 준비해드리겠습니다. 🤖 Prompt for AI Agents |
||
|
|
||
| onDispose { | ||
| cameraController.unbind() | ||
| cameraController.clearImageAnalysisAnalyzer() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * SystemStatusBar Color | ||
| */ | ||
| val systemUiController = rememberSystemUiController() | ||
|
|
||
| DisposableEffect(systemUiController) { | ||
| systemUiController.setSystemBarsColor( | ||
| color = Color.Transparent, | ||
| darkIcons = false, | ||
| isNavigationBarContrastEnforced = false, | ||
| ) | ||
|
|
||
| onDispose { | ||
| systemUiController.setSystemBarsColor( | ||
| color = Color.Transparent, | ||
| darkIcons = true, | ||
| isNavigationBarContrastEnforced = false, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| ReedScaffold( | ||
| modifier = modifier.fillMaxSize(), | ||
| containerColor = Neutral950, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Analyzer에서 imageProxy.close() 보장 필요 + eventSink 참조 최신화
권장 diff(참고: 비동기 처리라면 finally에서 close하지 말고, 소비 측에서 완료 시 close):
검증 스크립트(소비 측에서 close 호출 여부 확인):
🏁 Script executed:
Length of output: 9435
🏁 Script executed:
Length of output: 1372
Analyzer에서 close 처리 확인 및 remember(key) 반영 요청
remember블록에state.eventSink를 키로 포함하지 않으면,eventSink가 변경되어도 람다에 최신 참조가 반영되지 않습니다. 아래 예시처럼 수정해 주세요.📝 Committable suggestion
🤖 Prompt for AI Agents