Thursday, February 2, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Swift

Capturing Text within Image Using Live Text API and SwiftUI

learningcode_x1mckf by learningcode_x1mckf
September 8, 2022
in Swift
0
Capturing Text within Image Using Live Text API and SwiftUI
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


Final yr, iOS 15 got here with a really helpful function often called Live Text. You will have heard of the time period OCR (quick for Optical Character Recognition), which is the method for changing a picture of textual content right into a machine-readable textual content format. That is what Reside Textual content is about.

You might also like

The abstract Vapor service factory design pattern

SwiftNIO tutorial – The echo server

Introducing – Vapor cheatsheet – The.Swift.Dev.

Reside Textual content is built-into the digital camera app and Pictures app. Should you haven’t tried out this function, merely open the Digital camera app. While you level the system’s digital camera at a picture of textual content, you will see that a Reside Textual content button on the lower-right nook. By tapping the button, iOS routinely captures the textual content for you. You’ll be able to then copy and paste it into different functions (e.g. Notes).

This can be a very highly effective and handy options for many customers. As a developer, wouldn’t or not it’s nice in case you can incorporate this Reside Textual content function in your individual app? In iOS 16, Apple launched the Reside Textual content API for builders to energy their apps with Reside Textual content. On this tutorial, let’s see methods to use the Reside Textual content API with SwiftUI.

Allow Reside Textual content Utilizing DataScannerViewController

Within the WWDC session about Capturing Machine-readable Codes and Text with VisionKit, Apple’s engineer confirmed the next diagram:

avfoundation-and-vision-framework

Textual content recognization will not be a brand new function on iOS 16. On older model of iOS, you should utilize APIs from the AVFoundation and Imaginative and prescient framework to detect and acknowledge textual content. Nevertheless, the implementation is kind of difficult, particularly for individuals who are new to iOS improvement.

Within the subsequent launch of iOS, all of the above is simplified to a brand new class known as DataScannerViewController in VisionKit. By utilizing this view controller, your app can routinely show a digital camera UI with the Reside Textual content functionality.

To make use of the category, you first import the VisionKit framework after which examine if the system helps the info scanner function:

DataScannerViewController.isSupported

The Reside Textual content API solely helps gadgets launched in 2018 or newer with Neural engine. On prime of that, you additionally have to examine the supply to see if the consumer approves the usage of information scanner:

DataScannerViewController.isAvailable

As soon as each checks come again with constructive outcomes, you might be prepared to begin the scanning. Right here is the pattern code to launch a digital camera with Reside Textual content:

let dataScanner = DataScannerViewController(

                     recognizedDataTypes: [.text()],

                     qualityLevel: .balanced,

                     isHighlightingEnabled: true

                  )

 

current(dataScanner, animated: true)

    strive? dataScanner.startScanning()

All you want is create an occasion of DataScannerViewController and specify the acknowledged information sorts. For textual content recognition, you move .textual content() as the info sort. As soon as the occasion is prepared, you’ll be able to current it and begin the scanning course of by calling the startScanning() methodology.

Working with DataScannerViewController in SwiftUI

The DataScannerViewController class now solely helps UIKit. For SwiftUI, it wants a bit of labor. We now have to undertake the UIViewControllerRepresentable protocol to make use of the category in SwiftUI initiatives. On this case, I create a brand new struct named DataScanner like this:

struct DataScanner: UIViewControllerRepresentable

  .

  .

  .

This struct accepts two binding variables: one for triggering the info scanning and the opposite is a binding for storing the scanned textual content.

@Binding var startScanning: Bool

@Binding var scanText: String

To efficiently undertake the UIViewControllerRepresentable protocol, we have to implement the next strategies:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

func makeUIViewController(context: Context) –> DataScannerViewController

    let controller = DataScannerViewController(

                        recognizedDataTypes: [.text()],

                        qualityLevel: .balanced,

                        isHighlightingEnabled: true

                    )

 

    return controller

 

func updateUIViewController(_ uiViewController: DataScannerViewController, context: Context)

 

    if startScanning

        strive? uiViewController.startScanning()

     else

        uiViewController.stopScanning()

    

Within the makeUIViewController methodology, we return an occasion of DataScannerViewController. For updateUIViewController, we begin (or cease) the scanning relying on the worth of startScanning.

To seize the scanned textual content, we now have to undertake the next methodology of DataScannerViewControllerDelegate:

func dataScanner(_ dataScanner: DataScannerViewController, didTapOn merchandise: RecognizedItem)

  .

  .

  .

The tactic known as when the consumer faucets the detected textual content, so we are going to implement it like this:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

class Coordinator: NSObject, DataScannerViewControllerDelegate

    var mum or dad: DataScanner

 

    init(_ mum or dad: DataScanner)

        self.mum or dad = mum or dad

    

 

    func dataScanner(_ dataScanner: DataScannerViewController, didTapOn merchandise: RecognizedItem)

        swap merchandise

        case .textual content(let textual content):

            mum or dad.scanText = textual content.transcript

        default: break

        

    

 

 

