iOS Swift 拍照识别数字(Recognizing Text in Images)

1.苹果官方的解决方案(推荐)

Recognizing Text in Images - apple developer
Extracting phone numbers from text in images(Sample Code) - apple developer

1.1 demo下载(亲测有效) :recognizeTextInImage - gitee

iOS Swift 拍照识别数字(Recognizing Text in Images)_第1张图片

iOS Swift 拍照识别数字(Recognizing Text in Images)_第2张图片

1.2 测试成功的代码

import UIKit
import Vision

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    @IBOutlet weak var resLabel: UILabel!
    
    
    @IBAction func recognizeAssetsImage(_ sender: Any) {
        recognizeText(in: #imageLiteral(resourceName: "image_sample"))
    }
    
    // 按钮点击,打开相册
    @IBAction func selectImage(_ sender: UIButton) {
        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self
        present(imagePickerController, animated: true, completion: nil)
    }
    
    // 选择图片完成后的回调
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
        picker.dismiss(animated: true, completion: nil)
        
        // 获取选择的图片
        guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
            return
        }
        
        let resImage = binarize(image: selectedImage)!
        // 识别图片中的文本
        recognizeText(in: resImage)
    }
    
    func binarize(image: UIImage, threshold: CGFloat = 0.5) -> UIImage? {
        guard let cgImage = image.cgImage else { return nil }
        let width = cgImage.width
        let height = cgImage.height
        let colorSpace = CGColorSpaceCreateDeviceGray()
        var pixels = [UInt8](repeating: 0, count: width * height)
        
        guard let context = CGContext(data: &pixels, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width, space: colorSpace, bitmapInfo: CGImageAlphaInfo.none.rawValue) else { return nil }
        context.draw(cgImage, in: CGRect(x: 0, y: 0, width: width, height: height))
        
        for i in 0 ..< pixels.count {
            pixels[i] = pixels[i] < UInt8(threshold * 255) ? 0 : 255
        }
        
        guard let binarizedCgImage = context.makeImage() else { return nil }
        return UIImage(cgImage: binarizedCgImage)
    }

    
    func recognizeText(in image: UIImage) {
        // 将 UIImage 转换为 CGImage
        guard let cgImage = image.cgImage else { return }
        
        // 创建处理请求的处理器
        let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
        
        // 创建 VNRecognizeTextRequest
        let request = VNRecognizeTextRequest { (request, error) in
            if let error = error {
                print("Text recognition error: \(error)")
                return
            }
            
            // 处理识别结果
            guard let observations = request.results as? [VNRecognizedTextObservation] else { return }
            for observation in observations {
                // 获取识别到的文本
                guard let topCandidate = observation.topCandidates(1).first else { continue }
                print("Recognized text: \(topCandidate.string)")
                self.resLabel.text = topCandidate.string
            }
        }
        
        // 设置识别级别
        request.recognitionLevel = .accurate
        
        // 执行请求
        do {
            try handler.perform([request])
        } catch {
            print("Failed to perform text recognition: \(error)")
        }
    }
}



前期尝试过的解决方案(没有试成功过)
TesseractOCRiOS - github(最后一次提交2020.2)
SwiftyTesseract - github(最后一次提交2022.4)
OCR的iOS demo - 腾讯云

你可能感兴趣的:(ios,ios)