Skip to content

Commit

Permalink
wip kmm
Browse files Browse the repository at this point in the history
  • Loading branch information
ujizin committed Oct 14, 2023
1 parent 03bd85d commit 11188c5
Show file tree
Hide file tree
Showing 182 changed files with 1,111 additions and 194 deletions.
Empty file modified .github/ISSUE_TEMPLATE/bug_report.md
100644 → 100755
Empty file.
Empty file modified .github/ISSUE_TEMPLATE/feature_request.md
100644 → 100755
Empty file.
Empty file modified .github/workflows/android_test.yml
100644 → 100755
Empty file.
Empty file modified .github/workflows/build.yml
100644 → 100755
Empty file.
Empty file modified .github/workflows/documentation.yml
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified License
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
7 changes: 5 additions & 2 deletions build.gradle.kts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ plugins {
alias(libs.plugins.application) apply false
alias(libs.plugins.library) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.multiplatform) apply false
alias(libs.plugins.gradle.nexus)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.dokka)
// alias(libs.plugins.dokka)
alias(libs.plugins.multiplatform.compose)
}

apply(from = "${rootDir}/scripts/publish-root.gradle")
apply(from = "${rootDir}/scripts/publish-root.gradle")

Empty file modified buildSrc/build.gradle.kts
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/ujizin/camposer/Config.kt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.ujizin.camposer

