1.苹果官方的解决方案(推荐)
Recognizing Text in Images - apple developer
Extracting phone numbers from text in images(Sample Code) - apple developer
1.1 demo下载(亲测有效) :recognizeTextInImage - gitee
1.2 测试成功的代码
import UIKit
import Visionclass 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 = selfpresent(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.widthlet height = cgImage.heightlet 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 转换为 CGImageguard let cgImage = image.cgImage else { return }// 创建处理请求的处理器let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])// 创建 VNRecognizeTextRequestlet request = VNRecognizeTextRequest { (request, error) inif 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 - 腾讯云