Skip to content
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

Couple of questions please - Logo Tint Color and Center Image #30

Closed
waelsaad opened this issue Oct 10, 2023 · 3 comments
Closed

Couple of questions please - Logo Tint Color and Center Image #30

waelsaad opened this issue Oct 10, 2023 · 3 comments

Comments

@waelsaad
Copy link

waelsaad commented Oct 10, 2023

Hi there, I've been diving into QRCode generation, thank you for this project. I've come across the following code and was wondering if there's a more concise or elegant way to achieve the same result.

How can I change the tint color of the centered logo, also how to use circle center (QRCode.LogoTemplate.CircleCenter) perhaps there is a more direct way to center the logo as oppose to using path as below?

let document: QRCode.Document = {
    let d = QRCode.Document(generator: QRCodeGenerator_External())
    
    // Set the text for the QR code
    d.utf8String = "https://www.swift.org"
    
    // Load and set the logo image for the QR code
    if let image = CGImage.named("verified_user") {
        // Define the logo as a circle in the center
        d.logoTemplate = QRCode.LogoTemplate(
            image: image,
            path: CGPath(rect: CGRect(x: 0.35, y: 0.35, width: 0.30, height: 0.30), transform: nil),
            inset: 3
        )
    }
    
    // Set the error correction level to high
    d.errorCorrection = .high
    
    return d
}()
@waelsaad waelsaad changed the title Couple of questions - Logo Tint Color Couple of questions please - Logo Tint Color and share QR image in Messages.app Oct 10, 2023
@waelsaad waelsaad changed the title Couple of questions please - Logo Tint Color and share QR image in Messages.app Couple of questions please - Logo Tint Color and Center Image Oct 11, 2023
@dagronf
Copy link
Owner

dagronf commented Oct 11, 2023

Hi @waelsaad,

was wondering if there's a more concise or elegant way to achieve the same result.

If you're not targeting watchOS, you can (probably) just use the default QR code generator instead of specifying the external generator.

let d = QRCode.Document(generator: QRCodeGenerator_External())
d.errorCorrection = .high
d.utf8String = "https://www.swift.org"

becomes

let d = QRCode.Document(utf8String: "https://www.swift.org", errorCorrection, .high)

also how to use circle center (QRCode.LogoTemplate.CircleCenter)

let document: QRCode.Document = {
   let d = QRCode.Document(utf8String: "https://www.swift.org", errorCorrection, .high)
   d.logoTemplate = QRCode.LogoTemplate.CircleCenter(image: myImage, inset: 3)
   return d
}()

How can I change the tint color of the centered logo

Tinting images is not part of this library. I have another library called Bitmap which has the ability to tint an image

guard 
   let orig = CGImage.named("verified_user"),
   let tintedImage = try? Bitmap(orig)
      .tinting(with: CGColor(red: 0, green: 0, blue: 1, alpha: 1)
      .cgImage
else {
   // handle error
}

If you're not keen to add another library to your code, you can look through the Bitmap source code for tinting.

Hope this helps!

@waelsaad
Copy link
Author

Hi @dagronf thank you so much for the response and correcting the code, its exactly what I was looking for and I'll be looking into the Bitmap. Kind Regards.

@dagronf
Copy link
Owner

dagronf commented Oct 11, 2023

Brilliant mate. Glad I can help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants