Skip to content

Commit

Permalink
Scale logo in advance using getScaledInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Scholz committed Nov 4, 2023
1 parent 8f56458 commit 8f175c0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.github.simonscholz.qrcode.QrPositionalSquaresConfig
import io.github.simonscholz.ui.ImageUI
import java.awt.Color
import java.awt.Component
import java.awt.Image
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
Expand Down Expand Up @@ -38,8 +39,13 @@ object RenderImageService {
if (qrCodeConfigViewModel.logo.value.isNotBlank() && File(qrCodeConfigViewModel.logo.value).exists()) {
runCatching {
ImageIO.read(File(qrCodeConfigViewModel.logo.value)).let {

val logoSize = (qrCodeConfigViewModel.size.value * qrCodeConfigViewModel.logoRelativeSize.value).toInt()

val scaledLogo = it.getScaledInstance(logoSize, logoSize, Image.SCALE_SMOOTH)

builder.qrLogoConfig(
logo = it,
logo = scaledLogo,
relativeSize = qrCodeConfigViewModel.logoRelativeSize.value,
bgColor = qrCodeConfigViewModel.logoBackgroundColor.value,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.simonscholz.qrcode

import java.awt.Color
import java.awt.image.BufferedImage
import java.awt.Image

const val DEFAULT_IMG_SIZE = 300

Expand Down Expand Up @@ -37,7 +37,7 @@ class QrCodeConfig @JvmOverloads constructor(

fun qrCodeSize(qrCodeSize: Int) = apply { this.qrCodeSize = qrCodeSize }

@JvmOverloads fun qrLogoConfig(logo: BufferedImage, relativeSize: Double = .2, bgColor: Color? = null) = apply { this.qrLogoConfig = QrLogoConfig(logo, relativeSize, bgColor) }
@JvmOverloads fun qrLogoConfig(logo: Image, relativeSize: Double = .2, bgColor: Color? = null) = apply { this.qrLogoConfig = QrLogoConfig(logo, relativeSize, bgColor) }

@JvmOverloads fun qrCodeColorConfig(bgColor: Color = Color.WHITE, fillColor: Color = Color.BLACK) = apply { this.qrCodeColorConfig = QrCodeColorConfig(bgColor, fillColor) }

Expand All @@ -64,7 +64,7 @@ class QrCodeConfig @JvmOverloads constructor(
* @param bgColor - specify the background color of the logo, defaults to null
*/
class QrLogoConfig @JvmOverloads constructor(
val logo: BufferedImage,
val logo: Image,
val relativeSize: Double = .2,
val bgColor: Color? = null,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package io.github.simonscholz.qrcode.internal.logo

import java.awt.Color
import java.awt.Graphics2D
import java.awt.Image
import java.awt.geom.Area
import java.awt.geom.Ellipse2D
import java.awt.image.BufferedImage
import kotlin.math.floor

internal object LogoGraphics {

fun drawLogo(graphics: Graphics2D, size: Int, logoImage: BufferedImage, relativeLogoSize: Double, logoBackgroundColor: Color?) {
fun drawLogo(graphics: Graphics2D, size: Int, logoImage: Image, relativeLogoSize: Double, logoBackgroundColor: Color?) {
val logoSize: Int = floor(size * relativeLogoSize).toInt()
val cx = size / 2 - logoSize / 2
val cy = size / 2 - logoSize / 2
Expand Down

0 comments on commit 8f175c0

Please sign in to comment.