From b791a7c225b2bcc69a8e6afcb15773e95be8a0e4 Mon Sep 17 00:00:00 2001 From: Simon Scholz Date: Sun, 8 Oct 2023 00:31:03 +0200 Subject: [PATCH] Added various examples --- .../main/java/io/github/simonscholz/Main.java | 122 +++++++++++------- java-sample/src/main/resources/rainbow.png | Bin 0 -> 2630 bytes .../github/simonscholz/qrcode/QrCodeConfig.kt | 14 +- .../simonscholz/qrcode/api/QrCodeApiImpl.kt | 2 +- 4 files changed, 86 insertions(+), 52 deletions(-) create mode 100644 java-sample/src/main/resources/rainbow.png diff --git a/java-sample/src/main/java/io/github/simonscholz/Main.java b/java-sample/src/main/java/io/github/simonscholz/Main.java index b10e419..3defa70 100644 --- a/java-sample/src/main/java/io/github/simonscholz/Main.java +++ b/java-sample/src/main/java/io/github/simonscholz/Main.java @@ -2,7 +2,6 @@ import io.github.simonscholz.qrcode.QrCodeApi; import io.github.simonscholz.qrcode.QrCodeConfig; -import io.github.simonscholz.qrcode.QrCodeConfig.Builder; import io.github.simonscholz.qrcode.QrCodeFactory; import io.github.simonscholz.qrcode.QrLogoConfig; import io.github.simonscholz.qrcode.QrPositionalSquaresConfig; @@ -16,6 +15,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Objects; public class Main { public static void main(String[] args) throws IOException { @@ -30,82 +30,116 @@ public static void main(String[] args) throws IOException { if(resource != null) { createDefaultQrCodeWithLogo(resource, qrCodeApi, qrCodeDir); - /* - createDefaultQrCodeWithLogoAndCustomColors(resource, qrCodeApi, qrCodeDir); + createDefaultQrCodeWithLogoAndBorder(resource, qrCodeApi, qrCodeDir); - createQrCodeWithRoundedPositionalSquares(resource, qrCodeApi, qrCodeDir); + createDefaultQrCodeWithLogoAndBorderAndPositionalSquareBorderRadius(resource, qrCodeApi, qrCodeDir); - reallyColorfulQrCode(resource, qrCodeApi, qrCodeDir); - */ + createDefaultQrCodeWithLogoAndBorderAndPositionalSquareCircle(resource, qrCodeApi, qrCodeDir); + + decentRedColor(resource, qrCodeApi, qrCodeDir); + + mineCraftCreeperColor(resource, qrCodeApi, qrCodeDir); + + rainbowColor(qrCodeApi, qrCodeDir); } } - private static void reallyColorfulQrCode(final URL resource, final QrCodeApi qrCodeApi, final String qrCodeDir) throws IOException { + private static void createDefaultQrCode(QrCodeApi qrCodeApi, String qrCodeDir) throws IOException { + var qrCode = qrCodeApi.createQrImage(new QrCodeConfig("https://simonscholz.github.io/", 200)); + ImageIO.write(qrCode, "png", new File(qrCodeDir, "/qr-with-defaults-java.png")); + } + + private static void createDefaultQrCodeWithLogo(URL resource, QrCodeApi qrCodeApi, String qrCodeDir) throws IOException { BufferedImage logo = ImageIO.read(resource); - final Color transparent = new Color(0, 0, 0, 0); + QrCodeConfig qrCodeConfig = new QrCodeConfig("https://simonscholz.github.io/", + 300, + new QrLogoConfig(logo)); + BufferedImage qrWithImage = qrCodeApi.createQrImage(qrCodeConfig); + ImageIO.write(qrWithImage, "png", new File(qrCodeDir, "/qr-with-logo-java.png")); + } + + private static void createDefaultQrCodeWithLogoAndBorder(URL resource, QrCodeApi qrCodeApi, String qrCodeDir) throws IOException { + BufferedImage logo = ImageIO.read(resource); - QrPositionalSquaresConfig qrPositionalSquaresConfig = - new QrPositionalSquaresConfig(Color.CYAN, new Color(0x0063,0x000B,0x00A5), Color.PINK, Color.BLUE, true); - QrCodeConfig qrCodeConfig =new Builder("https://simonscholz.github.io/") - .qrCodeSize(500) + QrCodeConfig qrCodeConfig = new QrCodeConfig + .Builder("https://simonscholz.github.io/") + .qrBorderConfig(Color.BLACK) .qrLogoConfig(logo) - .qrPositionalSquaresConfig(qrPositionalSquaresConfig) - .qrBorderConfig(Color.ORANGE.darker(), .05) - //.qrBorderConfig(transparent, .05) - //.qrCodeColorConfig(transparent, Color.BLUE) - .qrCodeColorConfig(Color.WHITE, Color.BLUE) .build(); - BufferedImage qrWithImage = qrCodeApi.createQrImage(qrCodeConfig); - ImageIO.write(qrWithImage, "png", new File(qrCodeDir, "/qr-colorful-java.png")); + ImageIO.write(qrWithImage, "png", new File(qrCodeDir, "/qr-with-logo-and-border-java.png")); } - private static void createQrCodeWithRoundedPositionalSquares(URL resource, QrCodeApi qrCodeApi, String qrCodeDir) throws IOException { + private static void createDefaultQrCodeWithLogoAndBorderAndPositionalSquareBorderRadius(URL resource, QrCodeApi qrCodeApi, String qrCodeDir) throws IOException { BufferedImage logo = ImageIO.read(resource); - QrPositionalSquaresConfig qrPositionalSquaresConfig = - new QrPositionalSquaresConfig(Color.BLACK, Color.RED, Color.BLUE, Color.WHITE, true); - QrCodeConfig qrCodeConfig =new Builder("https://simonscholz.github.io/") + QrCodeConfig qrCodeConfig = new QrCodeConfig + .Builder("https://simonscholz.github.io/") + .qrBorderConfig(Color.BLACK) .qrLogoConfig(logo) - .qrPositionalSquaresConfig(qrPositionalSquaresConfig) - .qrBorderConfig(Color.WHITE, .04) + .qrPositionalSquaresConfig(new QrPositionalSquaresConfig(false, 0.5)) .build(); - BufferedImage qrWithImage = qrCodeApi.createQrImage(qrCodeConfig); - ImageIO.write(qrWithImage, "png", new File(qrCodeDir, "/qr-rounded-positionals-java.png")); + ImageIO.write(qrWithImage, "png", new File(qrCodeDir, "/qr-with-logo-and-border-and-p-border-round-java.png")); } - private static void createDefaultQrCodeWithLogoAndCustomColors(URL resource, QrCodeApi qrCodeApi, String qrCodeDir) throws IOException { + private static void createDefaultQrCodeWithLogoAndBorderAndPositionalSquareCircle(URL resource, QrCodeApi qrCodeApi, String qrCodeDir) throws IOException { BufferedImage logo = ImageIO.read(resource); - Color darkGreen = Color.GREEN.darker().darker().darker(); - Color brightGreen = Color.GREEN.brighter(); - - QrCodeConfig qrCodeConfig =new Builder("https://simonscholz.github.io/") + QrCodeConfig qrCodeConfig = new QrCodeConfig + .Builder("https://simonscholz.github.io/") + .qrBorderConfig(Color.BLACK) .qrLogoConfig(logo) - .qrCodeColorConfig(darkGreen, brightGreen) - .qrPositionalSquaresConfig(new QrPositionalSquaresConfig(darkGreen, brightGreen)) - .qrBorderConfig(Color.WHITE) + .qrPositionalSquaresConfig(new QrPositionalSquaresConfig(true)) .build(); + BufferedImage qrWithImage = qrCodeApi.createQrImage(qrCodeConfig); + ImageIO.write(qrWithImage, "png", new File(qrCodeDir, "/qr-with-logo-and-border-and-p-border-circle-java.png")); + } + + private static void decentRedColor(URL resource, QrCodeApi qrCodeApi, String qrCodeDir) throws IOException { + BufferedImage logo = ImageIO.read(resource); + QrCodeConfig qrCodeConfig = new QrCodeConfig + .Builder("https://simonscholz.github.io/") + .qrBorderConfig(Color.BLACK) + .qrLogoConfig(logo) + .qrPositionalSquaresConfig(new QrPositionalSquaresConfig(true, 0.2, Color.RED)) + .build(); BufferedImage qrWithImage = qrCodeApi.createQrImage(qrCodeConfig); - ImageIO.write(qrWithImage, "png", new File(qrCodeDir, "/qr-with-logo-green-color-java.png")); + ImageIO.write(qrWithImage, "png", new File(qrCodeDir, "/decent-red-color-java.png")); } - private static void createDefaultQrCodeWithLogo(URL resource, QrCodeApi qrCodeApi, String qrCodeDir) throws IOException { + private static void mineCraftCreeperColor(URL resource, QrCodeApi qrCodeApi, String qrCodeDir) throws IOException { BufferedImage logo = ImageIO.read(resource); - QrCodeConfig qrCodeConfig = new QrCodeConfig("https://simonscholz.github.io/", - 300, - new QrLogoConfig(logo)); + var brighterGreen = Color.GREEN.brighter(); + var darkerGreen = Color.GREEN.darker().darker().darker(); + + QrCodeConfig qrCodeConfig = new QrCodeConfig + .Builder("https://simonscholz.github.io/") + .qrBorderConfig(Color.WHITE) + .qrLogoConfig(logo) + .qrCodeColorConfig(darkerGreen, brighterGreen) + .qrPositionalSquaresConfig(new QrPositionalSquaresConfig(false, .5, brighterGreen, darkerGreen, brighterGreen, darkerGreen)) + .build(); BufferedImage qrWithImage = qrCodeApi.createQrImage(qrCodeConfig); - ImageIO.write(qrWithImage, "png", new File(qrCodeDir, "/qr-with-logo-java.png")); + ImageIO.write(qrWithImage, "png", new File(qrCodeDir, "/minecraft-creeper-color-java.png")); } - private static void createDefaultQrCode(QrCodeApi qrCodeApi, String qrCodeDir) throws IOException { - var qrCode = qrCodeApi.createQrImage(new QrCodeConfig("https://simonscholz.github.io/", 200)); - ImageIO.write(qrCode, "png", new File(qrCodeDir, "/qr-with-defaults-java.png")); + private static void rainbowColor(QrCodeApi qrCodeApi, String qrCodeDir) throws IOException { + URL resource = Objects.requireNonNull(Main.class.getClassLoader().getResource("rainbow.png")); + BufferedImage logo = ImageIO.read(resource); + + QrCodeConfig qrCodeConfig = new QrCodeConfig + .Builder("https://simonscholz.github.io/") + .qrBorderConfig(Color.YELLOW) + .qrLogoConfig(logo) + .qrCodeColorConfig(Color.BLUE, new Color(0x0063,0x000B,0x00A5)) + .qrPositionalSquaresConfig(new QrPositionalSquaresConfig(true, .5, Color.PINK, Color.GREEN, Color.RED, Color.CYAN)) + .build(); + BufferedImage qrWithImage = qrCodeApi.createQrImage(qrCodeConfig); + ImageIO.write(qrWithImage, "png", new File(qrCodeDir, "/rainbow-color-java.png")); } } diff --git a/java-sample/src/main/resources/rainbow.png b/java-sample/src/main/resources/rainbow.png new file mode 100644 index 0000000000000000000000000000000000000000..ac7c07ac46314844ba9b6ed43230ecd0caac8f14 GIT binary patch literal 2630 zcmV-M3c2-(P)=M~2VA895wjyRTOckkUrU_m4TE^n5XBr4(q zgMyP+MGys?Bv!{Ln)s+1iBxN~;UO3WYJA`$R!v(H6*Y)4X`(TxwYF-eO;cZpF=|Xa zJ!i|#?0)y#7khCbcjnB_a`%4s`<-*X|2f~?rALpS?$JxBRE8-OiWhu*e3qeY#rH?> z^&Hv_v{rl%?w+KahX+FG5o#g69>mw%1ZdCPJ!zK@jBo^ktU>61I|BTZaDczR|6?kZ z>LrB!yd%)v)&bt$-cKO(T%7n-N8kdKY8a{ZhZ({UcuUknttJ-sXyV}mZ6Y*jli`$a zYPWGfPXsDJ`^FZqs8qoibpWgohZ!MTe4n;!?gq1aV8ji!QeA~MUaf;NO%$BerZ@!M zEgYa$s~<}{UkbsnUt@3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la}*FH-PmMK&4u?aT(o=rfy-BnRfxS_5PTsz zZVIRcEen2+z!A^W-dfM5Us2ggo1yv$mjy3%{CFmX)i8hEdZ)*Fy}UxQiDJbLdG2B+ z54XF{mH+=0#zTAXWq)aK$pLTHZIO)I5qOg=EGxIC5ruZ)(L)n?@UbZ=MlbL9;!ERN zltcJpl9MHA@9LNnrsPTsj1;3j){`3j2(lG@8Pc2@p$tIL3N4fisc)}n8r_7wTf zfF~O@agxQvsMN_dHf0j;fr^>YHoky$z^m+a}H7c(56ZV|3XhRifd?mjpxFraJ z(&T`P<|pgVw8(OEs`dE`7TPRNuJ#j-8nxT36G-<=4*0e+BH{?VtNnyCMoWUN4x%Xf zkOSJ}b^xV43ruT2zj%T1$*)}fgfm9VAu2JEo!1-^!Six10TR5>h)9bYg@g}-ZJ(ch zugX z)&X~ndwwOe*Qe6a(VMW1NZrSuxE={6?8ny@ zrGx*C$uknwym|AX=J<(D6-FAH^oj(Jv*yWu#L}N`7v9waomT8h*05mCp0{G8Z$+IB zZ{~!>9Tio%9YAUSNFvW33WXvGZ+^j92e9Kfd*00Gp=h~7YrYeYp6G})oJ<{kHD&tO o^-gjC=~N3fIiz1Up9bUcKl2+2<_9~?x&QzG07*qoM6N<$f*G&_Hvj+t literal 0 HcmV?d00001 diff --git a/qr-code/src/main/kotlin/io/github/simonscholz/qrcode/QrCodeConfig.kt b/qr-code/src/main/kotlin/io/github/simonscholz/qrcode/QrCodeConfig.kt index bbea713..d00973b 100644 --- a/qr-code/src/main/kotlin/io/github/simonscholz/qrcode/QrCodeConfig.kt +++ b/qr-code/src/main/kotlin/io/github/simonscholz/qrcode/QrCodeConfig.kt @@ -27,7 +27,7 @@ data class QrCodeConfig @JvmOverloads constructor( } class Builder(private val qrCodeText: String) { - private var qrCodeSize: Int = 200 + private var qrCodeSize: Int = 300 private var qrLogoConfig: QrLogoConfig? = null private var qrCodeColorConfig: QrCodeColorConfig = QrCodeColorConfig() private var qrPositionalSquaresConfig: QrPositionalSquaresConfig = QrPositionalSquaresConfig() @@ -38,8 +38,8 @@ data class QrCodeConfig @JvmOverloads constructor( fun qrLogoConfig(logo: BufferedImage) = apply { this.qrLogoConfig = QrLogoConfig(logo, .2) } fun qrCodeColorConfig(bgColor: Color = Color.WHITE, fillColor: Color = Color.BLACK) = apply { this.qrCodeColorConfig = QrCodeColorConfig(bgColor, fillColor) } fun qrPositionalSquaresConfig(qrPositionalSquaresConfig: QrPositionalSquaresConfig) = apply { this.qrPositionalSquaresConfig = qrPositionalSquaresConfig } - fun qrBorderConfig(color: Color = Color.BLACK, relativeSize: Double = .05) = apply { this.qrBorderConfig = QrBorderConfig(color, relativeSize) } - fun qrBorderConfig(relativeSize: Double = .05) = apply { this.qrBorderConfig = QrBorderConfig(Color.BLACK, relativeSize) } + fun qrBorderConfig(color: Color = Color.BLACK, relativeSize: Double = .05, relativeBorderRound: Double = 0.2) = apply { this.qrBorderConfig = QrBorderConfig(color, relativeSize, relativeBorderRound) } + fun qrBorderConfig(relativeSize: Double = .05, relativeBorderRound: Double = 0.2) = apply { this.qrBorderConfig = QrBorderConfig(Color.BLACK, relativeSize, relativeBorderRound) } fun qrBorderConfig(color: Color = Color.BLACK) = apply { this.qrBorderConfig = QrBorderConfig(color, .05) } fun build() = QrCodeConfig( @@ -86,12 +86,12 @@ data class QrCodeColorConfig @JvmOverloads constructor(val bgColor: Color = Colo * @param relativeSquareBorderRound - in case isCircleShaped==false the borders of the square may be round at the edges, defaults to 0.05 */ data class QrPositionalSquaresConfig @JvmOverloads constructor( - val outerBorderColor: Color = Color.WHITE, - val outerSquareColor: Color = Color.BLACK, - val innerSquareColor: Color = Color.WHITE, - val centerColor: Color = Color.BLACK, val isCircleShaped: Boolean = false, val relativeSquareBorderRound: Double = .0, + val centerColor: Color = Color.BLACK, + val innerSquareColor: Color = Color.WHITE, + val outerSquareColor: Color = Color.BLACK, + val outerBorderColor: Color = Color.WHITE, ) { init { require(relativeSquareBorderRound in 0.0..1.0) { "relativeSquareBorderRound must be in between 0 and 1." } diff --git a/qr-code/src/main/kotlin/io/github/simonscholz/qrcode/api/QrCodeApiImpl.kt b/qr-code/src/main/kotlin/io/github/simonscholz/qrcode/api/QrCodeApiImpl.kt index ec061ba..35dacaf 100644 --- a/qr-code/src/main/kotlin/io/github/simonscholz/qrcode/api/QrCodeApiImpl.kt +++ b/qr-code/src/main/kotlin/io/github/simonscholz/qrcode/api/QrCodeApiImpl.kt @@ -41,7 +41,7 @@ internal class QrCodeApiImpl : QrCodeApi { outerSquareColor = qrCodeConfig.qrPositionalSquaresConfig.outerSquareColor, innerSquareColor = qrCodeConfig.qrPositionalSquaresConfig.innerSquareColor, centerColor = qrCodeConfig.qrPositionalSquaresConfig.centerColor, - quietZone = qrCodeConfig.qrBorderConfig?.let { 2 } ?: 0, // have a quietZone if we have a border + quietZone = qrCodeConfig.qrBorderConfig?.let { 1 } ?: 0, // have a quietZone if we have a border borderWidth = qrCodeConfig.qrBorderConfig?.let { relativeSize(qrCodeConfig.qrCodeSize, it.relativeSize) } ?: 0, relativeBorderRound = qrCodeConfig.qrBorderConfig?.relativeBorderRound ?: .0, )