object Config {
const val compileSdk = 33
const val targetSdk = 33
const val compileSdk = 34
const val targetSdk = 34
const val minSdk = 21
const val versionCode = 6
const val versionName = "0.3.0"
Expand Down
Empty file modified camposer/.gitignore
100644 → 100755
Empty file.
99 changes: 67 additions & 32 deletions camposer/build.gradle.kts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import com.ujizin.camposer.Config

plugins {
kotlin("multiplatform")
// id("org.jetbrains.dokka")
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("org.jetbrains.dokka")
id("org.jetbrains.compose")
}

extra.apply {
Expand All @@ -12,51 +13,85 @@ extra.apply {
set("PUBLISH_VERSION", Config.versionName)
}

apply(from = "${rootDir}/scripts/publish-module.gradle")
// apply(from = "${rootDir}/scripts/publish-module.gradle")

android {
namespace = "com.ujizin.camposer"
compileSdk = Config.compileSdk
defaultConfig {
minSdk = Config.minSdk
targetSdk = Config.targetSdk
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = libs.versions.compose.compiler.get()
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

kotlinOptions {
freeCompilerArgs += "-Xexplicit-api=strict"
}
}

tasks.dokkaHtml.configure {
dokkaSourceSets {
named("main") {
noAndroidSdkLink.set(false)
kotlin {
targetHierarchy.default()


androidTarget {
compilations.all {
kotlinOptions {
freeCompilerArgs += "-Xexplicit-api=strict"
}
}
}
}

dependencies {
implementation(platform(libs.compose.bom))
implementation(libs.bundles.compose)
implementation(libs.lifecycle)
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach {
it.compilations.all {
kotlinOptions {
freeCompilerArgs += "-Xexplicit-api=strict"
}
}
it.binaries.framework { baseName = "camposer" }
}

api(libs.bundles.internal.camerax)
sourceSets {
val commonMain by getting {
dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
// implementation(compose.desktop.uiTestJUnit4)
}
}

androidTestImplementation(libs.androidx.test.rules)
androidTestImplementation(platform(libs.compose.bom))
androidTestImplementation(libs.compose.junit)
val androidMain by getting {
dependencies {
implementation(libs.lifecycle)
api(libs.bundles.internal.camerax)
}
}

// val iosX64Main by getting
// val iosArm64Main by getting
// val iosSimulatorArm64Main by getting
// val iosMain by creating {
// dependsOn(commonMain)
// iosX64Main.dependsOn(this)
// iosArm64Main.dependsOn(this)
// iosSimulatorArm64Main.dependsOn(this)
// }
}
}
//tasks.dokkaHtml.configure {
// dokkaSourceSets {
// named("main") {
// noAndroidSdkLink.set(false)
// }
// }
//}
Empty file modified camposer/consumer-rules.pro
100644 → 100755
Empty file.
Empty file modified camposer/proguard-rules.pro
100644 → 100755
Empty file.
File renamed without changes.
95 changes: 32 additions & 63 deletions ...java/com/ujizin/camposer/CameraPreview.kt → ...tlin/com/ujizin/camposer/CameraPreview.kt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.Lifecycle
import com.ujizin.camposer.extensions.clamped
import com.ujizin.camposer.extensions.onCameraTouchEvent
import com.ujizin.camposer.focus.FocusTap
import com.ujizin.camposer.focus.SquareCornerFocus
import com.ujizin.camposer.state.CamSelector
import com.ujizin.camposer.state.CameraState
import com.ujizin.camposer.state.CaptureMode
Expand All @@ -29,66 +30,33 @@ import com.ujizin.camposer.state.ImageCaptureMode
import com.ujizin.camposer.state.ImageTargetSize
import com.ujizin.camposer.state.ImplementationMode
import com.ujizin.camposer.state.ScaleType
import com.ujizin.camposer.state.rememberCameraState
import kotlinx.coroutines.delay
import androidx.camera.core.CameraSelector as CameraXSelector

/**
* Creates a Camera Preview's composable.
*
* @param cameraState camera state hold some states and camera's controller, it can be useful to given action like [CameraState.takePicture]
* @param camSelector camera selector to be added, default is back
* @param captureMode camera capture mode, default is image
* @param imageCaptureMode camera image capture mode, default is minimum latency for better performance
* @param imageCaptureTargetSize suggested target size for image camera capture, default is camera's preferred size
* @param flashMode flash mode to be added, default is off
* @param scaleType scale type to be added, default is fill center
* @param enableTorch enable torch from camera, default is false.
* @param exposureCompensation camera exposure compensation to be added
* @param zoomRatio zoom ratio to be added, default is 1.0
* @param imageAnalyzer image analyzer from camera, see [ImageAnalyzer]
* @param implementationMode implementation mode to be added, default is performance
* @param isImageAnalysisEnabled enable or disable image analysis
* @param isFocusOnTapEnabled turn on feature focus on tap if true
* @param isPinchToZoomEnabled turn on feature pinch to zoom if true
* @param onPreviewStreamChanged dispatch when preview is switching to front or back
* @param onSwitchToFront composable preview when change camera to front and it's not been streaming yet
* @param onSwitchToBack composable preview when change camera to back and it's not been streaming yet
* @param onZoomRatioChanged dispatch when zoom is changed by pinch to zoom
* @param focusTapContent content of focus tap, default is [SquareCornerFocus]
* @param onFocus callback to use when on focus tap is triggered, call onComplete to [focusTapContent] gone.
* @param content content composable within of camera preview.
* @see ImageAnalyzer
* @see CameraState
* */
@Composable
public fun CameraPreview(
modifier: Modifier = Modifier,
cameraState: CameraState = rememberCameraState(),
camSelector: CamSelector = cameraState.camSelector,
captureMode: CaptureMode = cameraState.captureMode,
imageCaptureMode: ImageCaptureMode = cameraState.imageCaptureMode,
imageCaptureTargetSize: ImageTargetSize? = cameraState.imageCaptureTargetSize,
flashMode: FlashMode = cameraState.flashMode,
scaleType: ScaleType = cameraState.scaleType,
enableTorch: Boolean = cameraState.enableTorch,
exposureCompensation: Int = cameraState.initialExposure,
zoomRatio: Float = 1F,
imageAnalyzer: ImageAnalyzer? = null,
implementationMode: ImplementationMode = cameraState.implementationMode,
isImageAnalysisEnabled: Boolean = cameraState.isImageAnalysisEnabled,
isFocusOnTapEnabled: Boolean = cameraState.isFocusOnTapEnabled,
isPinchToZoomEnabled: Boolean = cameraState.isZoomSupported,
onPreviewStreamChanged: () -> Unit = {},
onSwitchToFront: @Composable (Bitmap) -> Unit = {},
onSwitchToBack: @Composable (Bitmap) -> Unit = {},
onFocus: suspend (onComplete: () -> Unit) -> Unit = { onComplete ->
delay(1000L)
onComplete()
},
onZoomRatioChanged: (Float) -> Unit = {},
focusTapContent: @Composable () -> Unit = { SquareCornerFocus() },
content: @Composable () -> Unit = {},
public actual fun CameraPreview(
modifier: Modifier,
cameraState: CameraState,
camSelector: CamSelector,
captureMode: CaptureMode,
imageCaptureMode: ImageCaptureMode,
imageCaptureTargetSize: ImageTargetSize?,
flashMode: FlashMode,
scaleType: ScaleType,
enableTorch: Boolean,
exposureCompensation: Int,
zoomRatio: Float,
imageAnalyzer: ImageAnalyzer?,
implementationMode: ImplementationMode,
isImageAnalysisEnabled: Boolean,
isFocusOnTapEnabled: Boolean,
isPinchToZoomEnabled: Boolean,
onPreviewStreamChanged: () -> Unit,
onSwitchToFront: @Composable (ImageBitmap) -> Unit,
onSwitchToBack: @Composable (ImageBitmap) -> Unit,
onFocus: suspend (onComplete: () -> Unit) -> Unit,
onZoomRatioChanged: (Float) -> Unit,
focusTapContent: @Composable () -> Unit,
content: @Composable () -> Unit,
) {
CameraPreviewImpl(
modifier = modifier,
Expand Down Expand Up @@ -139,8 +107,8 @@ internal fun CameraPreviewImpl(
onZoomRatioChanged: (Float) -> Unit,
onPreviewStreamChanged: () -> Unit,
onFocus: suspend (() -> Unit) -> Unit,
onSwipeToFront: @Composable (Bitmap) -> Unit,
onSwipeToBack: @Composable (Bitmap) -> Unit,
onSwipeToFront: @Composable (ImageBitmap) -> Unit,
onSwipeToBack: @Composable (ImageBitmap) -> Unit,
focusTapContent: @Composable () -> Unit,
content: @Composable () -> Unit
) {
Expand Down Expand Up @@ -214,9 +182,10 @@ internal fun CameraPreviewImpl(

if (isCameraIdle) {
latestBitmap?.let {
val imageBitmap = it.asImageBitmap()
when (camSelector.selector.lensFacing) {
CameraXSelector.LENS_FACING_FRONT -> onSwipeToFront(it)
CameraXSelector.LENS_FACING_BACK -> onSwipeToBack(it)
CameraXSelector.LENS_FACING_FRONT -> onSwipeToFront(imageBitmap)
CameraXSelector.LENS_FACING_BACK -> onSwipeToBack(imageBitmap)
else -> Unit
}
LaunchedEffect(latestBitmap) {
Expand All @@ -227,4 +196,4 @@ internal fun CameraPreviewImpl(
}

content()
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,12 @@ import androidx.compose.runtime.saveable.listSaver
* @param selector internal camera selector from CameraX
* @see CameraSelector
* */
public enum class CamSelector(
public actual enum class CamSelector(
internal val selector: CameraSelector
) {
Front(CameraSelector.DEFAULT_FRONT_CAMERA),
Back(CameraSelector.DEFAULT_BACK_CAMERA);

/**
* Inverse camera selector. Works only with default Front & Back Selector.
* */
public val inverse: CamSelector
get() = when (this) {
Front -> Back
Back -> Front
}

internal companion object {

@SuppressLint("RestrictedApi")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.ujizin.camposer.CameraPreview
* Camera State from [CameraPreview] Composable.
* */
@Composable
public fun rememberCameraState(): CameraState {
public actual fun rememberCameraState(): CameraState {
val context = LocalContext.current
return remember { CameraState(context) }
}
Expand Down
Loading

0 comments on commit 11188c5

Please sign in to comment.