【问题标题】:Is the a way to modify the squares in the corners by circles in a QR?是否可以通过 QR 中的圆圈修改角落中的正方形?
【发布时间】:2019-07-11 21:46:04
【问题描述】:

我已按照本教程 (https://medium.com/@dominicfholmes/generating-qr-codes-in-swift-4-b5dacc75727c) 生成 qr,但我正在尝试生成自定义 qr,其中一个要求是它们不是正方形,而是角落中的圆形。这可能吗?

func generateQR(fromString : String) -> UIImage? {
    let data = fromString.data(using: String.Encoding.ascii)
    // Get a QR CIFilter
    guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil}
    // Input the data
    qrFilter.setValue(data, forKey: "inputMessage")
    // Get the output image
    guard let qrImage = qrFilter.outputImage else { return nil}
    // Scale the image
    let transform = CGAffineTransform(scaleX: 10, y: 10)
    let scaledQrImage = qrImage.transformed(by: transform)
    // Invert the colors
    guard let colorInvertFilter = CIFilter(name: "CIColorInvert") else { return nil}
    colorInvertFilter.setValue(scaledQrImage, forKey: "inputImage")
    guard let outputInvertedImage = colorInvertFilter.outputImage else { return nil}
    // Replace the black with transparency
    guard let maskToAlphaFilter = CIFilter(name: "CIMaskToAlpha") else { return nil}
    maskToAlphaFilter.setValue(outputInvertedImage, forKey: "inputImage")
    guard let outputCIImage = maskToAlphaFilter.outputImage else { return nil}
    // Do some processing to get the UIImage
    let context = CIContext()
    guard let cgImage = context.createCGImage(outputCIImage, from: outputCIImage.extent) else { return nil}
    let processedImage = UIImage(cgImage: cgImage)

    return processedImage
}

有一个预期结果的例子

https://www.qrcode-monkey.com/img/qrcode-logo.png

【问题讨论】:

    标签: swift qr-code


    【解决方案1】:

    我已经有一段时间没有使用 Core Image QR 码生成器过滤器了,CIQRCodeGenerator。查看文档,它只需要几个参数,inputMessageinputCorrectionLevel。除了这些参数之外,没有其他工具可以自定义它生成的二维码。

    我想您可以对生成的图像进行图像处理,以找到“靶心”角方块,将它们更改为圆角矩形,但这将是一个公平的挑战。

    相反,您始终可以编写自己的二维码渲染库。图像处理部分并不复杂。它正在弄清楚 QR 码标准以及如何生成很难的点阵图案。我还没有查看 QR 码的规格,但它是公开的。

    您可以使用现有的开源 QR 码库并对其进行修改以创建您所追求的圆角矩形。如果这是我的任务,我认为这是我会追求的选择。运气好的话,您可以找到一个编写良好的库,它首先将 QR 码生成为布尔网格,然后使用单独的函数将该网格呈现为图像。

    【讨论】:

      猜你喜欢
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 2020-07-11
      相关资源
      最近更新 更多