Skip to content

Commit

Permalink
feat: add gallery button on qr to pick images
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhijeet6395 committed Feb 3, 2025
1 parent a971f09 commit 0dc5b05
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
3 changes: 2 additions & 1 deletion quickie/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
android:required="false"/>

<uses-permission android:name="android.permission.CAMERA"/>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<application>

<activity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.github.g00fy2.quickie

import android.Manifest
import android.Manifest.permission.CAMERA
import android.app.Activity
import android.app.Dialog
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Size
import android.view.HapticFeedbackConstants
Expand All @@ -26,7 +29,10 @@ import androidx.core.content.IntentCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import com.google.mlkit.vision.barcode.BarcodeScannerOptions
import com.google.mlkit.vision.barcode.BarcodeScanning
import com.google.mlkit.vision.barcode.common.Barcode
import com.google.mlkit.vision.common.InputImage
import io.github.g00fy2.quickie.config.ParcelableScannerConfig
import io.github.g00fy2.quickie.databinding.QuickieScannerActivityBinding
import io.github.g00fy2.quickie.extensions.toParcelableContentType
Expand All @@ -43,6 +49,9 @@ internal class QRScannerActivity : AppCompatActivity() {
private var showTorchToggle = false
private var showCloseButton = false
private var useFrontCamera = false
private val galleryLauncher=registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
uri?.let { analyzeImageFromGallery(it) }
}
internal var errorDialog: Dialog? = null
set(value) {
field = value
Expand All @@ -68,6 +77,7 @@ internal class QRScannerActivity : AppCompatActivity() {

setupEdgeToEdgeUI()
applyScannerConfig()
setupGalleryButton()

analysisExecutor = Executors.newSingleThreadExecutor()

Expand All @@ -81,6 +91,64 @@ internal class QRScannerActivity : AppCompatActivity() {
}
}

private fun setupGalleryButton() {
binding.selectFromGalleryButton.setOnClickListener {
launchGalleryPicker()
}
}
private val storagePermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
if (granted) {
galleryLauncher.launch(IMAGE_MIME_TYPE)
} else {
onFailure(Exception("Storage permission required to select images"))
}
}
private fun launchGalleryPicker() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_IMAGES) == PackageManager.PERMISSION_GRANTED) {
galleryLauncher.launch(IMAGE_MIME_TYPE)
} else {
storagePermissionLauncher.launch(Manifest.permission.READ_MEDIA_IMAGES)
}
} else {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
galleryLauncher.launch(IMAGE_MIME_TYPE)
} else {
storagePermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
}
}
}

private fun analyzeImageFromGallery(imageUri: Uri) {
try {
val image = InputImage.fromFilePath(this, imageUri)
val options = BarcodeScannerOptions.Builder()
.setBarcodeFormats(barcodeFormats.sum())
.build()

val scanner = BarcodeScanning.getClient(options)

binding.overlayView.isLoading = true

scanner.process(image)
.addOnSuccessListener { barcodes ->
binding.overlayView.isLoading = false
if (barcodes.isNullOrEmpty()) {
onFailure(Exception("No QR code found in the image"))
} else {
onSuccess(barcodes.first())
}
}
.addOnFailureListener { e ->
binding.overlayView.isLoading = false
onFailure(e)
}
} catch (e: Exception) {
binding.overlayView.isLoading = false
onFailure(e)
}
}

override fun onDestroy() {
super.onDestroy()
analysisExecutor.shutdown()
Expand Down Expand Up @@ -216,6 +284,7 @@ internal class QRScannerActivity : AppCompatActivity() {
const val EXTRA_RESULT_TYPE = "quickie-type"
const val EXTRA_RESULT_PARCELABLE = "quickie-parcelable"
const val EXTRA_RESULT_EXCEPTION = "quickie-exception"
const val IMAGE_MIME_TYPE="image/*"
const val RESULT_MISSING_PERMISSION = RESULT_FIRST_USER + 1
const val RESULT_ERROR = RESULT_FIRST_USER + 2
}
Expand Down
14 changes: 14 additions & 0 deletions quickie/src/main/res/drawable/quickie_rounded_rectangle_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item>
<shape android:shape="rectangle">
<!-- Set the background color -->
<solid android:color="@color/quickie_white" />

<corners android:radius="24dp" />
</shape>
</item>

</layer-list>
11 changes: 11 additions & 0 deletions quickie/src/main/res/layout/quickie_scanner_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@
android:visibility="invisible"
tools:visibility="visible"
/>
<Button
android:id="@+id/selectFromGalleryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select from Gallery"
android:background="@drawable/quickie_rounded_rectangle_button"
android:textColor="@android:color/black"
android:padding="16dp"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="32dp" />

</FrameLayout>

0 comments on commit 0dc5b05

Please sign in to comment.