dagronf / QRCode

A quick and beautiful macOS/iOS/tvOS/watchOS QR Code generator/detector library for SwiftUI, Swift and Objective-C.
MIT License
438 stars 57 forks source link

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

Closed waelsaad closed 1 year ago

waelsaad commented 1 year ago

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
}()
dagronf commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Brilliant mate. Glad I can help.