|
@@ -9,10 +9,11 @@ import Foundation
|
|
|
|
|
|
@objc(KMGOCROperationDelegate)
|
|
|
protocol KMGOCROperationDelegate: AnyObject {
|
|
|
- @objc optional func GOCROperationCancel(_ op: KMGOCROperation, atIndex: Int)
|
|
|
- @objc optional func GOCROperationStart(_ op: KMGOCROperation, atIndex: Int)
|
|
|
- @objc optional func GOCROperationFinish(_ op: KMGOCROperation, atIndex: Int, results: Array<Any>)
|
|
|
- @objc optional func GOCROperationFail(_ op: KMGOCROperation, atIndex: Int, err: Error?)
|
|
|
+ @objc optional func GOCROperation(_ operation: KMGOCROperation, cancelOCRImageAtIndex index: Int)
|
|
|
+ @objc optional func GOCROperation(_ operation: KMGOCROperation, startOCRImageAtIndex index: Int)
|
|
|
+ @objc optional func GOCROperation(_ operation: KMGOCROperation, finishOCRImageAtIndex index: Int, results: Array<Any>)
|
|
|
+ @objc optional func GOCROperation(_ operation: KMGOCROperation, failureOCRImageAtIndex index: Int, error: Error?)
|
|
|
+
|
|
|
}
|
|
|
|
|
|
let KMGOC_API_URL = "https://vision.googleapis.com/v1/images:annotate"
|
|
@@ -26,36 +27,18 @@ let KMGOC_API_KEY = "AIzaSyCJuqJ9YvtkFKMl1mW3Yq-av3mmI9ScbRY"
|
|
|
|
|
|
@objcMembers class KMGOCROperation: Operation{
|
|
|
@objc var operationDelegate: KMGOCROperationDelegate?
|
|
|
+ var hasCanceled: Bool = false
|
|
|
+ var hasFinished: Bool = false
|
|
|
+ var hasExcuting: Bool = false
|
|
|
var selectedLanguages: Array<Any>?
|
|
|
override var isExecuting: Bool{
|
|
|
- set{
|
|
|
- self.willChangeValue(forKey: "isExecuting")
|
|
|
- self.isExecuting = newValue
|
|
|
- didChangeValue(forKey: "isExecuting")
|
|
|
- }
|
|
|
- get{
|
|
|
- return super.isExecuting
|
|
|
- }
|
|
|
+ return self.hasExcuting
|
|
|
}
|
|
|
override var isFinished: Bool{
|
|
|
- set{
|
|
|
- self.willChangeValue(forKey: "isFinished")
|
|
|
- self.isFinished = newValue
|
|
|
- didChangeValue(forKey: "isFinished")
|
|
|
- }
|
|
|
- get{
|
|
|
- return super.isFinished
|
|
|
- }
|
|
|
+ return self.hasFinished
|
|
|
}
|
|
|
override var isCancelled: Bool{
|
|
|
- set{
|
|
|
- self.willChangeValue(forKey: "isCancelled")
|
|
|
- self.isCancelled = newValue
|
|
|
- didChangeValue(forKey: "isCancelled")
|
|
|
- }
|
|
|
- get{
|
|
|
- return super.isCancelled
|
|
|
- }
|
|
|
+ return self.hasCanceled
|
|
|
}
|
|
|
|
|
|
var fileName: String = ""
|
|
@@ -70,8 +53,8 @@ let KMGOC_API_KEY = "AIzaSyCJuqJ9YvtkFKMl1mW3Yq-av3mmI9ScbRY"
|
|
|
self.orcImage = recognitionImg
|
|
|
self.queuePriority = .normal
|
|
|
self.name = self.fileName
|
|
|
- self.isExecuting = false
|
|
|
- self.isFinished = false
|
|
|
+ self.hasExcuting = false
|
|
|
+ self.hasFinished = false
|
|
|
}
|
|
|
func fileNameWithDate() -> String {
|
|
|
let formatter = DateFormatter()
|
|
@@ -83,22 +66,24 @@ let KMGOC_API_KEY = "AIzaSyCJuqJ9YvtkFKMl1mW3Yq-av3mmI9ScbRY"
|
|
|
|
|
|
override func start() {
|
|
|
if p_checkCancelled() { return }
|
|
|
- isExecuting = true
|
|
|
+ willChangeValue(forKey: "isExecuting")
|
|
|
+ self.hasExcuting = true
|
|
|
+ didChangeValue(forKey: "isExecuting")
|
|
|
|
|
|
- Thread.detachNewThreadSelector(#selector(main), toTarget: self, with: nil)
|
|
|
- }
|
|
|
- @objc override func main() {
|
|
|
- do {
|
|
|
+// Thread.detachNewThreadSelector(#selector(main), toTarget: self, with: nil)
|
|
|
+// }
|
|
|
+// @objc override func main() {
|
|
|
+// do {
|
|
|
if p_checkCancelled() { return }
|
|
|
recognitionImage(orcImage ?? NSImage())
|
|
|
- while isExecuting {
|
|
|
- if p_checkCancelled() {
|
|
|
- return
|
|
|
- }
|
|
|
- }
|
|
|
- } catch let e {
|
|
|
- Swift.debugPrint("Exception %@", e)
|
|
|
- }
|
|
|
+// while isExecuting {
|
|
|
+// if p_checkCancelled() {
|
|
|
+// return
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } catch let e {
|
|
|
+// Swift.debugPrint("Exception %@", e)
|
|
|
+// }
|
|
|
}
|
|
|
override func cancel() {
|
|
|
super.cancel()
|
|
@@ -107,34 +92,40 @@ let KMGOC_API_KEY = "AIzaSyCJuqJ9YvtkFKMl1mW3Yq-av3mmI9ScbRY"
|
|
|
task = nil
|
|
|
}
|
|
|
|
|
|
- self.operationDelegate?.GOCROperationCancel?(self, atIndex: self.imageIndex)
|
|
|
-
|
|
|
+ self.operationDelegate?.GOCROperation?(self, cancelOCRImageAtIndex: self.imageIndex)
|
|
|
if isExecuting {
|
|
|
- isExecuting = false
|
|
|
- isFinished = true
|
|
|
+ willChangeValue(forKey: "isFinished")
|
|
|
+ hasFinished = true
|
|
|
+ didChangeValue(forKey: "isFinished")
|
|
|
} else {
|
|
|
- isFinished = false
|
|
|
+ willChangeValue(forKey: "isCancelled")
|
|
|
+ hasCanceled = true
|
|
|
+ didChangeValue(forKey: "isCancelled")
|
|
|
}
|
|
|
- isCancelled = true
|
|
|
}
|
|
|
func p_done() {
|
|
|
- isExecuting = false
|
|
|
- isFinished = true
|
|
|
+ self.willChangeValue(forKey: "isFinished")
|
|
|
+ self.hasFinished = true
|
|
|
+ self.didChangeValue(forKey: "isFinished")
|
|
|
}
|
|
|
func p_checkCancelled() -> Bool {
|
|
|
if isCancelled {
|
|
|
- isFinished = true
|
|
|
+ willChangeValue(forKey: "isFinished")
|
|
|
+ willChangeValue(forKey: "isExecuting")
|
|
|
+ hasExcuting = false
|
|
|
+ hasFinished = true
|
|
|
+ didChangeValue(forKey: "isExecuting")
|
|
|
+ didChangeValue(forKey: "isFinished")
|
|
|
return true
|
|
|
}
|
|
|
return false
|
|
|
}
|
|
|
func recognitionImage(_ image: NSImage) {
|
|
|
- self.operationDelegate?.GOCROperationStart?(self, atIndex: self.imageIndex)
|
|
|
-
|
|
|
+ self.operationDelegate?.GOCROperation?(self, startOCRImageAtIndex: self.imageIndex)
|
|
|
let binaryImageData = base64EncodeImage(image)
|
|
|
|
|
|
if binaryImageData == nil {
|
|
|
- self.operationDelegate?.GOCROperationFail?(self, atIndex: self.imageIndex, err: nil)
|
|
|
+ self.operationDelegate?.GOCROperation?(self, failureOCRImageAtIndex: self.imageIndex, error: nil)
|
|
|
return
|
|
|
}
|
|
|
let urlString = "\(KMGOC_API_URL)?key=\(KMGOC_API_KEY)"
|
|
@@ -166,9 +157,9 @@ let KMGOC_API_KEY = "AIzaSyCJuqJ9YvtkFKMl1mW3Yq-av3mmI9ScbRY"
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
if error != nil || results == nil {
|
|
|
- self.operationDelegate?.GOCROperationFail?(self, atIndex: self.imageIndex, err: error)
|
|
|
+ self.operationDelegate?.GOCROperation?(self, failureOCRImageAtIndex: self.imageIndex, error: error)
|
|
|
} else {
|
|
|
- self.operationDelegate?.GOCROperationFinish?(self, atIndex: self.imageIndex, results: results ?? [])
|
|
|
+ self.operationDelegate?.GOCROperation?(self, finishOCRImageAtIndex: self.imageIndex, results: results ?? [])
|
|
|
}
|
|
|
}
|
|
|
if error != nil || results == nil {
|
|
@@ -214,11 +205,16 @@ let KMGOC_API_KEY = "AIzaSyCJuqJ9YvtkFKMl1mW3Yq-av3mmI9ScbRY"
|
|
|
return compressImageData
|
|
|
}
|
|
|
func responseDataResults(_ dictionary: NSDictionary) -> [Any]? {
|
|
|
- guard let responses = dictionary["responses"] as? [Any],
|
|
|
- let responseData = responses.first as? NSDictionary,
|
|
|
- let errorObj = dictionary["error"] as? NSDictionary else { return nil }
|
|
|
+ let responses = dictionary["responses"] as? [Any]
|
|
|
+ if responses == nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ let responseData = responses?.first as? NSDictionary
|
|
|
+ if let errorObj = dictionary["error"] as? NSDictionary {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
var results: [Any]? = nil
|
|
|
- if let textAnnotations = responseData["textAnnotations"] as? [Any] {
|
|
|
+ if let textAnnotations = responseData?["textAnnotations"] as? [Any] {
|
|
|
results = [Any]()
|
|
|
for annotation in textAnnotations {
|
|
|
var textBounds = CGRect.zero
|