[ad_1]
import UIKit
import Imaginative and prescient
class ViewController: UIViewController, ImageGet {
//MARK: OUTLETS
@IBOutlet weak var selectButton: UIButton!
//MARK: VARIABLES
var objU = UtilityClass()
var picture:UIImage?
var str:String?
var uiButton : UIButton?
var arrayString = [String]()
var imageView : UIImageView = UIImageView()
//MARK: DELEGATE FUNCTION
func img(picture: UIImage) {
self.picture = picture
imageView.picture = picture
setUp()
}
override func viewDidLoad() {
tremendous.viewDidLoad()
imageView.isUserInteractionEnabled = true
// Do any extra setup after loading the view.
}
//MARK: SETUPUI
func setUp() {
let realImg = resizeImage(picture: (imageView.picture!) , targetSize:CGSize(width: view.body.width, peak: view.body.peak) )
self.picture = realImg
self.imageView .picture = self.picture
imageView.isUserInteractionEnabled = true
self.imageView.body = CGRect(x: 0, y: 0, width: realImg.dimension.width, peak: realImg.dimension.peak)
view.addSubview(imageView)
guard let cgimg = realImg.cgImage else {return}
let requestHandler = VNImageRequestHandler(cgImage: cgimg)
let req = VNRecognizeTextRequest(completionHandler: recognizeTextHandler)
req.recognitionLevel = .correct
do {
attempt requestHandler.carry out([req])
} catch {
print("Unable to carry out the request: (error)")
}
}
//MARK: SELECT THE IMAGE
@IBAction func selectButtontapped(_ sender: Any) {
objU.delegate = self
objU.obj = self
objU.ImageGet()
}
func recognizeTextHandler(request : VNRequest , error:Error?) {
guard let remark = request.outcomes as? [VNRecognizedTextObservation], error == nil else {
return
}
_ = remark.compactMap({
$0.topCandidates(1).first?.string
}).joined(separator: "/n")
for subView in imageView.subviews {
subView.removeFromSuperview()
}
let boundingRect :[CGRect] = remark.compactMap{
remark in
guard let candidate = remark.topCandidates(1).first else {return .zero}
//discover the bounding field remark
let stringRange = candidate.string.startIndex..<candidate.string.endIndex
let boxObservation = attempt? candidate.boundingBox(for: stringRange)
let boundingBox = boxObservation?.boundingBox ?? .zero
str = candidate.string
self.arrayString.append(str!)
let rectInImg = VNImageRectForNormalizedRect(boundingBox, Int((imageView.body.dimension.width)), Int((imageView.body.dimension.peak)))
let convertedRect = self.getConvertedRect(boundingBox: remark.boundingBox, inImage:picture!.dimension , containedIn: (imageView.bounds.dimension))
drawBoundBox(rect: convertedRect)
return rectInImg
}
print(arrayString)
print(boundingRect)
}
func drawBoundBox(rect: CGRect) {
uiButton = UIButton(kind: .customized)
uiButton?.body = rect
uiButton?.layer.borderColor = UIColor.systemPink.cgColor
uiButton?.setTitle("", for: .regular)
uiButton?.layer.borderWidth = 2
uiButton?.tag = arrayString.rely
imageView.addSubview(uiButton ?? UIButton())
uiButton?.addTarget(self, motion: #selector(pressed(_:)), for: .touchUpInside)
}
@objc func pressed(_ sender : UIButton) {
alert(key: arrayString[sender.tag - 1])
}
//MARK: CONVERT THE NORMALISED BOUNDING RECT
func getConvertedRect(boundingBox: CGRect, inImage imageSize: CGSize, containedIn containerSize: CGSize) -> CGRect {
let rectOfImage: CGRect
let imageAspect = imageSize.width / imageSize.peak
let containerAspect = containerSize.width / containerSize.peak
if imageAspect > containerAspect { /// picture extends left and proper
let newImageWidth = containerSize.peak * imageAspect /// the width of the overflowing picture
let newX = -(newImageWidth - containerSize.width) / 2
rectOfImage = CGRect(x: newX, y: 0, width: newImageWidth, peak: containerSize.peak)
} else { /// picture extends prime and backside
let newImageHeight = containerSize.width * (1 / imageAspect) /// the width of the overflowing picture
let newY = -(newImageHeight - containerSize.peak) / 2
rectOfImage = CGRect(x: 0, y: newY, width: containerSize.width, peak: newImageHeight)
}
let newOriginBoundingBox = CGRect(
x: boundingBox.origin.x,
y: 1 - boundingBox.origin.y - boundingBox.peak,
width: boundingBox.width,
peak: boundingBox.peak
)
var convertedRect = VNImageRectForNormalizedRect(newOriginBoundingBox, Int(rectOfImage.width), Int(rectOfImage.peak))
/// add the margins
convertedRect.origin.x += rectOfImage.origin.x
convertedRect.origin.y += rectOfImage.origin.y
return convertedRect
}
//MARK: RESIZE THE IMAGE ACCORD TO DEVICE
func resizeImage(picture: UIImage, targetSize: CGSize) -> UIImage {
let dimension = picture.dimension
let widthRatio = targetSize.width / picture.dimension.width
let heightRatio = targetSize.peak / picture.dimension.peak
// Work out what our orientation is, and use that to type the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: dimension.width * heightRatio, peak: dimension.peak * heightRatio)
} else {
newSize = CGSize(width: dimension.width * widthRatio, peak: dimension.peak * widthRatio)
}
// That is the rect that we have calculated out and that is what is definitely used beneath
let rect = CGRect(x: 0, y: 0, width: newSize.width, peak: newSize.peak)
// Truly do the resizing to the rect utilizing the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
picture.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
//MARK: POPPING ALERT WITH STRING
func alert(key:String){
let alertController = UIAlertController(title: "String", message: key, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", fashion: .default) {
(motion: UIAlertAction!) in
// Code on this block will set off when OK button tapped.
}
let copyAction = UIAlertAction(title: "Copy", fashion: .default) {
(motion: UIAlertAction!) in
UIPasteboard.normal.string = key
}
alertController.addAction(copyAction)
alertController.addAction(OKAction)
self.current(alertController, animated: true, completion: nil)
}
}
[ad_2]
