Skip to content

Commit

Permalink
Added various examples
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonScholz committed Oct 7, 2023
1 parent d93e876 commit b791a7c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 52 deletions.
122 changes: 78 additions & 44 deletions java-sample/src/main/java/io/github/simonscholz/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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"));
}
}
Binary file added java-sample/src/main/resources/rainbow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(
Expand Down Expand Up @@ -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." }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down

0 comments on commit b791a7c

Please sign in to comment.