IOS AV Foundation qr code scan 04 to read two-dimensional code content using synthetic speech

Source: Internet
Author: User

In the previous section, we provided a visual display of the QR code that was recognized by the program, and in this section we used synthetic speech to read the contents of the QR code scanned.

Modify the VIEWCONTROLLER.M, define and initialize the following instance variables:

Avspeechsynthesizer *_speechsynthesizer;
_speechsynthesizer = [[Avspeechsynthesizer alloc] init];
Initializing the speech synthesizer is straightforward. The speech synthesizer controls the playback and sequencing of each voice data. When initialization is complete, Metadata output triggers the speech synthesizer to read the contents of the scanned QR code.

tracking the change of QR code

Add the following code to the CaptureOutput:didOutputMetadataObjects:fromConnection: start location:

Nsset *originalbarcodes = [Nsset setwitharray:_barcodes.allvalues];
The goal is to store all detected QR codes before processing a new frame. Used to compare the two-dimensional code that has been cached with the newly detected QR code.
Add the following code to the Enumerateobjectsusingblock "}"; After:

    Nsmutableset *newbarcodes = [Foundbarcodes mutablecopy];    [Newbarcodes Minusset:originalbarcodes];
This code uses the subtraction of the collection to remove the already cached QR code, leaving only the newly scanned QR code.

Finally, we use the collection operation again to remove the QR code that is not already in the screen range and update the _barcode dictionary:

    Nsmutableset *gonebarcodes = [Originalbarcodes mutablecopy];    [Gonebarcodes minusset:foundbarcodes];    [Gonebarcodes enumerateobjectsusingblock: ^ (Barcode *barcode, BOOL *stop) {        [_barcodes removeobjectforkey: Barcode.metadataObject.stringValue];    }];

Create a "speaking style"

Next, we set "talking mode" for all QR code data, including frequency, volume, and pitch. Finally call Speakutterace: read out the contents of the QR code:

                Speak the new barcodes        [Newbarcodes enumerateobjectsusingblock:^ (Barcode *barcode, BOOL *stop) {            Avspeechutterance *utterance = [[Avspeechutterance alloc] initWithString:barcode.metadataObject.stringValue];            Utterance.rate = Avspeechutteranceminimumspeechrate + ((Avspeechutterancemaximumspeechrate- avspeechutteranceminimumspeechrate) * 0.5f);            Utterance.volume = 1.0f;            Utterance.pitchmultiplier = 1.2f;                        [_speechsynthesizer speakutterance:utterance];        }];

Modify the Startrunning method to enable Audiosession:

    [[Avaudiosession sharedinstance] Setcategory:avaudiosessioncategoryplayback withoptions:0 Error:nil];    [[Avaudiosession sharedinstance] Setactive:yes Error:nil];

Modify the Stoprunning method to disable audio audiosession:

    [[Avaudiosession sharedinstance] Setactive:no Error:nil];
Compile and run, when the program recognizes the QR code, the content of the two-dimensional code will be read out with the voice.

In the next section, we'll add image scaling to the program.

Reprint Please specify source: http://blog.csdn.net/yamingwu/article/details/44535513


IOS AV Foundation qr code scan 04 to read two-dimensional code content using synthetic speech

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.