当前位置: 首页 > 面试题库 >

Swift-来自CIImage QR代码的图像数据/如何呈现CIFilter输出

南门志
2023-03-14
问题内容

我已经遇到这个问题已有一段时间了,在这里看了几十个答案,似乎找不到任何有用的答案。

我正在应用的iOS端生成QR码,并希望将此QR码发送到我当前正在开发的WatchKit扩展程序中。

我如何生成QR码

func createQR(with string: String) {

    if let filter = CIFilter(name: "CIQRCodeGenerator") {

        //set the data to the contact data
        filter.setValue(string, forKey: "inputMessage")
        filter.setValue("L", forKey: "inputCorrectionLevel")

        if let codeImage = filter.outputImage {
            return UIImage(ciImage: codeImage);
        }
    }
}

我接下来要

我想从QR图像中获取数据,以便可以将其发送到Apple Watch应用程序,如下所示:

let data = UIImagePNGRepresentation(QRCodeImage);

但是, 这总是会返回,nil因为没有图像数据支持过滤器的输出。

注意: 我知道没有与CI图像相关联的数据,因为它尚未呈现,甚至没有与之相关联的数据,因为它只是过滤器的输出。
我不知道该如何解决这个问题, 因为我对图像处理等还很陌生。:/

我尝试过的

cgImage从创建filter.outputImage

func createQR(with string: String) {
    if let filter = CIFilter(name: "CIQRCodeGenerator") {

        //set the data to the contact data
        filter.setValue(contactData, forKey: "inputMessage")
        filter.setValue("L", forKey: "inputCorrectionLevel")

        if let codeImage = filter.outputImage {
            let context = CIContext(options: nil)
            if let cgImage = context.createCGImage(codeImage, from: codeImage.extent) {
                self.QRCode = UIImage(cgImage: cgImage)
            }
        }
    }
}

但这似乎行不通,因为视图上的图像是空白的。

创建空白CIImage作为输入图像

func update(with string: String) {
    let blankCiImage = CIImage(color: .white) //This probably isn't right...
    if let filter = CIFilter(name: "CIQRCodeGenerator") {

        filter.setValue(contactData, forKey: "inputMessage")
        filter.setValue("L", forKey: "inputCorrectionLevel")
        filter.setValue(blankCiImage, forKey: kCIInputImageKey)

        if let codeImage = filter.outputImage {
            let context = CIContext(options: nil)
            if let cgImage = context.createCGImage(codeImage, from: codeImage.extent) {
                self.contactCode = UIImage(cgImage: cgImage)
                print(self.contactCode!)
                print(UIImagePNGRepresentation(self.contactCode!))
            }
        }
    }
}

这也不起作用-我的想法是向其添加空白图像,然后在其上方进行过滤,但我可能做得不正确。

我的目标

Literally, just to get the data from the generated QR Code. Most threads
suggest UIImage(ciImage: output) , but this doesn’t have any backing data.

If anyone could help me out with this, that’d be great. And any explanation on
how it works would be wonderful too.

Edit: I don’t believe this is the same as the marked duplicate - The
marked duplicate is about editing an existing image using CI filters and
getting that data and this is about an image that is solely created through CI
filter with no input image - QR Codes. the other answer did not fully relate.


问题答案:

You have a couple of issues in your code. You need to convert your string to
data using String Encoding isoLatin1 before passing it to the filter. Another
issue is that to convert your CIImage to data you need to redraw/render your
CIImage and to prevent blurring the image when scaled you need to apply a
transform to the image to increase its size:

extension StringProtocol {
    var qrCode: UIImage? {
        guard
            let data = data(using: .isoLatin1),
            let outputImage = CIFilter(name: "CIQRCodeGenerator",
                              parameters: ["inputMessage": data, "inputCorrectionLevel": "M"])?.outputImage
        else { return nil }
        let size = outputImage.extent.integral
        let output = CGSize(width: 250, height: 250)
        let format = UIGraphicsImageRendererFormat()
        format.scale = UIScreen.main.scale
        return UIGraphicsImageRenderer(size: output, format: format).image { _ in outputImage
            .transformed(by: .init(scaleX: output.width/size.width, y: output.height/size.height))
            .image
            .draw(in: .init(origin: .zero, size: output))
        }
    }
}
extension CIImage {
    var image: UIImage { .init(ciImage: self) }
}

Playground testing:

let link = "https://stackoverflow.com/questions/51178573/swift-image-data-from-ciimage-qr-code-how-to-render-cifilter-output?noredirect=1"
let image = link.qrCode!
let data =  image.jpegData(compressionQuality: 1)  // 154785 bytes

enter image description
here



 类似资料:
  • 问题内容: 我正在使用核心图像,并且将CIFilter棕褐色调应用于图像。我在viewDidLoad中运行了一次过滤器,然后立即调用另一个再次添加过滤器的函数。由于某些原因,当我尝试访问输出图像时,应用程序崩溃并说输出图像为nil。有人知道为什么会这样吗? 谢谢 问题答案: 您不能调用该UIImage并将其用作UIImageView的图像。UIImageView需要一个由位图(CGImage)支持

  • 问题内容: 此代码将图片test.gif放入图片位值中。 我的下一个目标是显示以字节为单位保存在数据库中的图片。如何才能做到这一点 ? 问题答案: 就像是: 可能为您工作。

  • 我正在尝试在表FORNMAT中显示数据。数据来自API。我需要显示表一旦数据接收从api。我正在使用类组件。下面我给出了尝试的东西。 //下面是我的jsx //下面是我从api获取数据的函数 //下面是我的api输出

  • 我目前正在尝试将一个图像呈现给JPanel。下面是我的代码: 在JFrame加载之后调用“initCode()”方法。我现在的问题是,我想调用“renderImage()”方法。在参数中,我必须放入“graphics g”来使用“g.drawimage”函数。遗憾的是,当我想调用“renderImage()”时,我现在不知道应该在括号中放些什么。有人能帮忙吗?

  • 此函数类似于numpy.array,除了它有较少的参数。 这个例程对于将 Python 序列转换为ndarray非常有用。 构造器接受下列参数: 下面的例子展示了如何使用asarray函数: # 将列表转换为 ndarray import numpy as np a = np.asarray(x) print a 输出如下: [1 2 3] 示例 2 import numpy as np x

  • 我正在为我的游戏引擎创建一个糟糕的运动模糊效果,我有一个名为MotionBlur[]的五索引图像数组。 每次我的图形对象完成绘制到屏幕上时,我都需要将该数据存储到MotionBlur[0]中。每帧图像都会从0向下移动到4,然后被踢出。我将在最后一幅图像之后,用不同的、适当的透明胶片绘制下面的每幅图像。 我要问的是如何将Graphics2D数据存储到awt中。形象 感谢所有帮助