MathpixClient 0.1.4

MathpixClient 0.1.4

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2023年9月
SPM支持 SPM

Dmitriy Bukanovich 维护。



  • DmitriDevelopment

MathpixClient 是一个简单的包装器,通过 mathpix 服务器获取和识别图像 https://mathpix.com

示例

要运行示例项目,克隆仓库,然后首先从 Example 目录运行 pod install

用法

设置 API 密钥

import MathpixClient

MathpixClient.setApiKeys(appId: "demo_app", appKey: "demo_key")

您可以从 https://dashboard.mathpix.com 免费请求 API 密钥。

启动摄像头

在 iOS 上使用 Mathpix 最简单的方法是启动我们的摄像头控制器,这可以用一行代码完成!

MathpixClient.launchCamera(source: self,
                           outputFormats: [FormatLatex.simplified],
                           backButtonCallback: {
                                print("back button pressed") },
                           completion: { (error, result) in
                                print("complete") })

识别图像

如果您想使用自定义摄像头控制器,仍然可以使用 MathpixClient 来识别图像

MathpixClient.recognize(image: UIImage(named: "equation")!, outputFormats: [FormatLatex.simplified]) { (error, result) in
    print(result ?? error)
}

使用自定义 UI/UX 属性启动摄像头

您可以使用 MathCaptureProperties 实例来细化我们提供的摄像头控制器

let properties = MathCaptureProperties(captureType: .gesture,
                                               requiredButtons: [.flash, .back],
                                               cropColor: UIColor.green,
                                               errorHandling: true)
        
MathpixClient.launchCamera(source: self,
                           outputFormats: [FormatLatex.simplified],
                           withProperties: properties,
                           backButtonCallback: {
                                print("back button pressed") },
                           completion: { (error, result) in
                                print("complete") })

子类化 MathCaptureViewController

如果您需要更多控制,您可以对 MathCaptureViewController 进行子类化。

class CustomCameraViewController: MathCaptureViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Add your UI elements here
        
    }
}

API

我们将描述如何通过 iOS Mathpix SDK 将选项传递给 Mathpix API。有关这些选项的更多信息,请参阅以下 API 文档:https://docs.mathpix.com/

MathpixFormat

协议表示请求体中的额外字段“format”。作为参数 outputFormats 数组请求。您可以设置多个值

FormatLatex

如果存在 latex 字段,其一为“raw”(结果是未修改的 OCR 输出)、“defaultValue”(结果是去除无关空格的 OCR 输出)或“simplified”(结果去除空格、运算符快捷键,并根据需要分割成列表)

public enum FormatLatex {
    case raw, defaultValue, simplified
}

FormatMathml

如果存在 mathml 字段且设置为 on,则指示服务器应在 JSON 结果中添加一个包含所识别数学内容的 MathML 标记的 mathml 字段。在不兼容的结果情况下,服务器将添加 mathml_error

public enum FormatMathml {
    case on
}

FormatWolfram

如果存在 wolfram 字段且设置为 on,则指示服务器应在 JSON 结果中添加一个与 Wolfram Alpha 引擎兼容的 wolfram 字段。在不兼容的结果情况下,服务器将添加 wolfram_error 字段

public enum FormatWolfram {
    case on
}

MathCaptureProperties

用于封装 MathCaptureViewController 属性的结构体。您可以自定义一些 UI/UX 值

裁剪覆盖边界的颜色

let cropColor: UIColor

快门按钮的图标

let shutterIcon : UIImage?

闪光灯按钮的图标

let flashIcon : UIImage?

返回按钮的图标

let backIcon : UIImage?

取消请求按钮的图标

let cancelIcon : UIImage?

RecognitionAnimator 的类型。用于为识别过程提供动画

let animatorType : RecognitionAnimator.Type

UI 捕获动作的类型

let captureType: CaptureType

public enum CaptureType {
    /// Tap gesture is used to capture image.
    case gesture
    /// Shutter button is used to capture image.
    case button
}

将在实例化的 MathCaptureViewController 中显示的按钮

let requiredButtons: [MathCaptureButton]

 public enum MathCaptureButton {
    /// Back button
    case back
    /// Flash button
    case flash
}

快门的按钮大小

let bigButtonSize: CGSize

按钮的大小

let smallButtonSize: CGSize

裁剪区域的内边距

let cropAreaInsets: UIEdgeInsets

如果启用,则错误将由捕获控制器处理

let errorHandling: Bool

MathCaptureViewController

您可以继承它以获得更多控制。请参阅示例应用程序。

错误处理

对于简单的内部错误处理,请在 MathCaptureProperties 中添加 errorHandling: true 参数。
您在回调中可能遇到的错误表示为两种主要类型

NetworkError

如果网络失败,将抛出错误类型

public enum NetworkError: Error {

    /// Unknown or not supported error.
    case unknown
    
    /// Not connected to the internet.
    case notConnectedToInternet
    
    /// International data roaming turned off.
    case internationalRoamingOff
    
    /// Cannot reach the server.
    case notReachedServer
    
    /// Connection is lost.
    case connectionLost
    
    /// Incorrect data returned from the server.
    case incorrectDataReturned
    
    /// Request canceled.
    case requestCanceled
}

RecognitionError

如果识别失败,将抛出错误类型

public enum RecognitionError: Error {

    /// Failed parse JSON, incorrect data returned
    case failedParseJSON
    
    /// Server can't recognize image as math
    case notMath(description: String)
    
    /// Invalid credentials, set correct api keys
    case invalidCredentials
}

示例错误处理

MathpixClient.launchCamera(source: self,
                           outputFormats: [FormatLatex.simplified],
                           completion:
                                { (error, result) in
                                    if let error = error as? NetworkError {
                                        handleNetworkError(error)
                                    } else if let error = error as? RecognitionError {
                                        handleRecognitionError(error)
                                    } else if let error = error {
                                        handleOtherError(error)
                                    }
                                    ...
})

本地化

要设置标签或错误消息,请将 Localizable.strings 文件添加到您的项目中(如果没有的话),然后更改值。

// Errors
"Error credintials title" = "Error";
"Error credintials messages" = "Invalid credentials";
"Error capture title" = "Error capture";
"Error capture message" = "Capture image error";
"Error timeout title" = "Timeout error";
"Error timeout message" = "Send image timeout";
"Error no connection tittle" = "Network error";
"Error no connection message" = "No internet connection";
"Error parse title" = "Error parse";
"Error parse message" = "Error parse json";

// Tap info label
"Tap info label text" = "Tap anywhere to take a picture";

许可证

Mathpix 客户端在 MIT 许可下提供。有关更多信息,请参阅 LICENSE 文件。