QR Scanner

Thank you!

BTW, how is that NFC plugin coming along?  :slight_smile:

I will work on it in September, but first I need to find a phone with NFC support. Mine doesn’t have it.

Well, I guess things are a bit difficult without a device to test on…

In the meantime, I have a question about the QR scanner.

Since the scanner takes a few seconds to start, I show a little message/symbol just to show the user that something is going on. The plan was to remove this message/symbol in the qr listener function, but as it turns out, this is not called if the user aborts the scanning (left arrow or back button (Android)).

Now, this is not a big issue for me personally as I can use a timer and auto remove the message/symbol.

But maybe the listener should be called anyway (with message = nil or something)? Just so that the caller app has an idea about an aborted scan?

I tried making an event argument with event.isError, event.errorMessage and event.message, but this over complicates the plugin code. My goal is to make many plugins in minimal time, that assumes less functionality for now. But later on, for sure I will add more functionality.

I could make another listener onCancel() or something, but that’s ugly.

For now, timer function to hide your message is most concise solution.

I don’t think the event.xxx system complicates anything. Well, maybe on your side…  :slight_smile:

Actually I think it is much cleaner that what I have to do now. I’ve tried to time the message but the timer seems to be halted by the plugin? Is this possible? I know next to nothing about corona plugins.

Anyway, this means that if the message is not removed before the plugin shows/starts, it will be visible even after the plugin is finished, which looks bad.

I had to set the timer to 300ms to be sure that the message is removed before the plugin starts. I’m not very comfortable with this way of working though. Maybe the timing is different on other devices than mine and 300ms is not a good number.

Yeah, I was talking about ObjC and Java sides. Trivial things may be very complicated there.

Actually, there is a possibility that a system event will be fired when QR Scanner is closed.
https://docs.coronalabs.com/api/event/system/type.html
applicationResume that is, I don’t have time to check at the moment, but you can try.

Indeed it does! A great tip! Everything works smoothly now. Thanks, Lerg!

@runewinse,

Would you be willing to share the code with the rest of the community as it seems a useful addition to the plugin?

What part of the code are you thinking of? The message part that is hidden by the applicationResume event?

The mechanism you employed to display a message before the Plugin starts and then remove it afterwards.

(thinking about it you probably could have used Sergey’s new Toast plugin to show the message)

Ok, sure…

The message/removal part is the qrReturn() and onSystemEvent() functions. The rest is stuffing. I just use the native.alert for showing the message. Surely there are more fancy ways of displaying that the QR scanner is about to start, but the auto removal mechanism is the same.

I have added an extra variable (waitingForQRScanner) so that the qrReturn() is only called in the event of a return from the scanner and not each time the app gets focus. It’s probably not needed though…

local qrscanner = require('plugin.qrscanner') \_W = display.contentWidth \_H = display.contentHeight \_CENTER\_X = \_W/2 \_CENTER\_Y = \_H/2 -- Forward declarations local waitingForQRScanner = false local qrAlert = nil local qrText = nil -------------------------------------------------------------------------------- QR scanner functionality - START local function qrReturn() waitingForQRScanner = false if (qrAlert ~= nil) then native.cancelAlert(qrAlert) end end local function qrListener(message) qrText.text = message qrReturn() end local function qrButHit() waitingForQRScanner = true qrAlert = native.showAlert("Info", "Starting QR scanner") qrscanner.show(qrListener) end -------------------------------------------------------------------------------- QR scanner functionality - START -------------------------------------------------------------------------------- QR button/text setup - START local qrButtonRect = display.newRect(\_CENTER\_X, \_CENTER\_Y, \_W\*0.75, \_H\*0.1) local qrButtonText = display.newText("Start QR scanner", \_CENTER\_X, \_CENTER\_Y) qrButtonText:setFillColor(0) local function qrButtonHandler( event ) if (event.phase == "began") then qrButHit() return true end end qrButtonRect:addEventListener( "touch", qrButtonHandler ) qrText = display.newText("Scanned QR code goes here", \_CENTER\_X, \_H\*0.7) -------------------------------------------------------------------------------- QR button/text setup - END ----------------------------------------------------------- Setup applicationResume trigger - START local function onSystemEvent(event) if (event.name == "system" and event.type == "applicationResume" and waitingForQRScanner == true) then qrReturn() end end Runtime:addEventListener( "system", onSystemEvent ) ----------------------------------------------------------- Setup applicationResume trigger - END

I still have not received any email to download this. Is that what everyone else gets?

To keep this thread on-topic, I have moved your question and provided an answer here:

https://forums.coronalabs.com/topic/58784-login-instructions-email/

Great job on the plugin. It works great on my daughters samsung galaxy phone but I am having an issue with my two other android phones. Both have the same problem and both are running android version 4.1.2. When I press the button to start the scan both phones display the message “This device does not have a camera.”

Phone 1

droid razr (xt912) -> android version 4.1.2

Phone 2 

Lg (Lgl39c) -> android version 4.1.2

Thanks! Must be fixed, wait for the plugin to be synced and try again.

Hello Sergey, this works flawlessly on my BLU phone, but I am seeing the “This device does not have a camera” message on an Amazon Kindle Fire HD 8.9" tablet.  Please note this device only has a front-facing camera – perhaps it is not sufficient for QR scanning.  media.hasSource( media.Camera) succeeds as well as media.capturePhoto.

Can you please tell me on what Android version it’s OS is based on?

I believe it is 4.0.3

I’ve just uploaded a possible fix. Old androids are very tricky when it comes to detecting cameras.
Please try again when you see the plugin has been redownloaded in the simulator.