-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
5,184 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl | ||
|
||
plugins { | ||
alias(libs.plugins.kotlin.multiplatform) apply true | ||
alias(libs.plugins.android.library) apply true | ||
alias(libs.plugins.compose.multiplatform) apply true | ||
|
||
id("convention.publication") apply true | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
google() | ||
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev") | ||
} | ||
|
||
group = "in.procyk.compose" | ||
version = libs.versions.compose.extensions.get() | ||
|
||
kotlin { | ||
jvm("desktop") | ||
|
||
androidTarget { | ||
publishLibraryVariants("release") | ||
} | ||
iosX64() | ||
iosArm64() | ||
iosSimulatorArm64() | ||
|
||
@OptIn(ExperimentalWasmDsl::class) | ||
wasmJs { | ||
browser() | ||
} | ||
|
||
applyDefaultHierarchyTemplate() | ||
|
||
sourceSets { | ||
commonMain.dependencies { | ||
implementation(compose.ui) | ||
implementation(compose.runtime) | ||
implementation(compose.foundation) | ||
} | ||
val wasmJsMain by getting | ||
val desktopMain by getting | ||
create("nonAndroidMain") { | ||
dependsOn(commonMain.get()) | ||
|
||
wasmJsMain.dependsOn(this) | ||
desktopMain.dependsOn(this) | ||
iosMain.get().dependsOn(this) | ||
} | ||
} | ||
} | ||
|
||
android { | ||
compileSdk = libs.versions.android.compileSdk.get().toInt() | ||
namespace = "in.procyk.compose.qrcode" | ||
|
||
defaultConfig { | ||
minSdk = libs.versions.android.minSdk.get().toInt() | ||
} | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
} | ||
kotlin { | ||
jvmToolchain(17) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
qr-code/src/androidMain/kotlin/in/procyk/compose/qrcode/PlatformByteArray.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,16 @@ | ||
package `in`.procyk.compose.qrcode | ||
|
||
import android.graphics.Bitmap | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.asAndroidBitmap | ||
import java.io.ByteArrayOutputStream | ||
|
||
actual fun ImageBitmap.toByteArray(format: ImageFormat): ByteArray = ByteArrayOutputStream().use { | ||
asAndroidBitmap().compress(format.toCompressFormat(), 100, it) | ||
it.toByteArray() | ||
} | ||
|
||
private fun ImageFormat.toCompressFormat(): Bitmap.CompressFormat = when (this) { | ||
ImageFormat.PNG -> Bitmap.CompressFormat.PNG | ||
ImageFormat.JPEG -> Bitmap.CompressFormat.JPEG | ||
} |
66 changes: 66 additions & 0 deletions
66
qr-code/src/commonMain/kotlin/in/procyk/compose/qrcode/Converters.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,66 @@ | ||
/** | ||
* Based on QRose by Alexander Zhirkevich from [Github](https://github.com/alexzhirkevich/qrose) | ||
* | ||
* MIT License | ||
* | ||
* Copyright (c) 2023 Alexander Zhirkevich | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package `in`.procyk.compose.qrcode | ||
|
||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.graphics.Canvas | ||
import androidx.compose.ui.graphics.ColorFilter | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.drawscope.CanvasDrawScope | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import androidx.compose.ui.unit.Density | ||
import androidx.compose.ui.unit.LayoutDirection | ||
|
||
/** | ||
* Converts [Painter] to image with desired [width], [height] and [format] and returns its bytes. | ||
* */ | ||
fun Painter.toByteArray(width : Int, height: Int, format : ImageFormat = ImageFormat.PNG) : ByteArray = | ||
toImageBitmap(width, height).toByteArray(format) | ||
|
||
/** | ||
* Converts [Painter] to [ImageBitmap] with desired [width], [height], [alpha] and [colorFilter] | ||
* */ | ||
fun Painter.toImageBitmap( | ||
width : Int, | ||
height : Int, | ||
alpha : Float = 1f, | ||
colorFilter: ColorFilter? = null | ||
) : ImageBitmap { | ||
|
||
val bmp = ImageBitmap(width, height) | ||
val canvas = Canvas(bmp) | ||
|
||
CanvasDrawScope().draw( | ||
density = Density(1f, 1f), | ||
layoutDirection = LayoutDirection.Ltr, | ||
canvas = canvas, | ||
size = Size(width.toFloat(), height.toFloat()) | ||
) { | ||
draw(this@draw.size, alpha, colorFilter) | ||
} | ||
|
||
return bmp | ||
} |
111 changes: 111 additions & 0 deletions
111
qr-code/src/commonMain/kotlin/in/procyk/compose/qrcode/DrawCache.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,111 @@ | ||
/** | ||
* Based on QRose by Alexander Zhirkevich from [Github](https://github.com/alexzhirkevich/qrose) | ||
* | ||
* MIT License | ||
* | ||
* Copyright (c) 2023 Alexander Zhirkevich | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package `in`.procyk.compose.qrcode | ||
|
||
import androidx.compose.ui.graphics.* | ||
import androidx.compose.ui.graphics.drawscope.CanvasDrawScope | ||
import androidx.compose.ui.graphics.drawscope.DrawScope | ||
import androidx.compose.ui.unit.Density | ||
import androidx.compose.ui.unit.IntSize | ||
import androidx.compose.ui.unit.LayoutDirection | ||
import androidx.compose.ui.unit.toSize | ||
|
||
/** | ||
* Creates a drawing environment that directs its drawing commands to an [ImageBitmap] | ||
* which can be drawn directly in another [DrawScope] instance. This is useful to cache | ||
* complicated drawing commands across frames especially if the content has not changed. | ||
* Additionally some drawing operations such as rendering paths are done purely in | ||
* software so it is beneficial to cache the result and render the contents | ||
* directly through a texture as done by [DrawScope.drawImage] | ||
*/ | ||
internal class DrawCache { | ||
|
||
@PublishedApi internal var mCachedImage: ImageBitmap? = null | ||
private var cachedCanvas: Canvas? = null | ||
private var scopeDensity: Density? = null | ||
private var layoutDirection: LayoutDirection = LayoutDirection.Ltr | ||
private var size: IntSize = IntSize.Zero | ||
|
||
private val cacheScope = CanvasDrawScope() | ||
|
||
/** | ||
* Draw the contents of the lambda with receiver scope into an [ImageBitmap] with the provided | ||
* size. If the same size is provided across calls, the same [ImageBitmap] instance is | ||
* re-used and the contents are cleared out before drawing content in it again | ||
*/ | ||
fun drawCachedImage( | ||
size: IntSize, | ||
density: Density, | ||
layoutDirection: LayoutDirection, | ||
block: DrawScope.() -> Unit | ||
) { | ||
this.scopeDensity = density | ||
this.layoutDirection = layoutDirection | ||
var targetImage = mCachedImage | ||
var targetCanvas = cachedCanvas | ||
if (targetImage == null || | ||
targetCanvas == null || | ||
size.width > targetImage.width || | ||
size.height > targetImage.height | ||
) { | ||
targetImage = ImageBitmap(size.width, size.height) | ||
targetCanvas = Canvas(targetImage) | ||
|
||
mCachedImage = targetImage | ||
cachedCanvas = targetCanvas | ||
} | ||
this.size = size | ||
cacheScope.draw(density, layoutDirection, targetCanvas, size.toSize()) { | ||
clear() | ||
block() | ||
} | ||
targetImage.prepareToDraw() | ||
} | ||
|
||
/** | ||
* Draw the cached content into the provided [DrawScope] instance | ||
*/ | ||
fun drawInto( | ||
target: DrawScope, | ||
alpha: Float = 1.0f, | ||
colorFilter: ColorFilter? = null | ||
) { | ||
val targetImage = mCachedImage | ||
check(targetImage != null) { | ||
"drawCachedImage must be invoked first before attempting to draw the result " + | ||
"into another destination" | ||
} | ||
target.drawImage(targetImage, srcSize = size, alpha = alpha, colorFilter = colorFilter) | ||
} | ||
|
||
/** | ||
* Helper method to clear contents of the draw environment from the given bounds of the | ||
* DrawScope | ||
*/ | ||
private fun DrawScope.clear() { | ||
drawRect(color = Color.Black, blendMode = BlendMode.Clear) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
qr-code/src/commonMain/kotlin/in/procyk/compose/qrcode/PlatformByteArray.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 @@ | ||
/** | ||
* Based on QRose by Alexander Zhirkevich from [Github](https://github.com/alexzhirkevich/qrose) | ||
* | ||
* MIT License | ||
* | ||
* Copyright (c) 2023 Alexander Zhirkevich | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package `in`.procyk.compose.qrcode | ||
|
||
import androidx.compose.ui.graphics.ImageBitmap | ||
|
||
enum class ImageFormat { | ||
PNG, JPEG | ||
} | ||
|
||
expect fun ImageBitmap.toByteArray(format: ImageFormat = ImageFormat.PNG) : ByteArray |
Oops, something went wrong.