barcode-scanner icon indicating copy to clipboard operation
barcode-scanner copied to clipboard

Idea: Try an implementation with Googles ML Kit

Open thegnuu opened this issue 2 years ago • 22 comments

Due to some issues which are hard to resolve in a cross-platform manner, it might make sense to create a PoC with ML Kit.

https://developers.google.com/ml-kit/vision/barcode-scanning

  • This should enhance 1D barcode detection (#105)
  • Allow a client-side implementation of a frame with the help of cornerPoints (#10)
  • Would simplify the development since the same library can be used on iOS and Android

It might be absolutely possible that after the PoC we find out that it is not working better, but at it least it is worth trying since it should not be too much work to create a test plugin :)

thegnuu avatar Mar 03 '22 10:03 thegnuu

@thegnuu Looks interesting! The samples provided in the MLKit docs are already pretty close to what the plugin should provide, so indeed should be pretty doable to implement a PoC.

JanMisker avatar Mar 04 '22 10:03 JanMisker

Indeed yes, I already played around with the example app and the results are really promising so far.

It will take a few hours to create a PoC though, at least for me since I am not as fluent in Swift/Java as in other languages, but I should find some time in the next two weeks to tinker around with it 👍🏼

thegnuu avatar Mar 04 '22 10:03 thegnuu

It will take a few hours to create a PoC though, at least for me since I am not as fluent in Swift/Java as in other languages, but I should find some time in the next two weeks to tinker around with it 👍🏼

I'm quite comfortable with Swift; if you can do an Android PoC implementation I could use that as a guide to make the iOS one.

JanMisker avatar Mar 04 '22 14:03 JanMisker

Sounds great! I spent some time today to read some examples how all of this has to be done...

Unfortunately I was not able to start working on it, since I was not really sure where to start... I think the easiest way will be to create a completely new plugin to work on this one and move it to this repo once most of the work is done...

This seems to be a good and simple example to start the android implementation.

thegnuu avatar Mar 06 '22 16:03 thegnuu

Pretty cool idea. Wouldn't this also allow to detect multiple codes at the same time and send and update the position of the detected codes? In combination with the overlayed webview, this would allow to augment the barcodes with related content within the webview.

phal0r avatar Mar 11 '22 18:03 phal0r

@phal0r Exactly yes! This is one of the reasons why I think this is a good idea. It would allow us to detect any barcode within the camera frame and report back all of those with the position where they are detected.

thegnuu avatar Mar 11 '22 18:03 thegnuu

@thegnuu yeah, so what does it need to move this forward?

phal0r avatar Mar 11 '22 20:03 phal0r

@phal0r Right now, everything ;)

I played around with the demo projects of ML Kit and thought about implementing a simple PoC first, but I realised that once the PoC is implemented most of the critical parts are already done...

As I already told, I am not often developing native code. @JanMisker offered help on the iOS/Swift part of the implementation once the Android part is done, so I will try to focus on the Android part. If you @phal0r have some Android/Java/Kotlin experience and could offer some help, that would totally speed up the process. I have a lot of other projects running at the moment and it might take me some weeks to get this done (especially because I am not really fast at coding this stuff at the moment)...

I can create a branch this week and publish the (in my opinion) required interfaces changes for the new implementation. This is the only thing I was currently able to prepare.

thegnuu avatar Mar 15 '22 10:03 thegnuu

@thegnuu yeah, let me know, when the interfaces are available. This will certainly help :)

phal0r avatar Mar 15 '22 11:03 phal0r

Of course, I will do this this week and let you know!

I think we can reduce the current functions and remove the "single scan" ability, since this can be easily implemented by each developer itself.... also things like hideBackground and showBackground do not really make sense, since they are called by the plugin anyways.

thegnuu avatar Mar 15 '22 12:03 thegnuu

@phal0r & @JanMisker I just pushed the branch ml-kit for this changes...

The only thing I updated right now is the typescript definitions part: https://github.com/capacitor-community/barcode-scanner/blob/ml-kit/src/definitions.ts

What do you think about this changes?

thegnuu avatar Mar 17 '22 14:03 thegnuu

@thegnuu yeah, makes sense to me.

So in addition my general considerations:

  • the js side is responsible for deduplication of scanned codes
  • the native sides provides scanned codes as a stream, that will contain the same code multiple times (i.e. with the new coordinates due to phone movement)
  • the native sides controls, how often the scanned codes are send to the js side (i.e. for repositioning or newly detected codes)
  • the js sides needs to decide, when to stop detection

Regarding these considerations, the interface makes a lot of sense, so I guess, you would agree to my thoughts, correct?

phal0r avatar Mar 17 '22 14:03 phal0r

@phal0r exactly my thoughts yes. In my opinion we should keep the native side quite simple and let the individual implementation handle parts like deduplication and when the scanning is paused, stopped aso.

ML Kit will generate a lot of events, so a small throttling on native side might be required, but I think we will see this once we have a simple test environment...