func makeCoordinator() –> Coordinator

    Coordinator(self)

We examine the acknowledged merchandise and retailer the scanned textual content if any textual content is acknowledged. Lastly, insert this line of code within the makeUIViewController methodology to configure the delegate:

controller.delegate = context.coordinator

This controller is now prepared to be used in SwiftUI views.

Capturing Textual content Utilizing DataScanner

For example, we are going to construct a textual content scanner app with a quite simple consumer interface. When the app is launched, it routinely shows the digital camera view for reside textual content. When textual content is detected, customers can faucet the textual content to seize it. The scanned textual content is displayed within the decrease a part of the display screen.

swiftui-live-text-demo

Assuming you’ve created a normal SwiftUI challenge, open ContentView.swift and the VisionKit framework.

Subsequent, declare a few state variables to manage the operation of the info scanner and the scanned textual content.

@State personal var startScanning = false

@State personal var scanText = “”

For the physique half, let’s replace the code like this:

VStack(spacing: 0)

    DataScanner(startScanning: $startScanning, scanText: $scanText)

        .body(top: 400)

 

    Textual content(scanText)

        .body(minWidth: 0, maxWidth: .infinity, maxHeight: .infinity)

        .background(in: Rectangle())

        .backgroundStyle(Shade(uiColor: .systemGray6))

 

.activity

    if DataScannerViewController.isSupported && DataScannerViewController.isAvailable

        startScanning.toggle()

    

We begin the info scanner when the app launches. However earlier than that, we name DataScannerViewController.isSupported and DataScannerViewController.isAvailable to make sure Reside Textual content is supported on the system.

The demo app is nearly prepared to check. Since Reside Textual content requires Digital camera entry, please bear in mind to go to the challenge configuration. Add the important thing Privateness – Digital camera Utilization Description within the Data.plist file and specify the rationale why your app must entry the system’s digital camera.

xcode-info-plist-enable-camera-access

After the modifications, you’ll be able to deploy the app to an actual iOS system and check the Reside Textual content operate.

Aside from English, Reside Textual content additionally helps French, Italian, German, Spanish, Chinese language, Portuguese, Japanese, and Korean.

swiftui-live-text-api-demo-chinese

For the supply code of this demo challenge, you’ll be able to check it out on GitHub.

Observe: We’re updating our Mastering SwiftUI book for iOS 16. If you wish to begin studying SwiftUI, try the e-book here. You’ll obtain a free replace later this yr.





Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

The abstract Vapor service factory design pattern

by learningcode_x1mckf
February 1, 2023
0
The abstract Vapor service factory design pattern

I've written a number of articles about manufacturing unit design patterns on my weblog and this time I might like to speak a couple of particular one, which...

Read more

SwiftNIO tutorial – The echo server

by learningcode_x1mckf
January 27, 2023
0
SwiftNIO tutorial – The echo server

Intoducing SwiftNIO In the event you used a excessive degree net framework, corresponding to Vapor, up to now, you would possibly had some interplay with occasion loops...

Read more

Introducing – Vapor cheatsheet – The.Swift.Dev.

by learningcode_x1mckf
January 23, 2023
0
Introducing – Vapor cheatsheet – The.Swift.Dev.

Out there on Gumroad Thanks for supporting my work by buying the cheatsheet. 🙏 Download now A whole Vapor framework reference for novices. greater than...

Read more

iCloud Programming Tutorial for iOS: An Introduction

by learningcode_x1mckf
January 18, 2023
0
iCloud Programming Tutorial for iOS: An Introduction

Editor’s observe: This week, we work with Ziad Tamim once more to provide you an introduction of iCloud programming. You’ll learn to save and retrieve knowledge from iCloud.On...

Read more

Easy multipart file upload for Swift

by learningcode_x1mckf
January 18, 2023
0
Easy multipart file upload for Swift

I imagine that you've got already heard in regards to the well-known multipart-data add method that everybody likes to add recordsdata and submit type knowledge, but when not,...

Read more
Next Post
Quick Tip: How to Convert Numbers to Ordinals in JavaScript

Quick Tip: How to Convert Numbers to Ordinals in JavaScript

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Mastering Bitrise workflows for continuous iOS app delivery

Mastering Bitrise workflows for continuous iOS app delivery

September 30, 2022
Java’s JOptionPane showOptionDialog by Example

User input with a Java JOptionPane example

November 3, 2022
How to Append a JavaScript String to the DOM? – Medium

How to Append a JavaScript String to the DOM? – Medium

December 11, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java :Full Stack Developer – Western Cape saon_careerjunctionza_state
  • Pay What You Want for this Learn to Code JavaScript Certification Bundle
  • UPB Java Jam brings coffeehouse vibes to Taylor Down Under | Culture

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?