How to adjust a color image like a scanned image

别说谁变了你拦得住时间么 提交于 2019-12-01 13:21:58

Try below code.

func getScannedImage(inputImage: UIImage) -> UIImage? {

    let openGLContext = EAGLContext(api: .openGLES2)
    let context = CIContext(eaglContext: openGLContext!)

    let filter = CIFilter(name: "CIColorControls")
    let coreImage = CIImage(image: filterImage.image!)

    filter?.setValue(coreImage, forKey: kCIInputImageKey)
    //Key value are changable according to your need.
    filter?.setValue(7, forKey: kCIInputContrastKey) 
    filter?.setValue(1, forKey: kCIInputSaturationKey) 
    filter?.setValue(1.2, forKey: kCIInputBrightnessKey) 

    if let outputImage = filter?.value(forKey: kCIOutputImageKey) as? CIImage {
    let output = context.createCGImage(outputImage, from: outputImage.extent)
        return UIImage(cgImage: output!)
    }     
    return nil
}

You can call the above func like below.

filterImage.image = getImage(inputImage: filterImage.image!)

Output: (Output from real Device)


For more understanding about Core Image Filter try below links and answers.

Get UIImage from function and Convert UIImage to grayscale keeping image quality

Core Image Filter Reference from Apple Doc: https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html

Note: For more specific requirement you need to create your own custom filter. Following link may helps https://spin.atomicobject.com/2016/10/20/ios-image-filters-in-swift/


I believe in order to manually implement Photoshop-like level adjustment feature, you'll need to understand some of the core knowledge on the field of Image Processing.

You may want to read this Core Image Programming Guide as a starting point:

https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_intro/ci_intro.html

Or, you may simply end up using a third party framework, though you have already stated that you don't want to use 3rd party libraries. If this is the case, I would like to recommend GPUImage2, just in case:

https://github.com/BradLarson/GPUImage2

Finally, I found a similar question to this so you may want to take a look:

How can I map Photoshop's level adjustment to a Core Image filter?

Good luck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!