QR/Aztec scanning using google play services

Still need to generate codes for clients to scan
This commit is contained in:
Luke Hubmayer-Werner 2024-08-27 20:30:41 +09:30
parent 5c980880c6
commit b42a25e42e
8 changed files with 77 additions and 1 deletions

View File

@ -49,6 +49,7 @@ dependencies {
implementation(libs.androidx.lifecycle.viewmodel.ktx) implementation(libs.androidx.lifecycle.viewmodel.ktx)
implementation(libs.androidx.navigation.fragment.ktx) implementation(libs.androidx.navigation.fragment.ktx)
implementation(libs.androidx.navigation.ui.ktx) implementation(libs.androidx.navigation.ui.ktx)
implementation(libs.play.services.code.scanner)
testImplementation(libs.junit) testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core) androidTestImplementation(libs.androidx.espresso.core)

View File

@ -0,0 +1,42 @@
package com.lhw.pdf.ui
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.mlkit.vision.barcode.common.Barcode
import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions
import com.google.mlkit.vision.codescanner.GmsBarcodeScanning
import com.lhw.pdf.R
import com.lhw.pdf.databinding.FragmentNetworkBinding
import com.lhw.pdf.databinding.FragmentSlideshowBinding
class NetworkFragment : Fragment() {
private var _binding: FragmentNetworkBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
_binding = FragmentNetworkBinding.inflate(inflater, container, false)
val root: View = binding.root
binding.btnScanQrCode.setOnClickListener { getQrCode() }
return root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
private fun getQrCode() {
val options = GmsBarcodeScannerOptions.Builder().setBarcodeFormats(Barcode.FORMAT_QR_CODE, Barcode.FORMAT_AZTEC).enableAutoZoom().build()
val scanner = GmsBarcodeScanning.getClient(requireContext(), options)
scanner.startScan()
.addOnSuccessListener { println("QR scan success: ${it.rawValue}") }
.addOnCanceledListener { println("QR scan canceled") }
.addOnFailureListener { println("QR scan failure: $it") }
}
}

View File

@ -1,7 +1,6 @@
package com.lhw.pdf.ui.gallery package com.lhw.pdf.ui.gallery
import android.graphics.Bitmap import android.graphics.Bitmap
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.NetworkFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Network fragment"
android:textAlignment="center" />
<Button
android:id="@+id/btn_scan_qr_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan QR Code" />
</FrameLayout>

View File

@ -16,5 +16,9 @@
android:id="@+id/nav_slideshow" android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_menu_slideshow" android:icon="@drawable/ic_menu_slideshow"
android:title="@string/menu_slideshow" /> android:title="@string/menu_slideshow" />
<item
android:id="@+id/nav_network"
android:icon="@android:drawable/ic_menu_compass"
android:title="@string/menu_network_fragment" />
</group> </group>
</menu> </menu>

View File

@ -22,4 +22,10 @@
android:name="com.lhw.pdf.ui.slideshow.SlideshowFragment" android:name="com.lhw.pdf.ui.slideshow.SlideshowFragment"
android:label="@string/menu_slideshow" android:label="@string/menu_slideshow"
tools:layout="@layout/fragment_slideshow" /> tools:layout="@layout/fragment_slideshow" />
<fragment
android:id="@+id/nav_network"
android:name="com.lhw.pdf.ui.NetworkFragment"
android:label="@string/menu_network_fragment"
tools:layout="@layout/fragment_network" />
</navigation> </navigation>

View File

@ -10,4 +10,5 @@
<string name="menu_home">Home</string> <string name="menu_home">Home</string>
<string name="menu_gallery">Gallery</string> <string name="menu_gallery">Gallery</string>
<string name="menu_slideshow">Slideshow</string> <string name="menu_slideshow">Slideshow</string>
<string name="menu_network_fragment">Network</string>
</resources> </resources>

View File

@ -13,6 +13,7 @@ lifecycleViewmodelKtx = "2.8.4"
navigationFragmentKtx = "2.7.7" navigationFragmentKtx = "2.7.7"
navigationUiKtx = "2.7.7" navigationUiKtx = "2.7.7"
pdfboxAndroid = "2.0.27.0" pdfboxAndroid = "2.0.27.0"
playServicesCodeScanner = "16.1.0"
[libraries] [libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@ -27,6 +28,7 @@ androidx-lifecycle-viewmodel-ktx = { group = "androidx.lifecycle", name = "lifec
androidx-navigation-fragment-ktx = { group = "androidx.navigation", name = "navigation-fragment-ktx", version.ref = "navigationFragmentKtx" } androidx-navigation-fragment-ktx = { group = "androidx.navigation", name = "navigation-fragment-ktx", version.ref = "navigationFragmentKtx" }
androidx-navigation-ui-ktx = { group = "androidx.navigation", name = "navigation-ui-ktx", version.ref = "navigationUiKtx" } androidx-navigation-ui-ktx = { group = "androidx.navigation", name = "navigation-ui-ktx", version.ref = "navigationUiKtx" }
pdfbox-android = { module = "com.tom-roush:pdfbox-android", version.ref = "pdfboxAndroid" } pdfbox-android = { module = "com.tom-roush:pdfbox-android", version.ref = "pdfboxAndroid" }
play-services-code-scanner = { group = "com.google.android.gms", name = "play-services-code-scanner", version.ref = "playServicesCodeScanner" }
[plugins] [plugins]
android-application = { id = "com.android.application", version.ref = "agp" } android-application = { id = "com.android.application", version.ref = "agp" }