Skip to content

Instantly share code, notes, and snippets.

@sergiotapia
Created September 19, 2017 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergiotapia/f8611e439b638d9c4424525aa1202c65 to your computer and use it in GitHub Desktop.
Save sergiotapia/f8611e439b638d9c4424525aa1202c65 to your computer and use it in GitHub Desktop.
<body><hr/><h1>Fluid Camera2 Tracker Toggling</h1><h2>ADA | Adam Deconstructs Android</h2><img src="https://cdn-images-1.medium.com/max/2000/1*fjKwC62jP4YW4OCQbIGCXw.png"/>Photo by <a href="https://unsplash.com/photos/j3uncnQ-eAw?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Jenu Prasad</a> on <a href="https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a><blockquote>This pattern is useful for continuously running Android components while shifting functionality and not interrupting the UI.</blockquote><img src="https://cdn-images-1.medium.com/max/1600/1*_xFltb6WYRd4tTUEKSjmtw.gif"/><a href="https://github.com/AdamSHurwitz/ScannerApp"><strong>ScannerApp</strong></a><p>With Android Lollipop’s <a href="https://developer.android.com/reference/android/hardware/camera2/package-summary.html?utm_campaign=adp_series_how_to_camera2_031016&utm_source=medium&utm_medium=blog">Camera2</a> API developers were given the opportunity to customize the camera experience in their apps. Some of the features outlined by Developer Advocate <a href="https://medium.com/@davideast">David East</a> at <a href="https://medium.com/@googledevs">Google Developers</a> in his post <a href="https://medium.com/google-developers/detecting-camera-features-with-camera2-61675bb7d1bf"><em>Detecting camera features with Camera2</em></a><em>, </em>include<em></em>auto-focus, exposure, sensor controls, effects, filters, interval control, and so on... I tinkered with the camera’s barcode detection and scanning capabilities in order to create the <a href="https://github.com/AdamSHurwitz/ScannerApp"><strong><em>ScannerApp</em></strong></a><em></em>sample<strong></strong>showcasing smooth transitioning between barcode scanning modes while keeping the camera running. This results in seamless toggling between barcode types, which is a useful feature for switching between any type of camera detection.</p><h3>Implementation</h3><h4>Architecture</h4><img src="https://cdn-images-1.medium.com/max/1600/1*mE-2jelX-ucXYfwjC5NogQ.png"/>highly technical drawing by yours truly ;-)<p>This pattern is useful for continuously running Android components (<em>like the camera</em>) while smoothly shifting functionality (<em>barcode types</em>) and not interrupting the UI.</p><p><a href="https://github.com/AdamSHurwitz/ScannerApp"><em>ScannerApp</em></a> is designed to do the heavy lifting in one central location, the <em>BarcodeActivity </em>contains the camera logic including returned barcode results. The results (<em>represented in green above</em>) flow down to the <em>BarcodeFragment</em> → ViewPager. The ViewPager knows which Fragment is active and passes the results accordingly. Finally, the results are displayed in the <em>BarcodeFragmentOverlay</em>. This architecture keeps the camera logic in the <em>BarcodeActivity </em>de-coupled from the UI logic in the <em>BarcodeFragmentOverlay.</em></p><h4>Building Upon Google Vision and Camera2Vision Samples</h4><p><a href="https://github.com/googlesamples/android-vision"><strong>Google Vision</strong></a></p><p>When creating the <em>BarcodeTrackerFactory</em> in the <em>BarcodeActivity </em>I added <em>Context </em>to pass in as a parameter. The factory class creates the <em>BarcodeGraphicTracker </em>that consumes the barcode results in the <em>overrided </em>method <strong>onNewItem()</strong>. In order to share this data with my Activity I created an interface <em>UpdateBarcodeListener </em>that uses the context to ensure the Interface is implemented within the Activity. In <strong>onNewItem() </strong>I use the interface method <strong>getBarcode() </strong>to pass barcodes up to the Activity level.</p><p>Note: the <strong>getBarcode() </strong>method in the interface needs to run on the <strong>UIThread.</strong></p><p><em>Activity</em></p><pre><strong>runOnUiThread</strong><strong>{<br/></strong>(<em>supportFragmentManager</em>.findFragmentById(R.id.<em>content</em>) <strong>as <br/></strong>BarcodeFragment).addBarcode(barcode)<br/><strong>}</strong></pre><p><em>BarcodeTrackerFactory</em></p><p><em>BarcodeGraphicTracker</em></p><p><a href="https://github.com/EzequielAdrianM/Camera2Vision"><strong>Camera2Vision</strong></a></p><p>I used <a href="https://github.com/EzequielAdrianM">Ezequiel Adrian’s</a> sample for his <a href="https://github.com/EzequielAdrianM/Camera2Vision/blob/master/Camera2/app/src/main/java/com/example/ezequiel/camera2/others/Camera2Source.java"><em>Camera2Source</em></a><em></em>which manages the camera lifecycle. When the camera source is created it passes in a <em>BarcodeDetector </em>that communicates to the camera source the scanner type(s) to identify. In the current state, the only way to update the camera’s detector type is to pause/stop the camera and restart/recreate it.</p><p>By turning the <em>Detector </em>into a private instance variable a<strong> refreshDetector() </strong>method can be used to update the camera source’s detector while keeping the camera lifecycle active.</p><pre>public void<strong></strong><em>refreshDetector</em>(<strong>Detector</strong>���<emmarkup--em markup--pre-emdetector){<br="markup--pre-emdetector){<br"></emmarkup--em><em>mDetector </em>= <em>detector</em>;<br/>}</pre><p>On the Activity level a new Detector can be created after the existing one is released (<em>clearing the UI of the old barcode graphics</em>), and then the <strong>refreshDetector() </strong>method can be called.</p><p><em>Activity</em></p><pre>override<strong></strong>fun<strong></strong><em>updateScannerType</em>(<em>scannerType</em>: Int) {<br/><strong><em>barcodeDetector</em>.release()</strong><br/><strong><em>createBarCodeDetector</em></strong>(<em>scannerType</em>)<br/> camera2Source.<strong><em>refreshDetector</em></strong>(<strong><em>barcodeDetector</em></strong>)<br/>}</pre><pre>...</pre><pre>private fun<strong><em>createBarCodeDetector</em></strong>(scannerType: Int) {<br/><br/><strong><em>barcodeDetector</em></strong>= BarcodeDetector.Builder(this)<br/> .setBarcodeFormats(scannerType)<br/> .build()<br/><br/> if (<strong><em>barcodeDetector</em></strong>.isOperational()) {<br/><strong><em>barcodeDetector<br/></em></strong>.setProcessor(<br/> MultiProcessor.Builder(BarcodeTrackerFactory(<br/> graphicOverlay, this))<br/> .build())<br/> } else {<br/> Toast.makeText(this, "BARCODE DETECTION NOT AVAILABLE", <br/> Toast.<em>LENGTH_SHORT</em>).show()<br/> }<br/><br/>}</pre><h3>Resources</h3><p><strong>Documentation</strong></p><ul><li><a href="https://medium.com/google-developers/detecting-camera-features-with-camera2-61675bb7d1bf"><em>Detecting camera features with Camera2</em></a><em></em>by Developer Advocate <a href="https://medium.com/@davideast">David East</a></li><li><a href="https://www.youtube.com/watch?time_continue=12&v=Bi4QjMfSOE0">YouTube — How to Camera2 (Android Development Patterns S2 Ep 8)</a></li><li><a href="https://developer.android.com/reference/android/hardware/camera2/package-summary.html?utm_campaign=adp_series_camera2_030916&utm_source=gdev&utm_medium=yt-desc">Developers Guide — android.hardware.camera2</a></li></ul><p><strong>Samples</strong></p><ul><li><a href="https://github.com/AdamSHurwitz/ScannerApp"><em>ScannerApp</em></a> by <a href="http://medium.com/@AdamHurwitz">me</a></li><li><a href="https://github.com/EzequielAdrianM/Camera2Vision"><em>Camera2Vision</em></a><em></em>by <a href="https://github.com/EzequielAdrianM">Ezequiel Adrian</a> — implements face tracking with Camera2</li><li><a href="https://github.com/googlesamples/android-vision">Android-Vision</a> by Google — implements FaceTracker, barcode-reader, googly-eyes, multi-tracker, ocr-codelab, ocr-reader, photo-demo</li><li><a href="https://github.com/googlesamples/android-Camera2Basic">Camera2Basic</a> by Google</li></ul><hr/><p><em>I’m Adam Hurwitz — hit the clapping hands icon and check out the rest of my </em><a href="https://medium.com/@AdamHurwitz"><em>writing</em></a><em> if you enjoyed the above | Thanks!</em></p><img src="https://cdn-images-1.medium.com/max/1600/1*aqtxhMAJEHcL0PhVB1I8Fg.png"/></body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment