Eine aufbereitete Darstellung der Quelle

 
     
 
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung


Quelle  QRCodeGenerator.swift   Sprache: JAVA

 

import CocoaLumberjackSwift
import CoreImage.CIFilterBuiltins

enum QRCodeGenerator {
    
    /// Generate a QR code containing the provided string
    /// - Parameters:
    ///   - string: String to encode in QR code
    ///   - size: Size of generated UIImage
    /// - Returns: Image representing the QR code
    static func generateQRCode(for string: String, size: CGFloat? = nil) -> UIImage {
        // For now this creates a black QR code with a white background. This is also the easiest to be scanned.
        // If our needs extend we might create an image with a transparent background that can be tined which needs
        // multiple filters.
        // (e.g. https://www.avanderlee.com/swift/qr-code-generation-swift/#changing-the-color-of-a-qr-code)
        
        // If this function gets called very often we might optimize this and not create a new context for each
        // generation
        let context = CIContext()
        
        let filter = CIFilter.qrCodeGenerator()
        
        filter.message = Data(string.utf8)
        filter.correctionLevel = "Q" // 25% error correction
        
        if let qrCodeImage = filter.outputImage {
            let resizedImage: CIImage
            if let size {
                let xScale = size / qrCodeImage.extent.width
                let yScale = size / qrCodeImage.extent.height
                resizedImage = qrCodeImage.transformed(by: CGAffineTransform(scaleX: xScale, y: yScale))
            }
            else {
                resizedImage = qrCodeImage
            }
            
            // This allows us better control which context is (re)used to render the image instead of using
            // `UIImage(ciImage:)` directly. In our tests directly mapping to UIImage wouldn't actually render the QR
            // code in the view. https://www.kodeco.com/30195423-core-image-tutorial-getting-started#toc-anchor-005
            if let cgImage = context.createCGImage(resizedImage, from: resizedImage.extent) {
                return UIImage(cgImage: cgImage)
            }
        }
        
        DDLogError("Failed to generate QR code")
        return UIImage(systemName: "xmark.circle") ?? UIImage()
    }
}

Messung
CHZG
100919093

¤ Dauer der Verarbeitung: 0.2 Sekunden  (vorverarbeitet am  2026-11-24) ¤

*© Formatika GbR, Deutschland







                                                                                                                                                                                                                                                                                                                                                                                                     

 

Neuigkeiten

Aktuelles
Motto des Tages

Open Source Software


Gedichte
Musik
Bilder

Jenseits des Üblichen

Besucherstatistik

Besucherstatistik

Statistik
#Sources=1,187,813
#Domains=881,136