thegnuu avatar Mar 17 '22 14:03 thegnuu

Hi, I found other package on development (apparently abandoned) for capacitor ml kit barcode scanner. https://github.com/franckysolo/capacitor-mlkit-barcodescanner

And other for cordova. https://github.com/Ratsoc/cordova-plugin-firebase-ml-kit-barcode-scanner

I hope it can help you . And although I have no experience with native development I want to offer my help for anything.

danielberebel avatar Mar 23 '22 08:03 danielberebel

@danielberebel sorry for my delayed response and thank you for pointing to that examples, this will surely help to create at least the Android part of the plugin.

If you would like to help you can try to start the rewrite of the Android plugin - I won't find time until next week to get started, if there is already some work done I can pick up the work from there :)

thegnuu avatar Mar 30 '22 08:03 thegnuu

@thegnuu Is there a branch somewhere already?

phal0r avatar Apr 18 '22 08:04 phal0r

@phal0r Kind of: https://github.com/capacitor-community/barcode-scanner/tree/ml-kit

Currently only the new interfaces are committed, I am currently working on it and trying to get at least a single code scanned on Android.... But it's not going forward really fast since I have to look up a lot of stuff ;)

thegnuu avatar Apr 18 '22 12:04 thegnuu

Hi, I'm not experienced at all but I see that this has been done with flutter and was wondering if you could use their Java/Ios code to help form your bindings. *Github repo of the most used barcode scanner implementation *Github repo of the flutter barcode scanner using firebase ML Kit

Taufeeq-Mowzer avatar May 23 '22 18:05 Taufeeq-Mowzer

I was finally able to get ML Kit working (I am currently only working on the iOS version) and so far the results are really promising. I need to do some cleanup but in a week or so I should be ready to provide a iOS test build.

Would anyone out there be willing to support the Android development once the iOS version is done as an example?

thegnuu avatar Jun 17 '22 08:06 thegnuu

It would be awesome to have an MLKit implementation for this plugin as the current version is not working well for us (it isn't able to scan some barcodes...)

smachi avatar Aug 25 '22 16:08 smachi

Would anyone out there be willing to support the Android development once the iOS version is done as an example?

Hi I managed to make a quick little iOS app (permissions are not given yet so had to make a workaround) and the results are very good so I'm looking forward to starting with coding the Android version. If anyone wants to join me in the implementation, I'm open to get some help! as I'm not very experienced in Android development.

ferranJS avatar Aug 26 '22 11:08 ferranJS

@fewNeurons I'm currently working on android implementation the results are quite promising , I need to do some code clean up and then I'll Create a PR for it.

My next milestone is drawing rectangles around the barcodes that was scanned and detected on the camera View

1andonlyziz avatar Oct 15 '22 13:10 1andonlyziz

@1andonlyziz sounds great! Were you able to make any progress or do you need any help somewhere? :)

thegnuu avatar Nov 16 '22 12:11 thegnuu

@thegnuu yeah, I created a PR on ml-kit branch, please review and take a look, I can make a demo if you want ^_*

the only thing that is missing is drawing graphic objects on the barcode view when something is scanned with startScanning function

1andonlyziz avatar Nov 16 '22 12:11 1andonlyziz

@1andonlyziz awesome! Thank you very much for your work! I will have a look at it later today!

I don't think that this feature is necessary to begin with to be honest, surely we can think about it later. :)

thegnuu avatar Nov 16 '22 12:11 thegnuu

Hi everyone!

Thanks to the great work of @1andonlyziz the ml-kit branch should now work on android as well! So far, the results are really promising!

There are still a few TODOs and some cleanups that need to be done, but I should be able to create a dev-release in the upcoming week or so!

thegnuu avatar Nov 23 '22 18:11 thegnuu

Hey @thegnuu , when do you think the ml-kit branch will be merged to master and available in the official npm repo? I would love to start using this feature! 😄

PetervLeeuwen avatar Feb 06 '23 10:02 PetervLeeuwen

@PetervLeeuwen I would love to release it as soon as possible as well, unfortunately, I am way too busy at the moment to provide a proper ETA, I really hope I can finish the open work by the end of this month, but I cannot promise anything. I will do my best though.

thegnuu avatar Feb 06 '23 12:02 thegnuu

Hi @thegnuu I'm more than happy to help out. I am testing the branch and it seems to work quite well already. I do see a couple of small things, should I just make issues for each separate? And submit PRs if I have them? Or should we coordinate more? Not sure how much double work there would be... For now I see:

  • [ ] No support for setting the camera direction on Android
  • [ ] Camera and scanning 'hangs' when switching on the "torch" light on iOS.

JanMisker avatar Mar 16 '23 15:03 JanMisker

v5.0.0-beta.1 is available now :) This is an initial pre-release of the ml-kit implementation and should help anyone who is willing to test the new implementation with an easier setup ;)

thegnuu avatar May 08 '23 14:05 thegnuu