-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Multiplatform ImageViewer desktop camera support #2907
Open
DevSrSouza
wants to merge
3
commits into
JetBrains:master
Choose a base branch
from
DevSrSouza:feat/imageviewer-desktop-camera
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
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
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 |
---|---|---|
|
@@ -30,7 +30,14 @@ compose.desktop { | |
|
||
val iconsRoot = project.file("../common/src/desktopMain/resources/images") | ||
macOS { | ||
iconFile.set(iconsRoot.resolve("icon-mac.icns")) | ||
//iconFile.set(iconsRoot.resolve("icon-mac.icns")) does not exist | ||
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. This was required because it could not find this file. |
||
runtimeEntitlementsFile.set(project.file("runtime-entitlements.plist")) | ||
infoPlist { | ||
extraKeysRawXml = """ | ||
<key>NSCameraUsageDescription</key> | ||
<string>This app uses camera for capturing photos</string> | ||
""".trimIndent() | ||
} | ||
} | ||
windows { | ||
iconFile.set(iconsRoot.resolve("icon-windows.ico")) | ||
|
22 changes: 22 additions & 0 deletions
22
experimental/examples/imageviewer/desktopApp/runtime-entitlements.plist
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,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>com.apple.security.app-sandbox</key> | ||
<true/> | ||
<key>com.apple.security.cs.allow-jit</key> | ||
<true/> | ||
<key>com.apple.security.cs.allow-unsigned-executable-memory</key> | ||
<true/> | ||
<key>com.apple.security.cs.disable-library-validation</key> | ||
<true/> | ||
<key>com.apple.security.cs.allow-dyld-environment-variables</key> | ||
<true/> | ||
<key>com.apple.security.cs.debugger</key> | ||
<true/> | ||
<key>com.apple.security.device.audio-input</key> | ||
<true/> | ||
<key>com.apple.security.device.camera</key> | ||
<true/> | ||
</dict> | ||
</plist> |
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
226 changes: 226 additions & 0 deletions
226
...al/examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/utils/Webcam.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,226 @@ | ||
package example.imageviewer.utils | ||
|
||
import androidx.compose.foundation.* | ||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.material.* | ||
import androidx.compose.runtime.* | ||
import androidx.compose.ui.graphics.* | ||
import com.github.eduramiba.webcamcapture.drivers.NativeDriver | ||
import com.github.sarxos.webcam.* | ||
import com.github.sarxos.webcam.ds.gstreamer.GStreamerDriver | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.coroutines.yield | ||
import org.jetbrains.skia.Bitmap | ||
import org.jetbrains.skia.ColorAlphaType | ||
import org.jetbrains.skia.ImageInfo | ||
import java.awt.Dimension | ||
import java.awt.image.* | ||
import kotlin.math.max | ||
import kotlin.time.ExperimentalTime | ||
import kotlin.time.measureTimedValue | ||
|
||
private var driverInitialized = false | ||
|
||
@Composable | ||
internal fun rememberWebcamListState(): WebcamListState { | ||
val webcamListState = remember { WebcamListState() } | ||
webcamListState.setup() | ||
|
||
return webcamListState | ||
} | ||
|
||
@Stable | ||
internal class WebcamListState { | ||
private val webcamsMutable = mutableStateListOf<Webcam>() | ||
val webcams: List<Webcam> = webcamsMutable | ||
|
||
var isLoading by mutableStateOf(true) | ||
private set | ||
var defaultWebcam: Webcam? by mutableStateOf(null) // null when there is no camera. | ||
private set | ||
|
||
@Composable | ||
fun setup() { | ||
LaunchedEffect(Unit) { | ||
isLoading = true | ||
withContext(Dispatchers.IO) { | ||
// check if running on ARM/MacOS and apply the native driver. | ||
if(driverInitialized.not()) { | ||
val os = System.getProperty("os.name") | ||
val driver = if(os.contains("linux", ignoreCase = true)) { | ||
GStreamerDriver() | ||
} else { | ||
// mac and windows | ||
NativeDriver() | ||
} | ||
|
||
Webcam.setDriver(driver) | ||
driverInitialized = true | ||
} | ||
|
||
Webcam.getWebcams().forEach { webcam -> | ||
webcamsMutable.removeIf { it.name == webcam.name } | ||
webcamsMutable.add(webcam) | ||
} | ||
|
||
defaultWebcam = Webcam.getDefault() | ||
} | ||
isLoading = false | ||
} | ||
|
||
DisposableEffect(Unit) { | ||
val listener = object : WebcamDiscoveryListener { | ||
override fun webcamFound(event: WebcamDiscoveryEvent) { | ||
webcamsMutable.removeIf { it.name == event.webcam.name } | ||
webcamsMutable.add(event.webcam) | ||
} | ||
|
||
override fun webcamGone(event: WebcamDiscoveryEvent) { | ||
webcamsMutable.removeIf { it.name == event.webcam.name } | ||
} | ||
} | ||
Webcam.addDiscoveryListener(listener) | ||
|
||
onDispose { | ||
Webcam.removeDiscoveryListener(listener) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
internal fun rememberWebcamState(webcam: Webcam): WebcamState { | ||
val state = remember { | ||
webcam.viewSize = Dimension(1280, 720) | ||
WebcamState(webcam) | ||
} | ||
state.setup() | ||
|
||
return state | ||
} | ||
|
||
@Stable | ||
internal class WebcamState(webcam: Webcam) { | ||
var webcam by mutableStateOf(webcam) | ||
var resolutions by mutableStateOf(webcam.viewSizes.toList()) | ||
private set | ||
|
||
var currentWebcamResolution by mutableStateOf(webcam.viewSize) | ||
|
||
var lastFrame by mutableStateOf<ImageBitmap?>(null) | ||
private set | ||
|
||
var fpsLimitation: Int? by mutableStateOf(30) // if null, it will try the most FPS the computer can take. | ||
|
||
var timeSpentPerFrame: Long by mutableStateOf(0L) | ||
private set | ||
|
||
@OptIn(ExperimentalTime::class) | ||
@Composable | ||
fun setup() { | ||
val coroutineScope = rememberCoroutineScope() | ||
DisposableEffect(webcam) { | ||
coroutineScope.launch(Dispatchers.IO) { | ||
webcam.lock.disable() | ||
webcam.open(true) | ||
} | ||
onDispose { | ||
webcam.close() | ||
} | ||
} | ||
|
||
LaunchedEffect(webcam) { | ||
resolutions = webcam.viewSizes.toList() | ||
} | ||
|
||
LaunchedEffect(currentWebcamResolution) { | ||
requiredClose(webcam) { | ||
viewSize = currentWebcamResolution | ||
} | ||
} | ||
|
||
LaunchedEffect(webcam) { | ||
withContext(Dispatchers.IO) { | ||
while (true) { | ||
val (imageBitmap, measure) = measureTimedValue { | ||
webcam.imageBitmap() | ||
} | ||
if (imageBitmap != null) { | ||
lastFrame = imageBitmap | ||
timeSpentPerFrame = measure.inWholeMilliseconds | ||
|
||
if(fpsLimitation != null) { | ||
delay(max(fpsLimitation!! - timeSpentPerFrame, 1)) | ||
} | ||
} else { | ||
delay(50) // delay until pickup camera again | ||
} | ||
yield() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun requiredClose(webcam: Webcam, apply: Webcam.() -> Unit) { | ||
val listener = object : WebcamListener { | ||
override fun webcamOpen(we: WebcamEvent) { | ||
we.source.removeWebcamListener(this) | ||
} | ||
|
||
override fun webcamClosed(we: WebcamEvent) { | ||
we.source.apply() | ||
we.source.open() | ||
} | ||
|
||
override fun webcamDisposed(we: WebcamEvent?) {} | ||
|
||
override fun webcamImageObtained(we: WebcamEvent?) {} | ||
|
||
} | ||
|
||
webcam.addWebcamListener(listener) | ||
webcam.close() | ||
} | ||
|
||
private fun Webcam.imageBitmap(): ImageBitmap? { | ||
val buffer = imageBytes ?: return null | ||
val arrayBuffer = ByteArray(buffer.capacity()) | ||
|
||
|
||
val width = viewSize.width | ||
val height = viewSize.height | ||
|
||
val bytesPerPixel = 4 | ||
val pixels = ByteArray(width * height * bytesPerPixel) | ||
|
||
buffer.mark() | ||
buffer.position(0) | ||
buffer.get(arrayBuffer, 0, buffer.capacity()) | ||
buffer.reset() | ||
|
||
// d15f35 -> 3558c7ff | ||
// to not go through BufferedImage.toComposeImageBitmap | ||
// instead we map directly de sRGB from Webcam Image Buffer | ||
// this is way faster, probably there is faster approach. | ||
var k = 0 | ||
for (i in 0 until buffer.capacity() step 3) { | ||
val r = arrayBuffer.get(i) | ||
val g = arrayBuffer.get(i + 1) | ||
val b = arrayBuffer.get(i + 2) | ||
pixels[k++] = b | ||
pixels[k++] = g | ||
pixels[k++] = r | ||
pixels[k++] = 0xff.toByte() | ||
} | ||
|
||
val bitmap = Bitmap() | ||
|
||
bitmap.allocPixels(ImageInfo.makeS32(width, height, ColorAlphaType.UNPREMUL)) | ||
bitmap.installPixels(pixels) | ||
|
||
return bitmap.asComposeImageBitmap() | ||
} |
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.
We can't add jitpack dependencies because of potential security risks.
Also, we can't add this dependency as a jar, because it is not convenient.