forked from shihaam/thijooree
add zoom QR and flashlight button
This commit is contained in:
@@ -13,6 +13,12 @@ import android.provider.Settings
|
|||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import android.content.res.ColorStateList
|
||||||
|
import android.graphics.Color
|
||||||
|
import android.graphics.drawable.Animatable
|
||||||
|
import android.view.ScaleGestureDetector
|
||||||
|
import androidx.appcompat.content.res.AppCompatResources
|
||||||
|
import androidx.camera.core.Camera
|
||||||
import androidx.camera.core.CameraSelector
|
import androidx.camera.core.CameraSelector
|
||||||
import androidx.core.view.ViewCompat
|
import androidx.core.view.ViewCompat
|
||||||
import androidx.core.view.WindowCompat
|
import androidx.core.view.WindowCompat
|
||||||
@@ -52,6 +58,8 @@ class QrScannerActivity : AppCompatActivity() {
|
|||||||
textMode = ZxingCpp.TextMode.PLAIN
|
textMode = ZxingCpp.TextMode.PLAIN
|
||||||
)
|
)
|
||||||
|
|
||||||
|
private var camera: Camera? = null
|
||||||
|
private var torchEnabled = false
|
||||||
private var cameraStarted = false
|
private var cameraStarted = false
|
||||||
|
|
||||||
private val permissionLauncher = registerForActivityResult(
|
private val permissionLauncher = registerForActivityResult(
|
||||||
@@ -104,8 +112,36 @@ class QrScannerActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
insets
|
insets
|
||||||
}
|
}
|
||||||
binding.btnCancel.setOnClickListener { finish() }
|
|
||||||
binding.btnPickImage.setOnClickListener { pickImageLauncher.launch("image/*") }
|
binding.btnPickImage.setOnClickListener { pickImageLauncher.launch("image/*") }
|
||||||
|
binding.zoomSlider.addOnChangeListener { _, value, fromUser ->
|
||||||
|
if (fromUser) camera?.cameraControl?.setLinearZoom(value)
|
||||||
|
}
|
||||||
|
val scaleDetector = ScaleGestureDetector(this,
|
||||||
|
object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
|
||||||
|
override fun onScale(detector: ScaleGestureDetector): Boolean {
|
||||||
|
val state = camera?.cameraInfo?.zoomState?.value ?: return true
|
||||||
|
camera?.cameraControl?.setZoomRatio(
|
||||||
|
(state.zoomRatio * detector.scaleFactor)
|
||||||
|
.coerceIn(state.minZoomRatio, state.maxZoomRatio)
|
||||||
|
)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
binding.previewView.setOnTouchListener { _, event ->
|
||||||
|
scaleDetector.onTouchEvent(event)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
binding.btnFlashlight.setOnClickListener {
|
||||||
|
torchEnabled = !torchEnabled
|
||||||
|
camera?.cameraControl?.enableTorch(torchEnabled)
|
||||||
|
val drawableRes = if (torchEnabled) R.drawable.ic_flashlight_to_on else R.drawable.ic_flashlight_to_off
|
||||||
|
val drawable = AppCompatResources.getDrawable(this, drawableRes)
|
||||||
|
binding.btnFlashlight.icon = drawable
|
||||||
|
(drawable as? Animatable)?.start()
|
||||||
|
binding.btnFlashlight.iconTint = ColorStateList.valueOf(
|
||||||
|
if (torchEnabled) Color.parseColor("#FFEB3B") else Color.WHITE
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
|
||||||
== PackageManager.PERMISSION_GRANTED
|
== PackageManager.PERMISSION_GRANTED
|
||||||
@@ -179,9 +215,12 @@ class QrScannerActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
provider.unbindAll()
|
provider.unbindAll()
|
||||||
provider.bindToLifecycle(
|
camera = provider.bindToLifecycle(
|
||||||
this, CameraSelector.DEFAULT_BACK_CAMERA, preview, analysis
|
this, CameraSelector.DEFAULT_BACK_CAMERA, preview, analysis
|
||||||
)
|
)
|
||||||
|
camera?.cameraInfo?.zoomState?.observe(this@QrScannerActivity) { state ->
|
||||||
|
binding.zoomSlider.value = state.linearZoom
|
||||||
|
}
|
||||||
} catch (_: Exception) {
|
} catch (_: Exception) {
|
||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:duration="250"
|
||||||
|
android:propertyName="trimPathEnd"
|
||||||
|
android:valueFrom="0"
|
||||||
|
android:valueTo="1"
|
||||||
|
android:valueType="floatType"
|
||||||
|
android:interpolator="@android:interpolator/accelerate_decelerate" />
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:duration="250"
|
||||||
|
android:propertyName="trimPathEnd"
|
||||||
|
android:valueFrom="1"
|
||||||
|
android:valueTo="0"
|
||||||
|
android:valueType="floatType"
|
||||||
|
android:interpolator="@android:interpolator/accelerate_decelerate" />
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportWidth="24"
|
||||||
|
android:viewportHeight="24">
|
||||||
|
<path
|
||||||
|
android:fillColor="@android:color/black"
|
||||||
|
android:pathData="M7,2v11h3v9l7,-12h-4l4,-8z" />
|
||||||
|
</vector>
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- base for "turning off" animation: line starts invisible -->
|
||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportWidth="24"
|
||||||
|
android:viewportHeight="24">
|
||||||
|
<path
|
||||||
|
android:fillColor="@android:color/black"
|
||||||
|
android:pathData="M7,2v11h3v9l7,-12h-4l4,-8z" />
|
||||||
|
<path
|
||||||
|
android:name="line"
|
||||||
|
android:strokeColor="@android:color/black"
|
||||||
|
android:strokeWidth="2"
|
||||||
|
android:strokeLineCap="round"
|
||||||
|
android:pathData="M2,4 L22,20"
|
||||||
|
android:trimPathEnd="0" />
|
||||||
|
</vector>
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- base for "turning on" animation: line starts fully visible -->
|
||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportWidth="24"
|
||||||
|
android:viewportHeight="24">
|
||||||
|
<path
|
||||||
|
android:fillColor="@android:color/black"
|
||||||
|
android:pathData="M7,2v11h3v9l7,-12h-4l4,-8z" />
|
||||||
|
<path
|
||||||
|
android:name="line"
|
||||||
|
android:strokeColor="@android:color/black"
|
||||||
|
android:strokeWidth="2"
|
||||||
|
android:strokeLineCap="round"
|
||||||
|
android:pathData="M2,4 L22,20"
|
||||||
|
android:trimPathEnd="1" />
|
||||||
|
</vector>
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportWidth="24"
|
||||||
|
android:viewportHeight="24">
|
||||||
|
<!-- bolt with line through it (flash_off) -->
|
||||||
|
<path
|
||||||
|
android:fillColor="@android:color/black"
|
||||||
|
android:pathData="M3.27,3L2,4.27l5,5V13h3v9l3.58,-6.14L17.73,20 19,18.73 3.27,3zM17,10h-4l4,-8H7v2.18l8.46,8.46L17,10z" />
|
||||||
|
</vector>
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:drawable="@drawable/ic_flashlight_anim_off">
|
||||||
|
<target
|
||||||
|
android:name="line"
|
||||||
|
android:animation="@animator/flashlight_line_appear" />
|
||||||
|
</animated-vector>
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:drawable="@drawable/ic_flashlight_anim_on">
|
||||||
|
<target
|
||||||
|
android:name="line"
|
||||||
|
android:animation="@animator/flashlight_line_disappear" />
|
||||||
|
</animated-vector>
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportWidth="24"
|
||||||
|
android:viewportHeight="24">
|
||||||
|
<path
|
||||||
|
android:fillColor="@android:color/black"
|
||||||
|
android:pathData="M21,19V5c0,-1.1 -0.9,-2 -2,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2zM8.5,13.5l2.5,3.01 3.5,-4.51 4.5,6H5l3.5,-4.5z" />
|
||||||
|
</vector>
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:background="@android:color/black">
|
android:background="@android:color/black">
|
||||||
@@ -11,26 +12,49 @@
|
|||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:id="@+id/btnContainer"
|
android:id="@+id/btnContainer"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_gravity="bottom|center_horizontal"
|
android:layout_gravity="bottom|center_horizontal"
|
||||||
android:layout_marginBottom="48dp"
|
android:layout_marginBottom="48dp"
|
||||||
android:orientation="horizontal">
|
android:orientation="vertical"
|
||||||
|
android:paddingHorizontal="16dp">
|
||||||
|
|
||||||
<com.google.android.material.button.MaterialButton
|
<com.google.android.material.slider.Slider
|
||||||
android:id="@+id/btnPickImage"
|
android:id="@+id/zoomSlider"
|
||||||
style="@style/Widget.Material3.Button.TonalButton"
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginBottom="8dp"
|
||||||
|
android:valueFrom="0"
|
||||||
|
android:valueTo="1"
|
||||||
|
android:value="0"
|
||||||
|
app:labelBehavior="gone"
|
||||||
|
app:thumbColor="@android:color/white"
|
||||||
|
app:trackColorActive="@android:color/white"
|
||||||
|
app:trackColorInactive="#80FFFFFF" />
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_gravity="center_horizontal"
|
||||||
android:text="@string/qr_pick_image" />
|
android:orientation="horizontal">
|
||||||
|
|
||||||
<com.google.android.material.button.MaterialButton
|
<com.google.android.material.button.MaterialButton
|
||||||
android:id="@+id/btnCancel"
|
android:id="@+id/btnPickImage"
|
||||||
style="@style/Widget.Material3.Button.TonalButton"
|
style="@style/Widget.Material3.Button.TonalButton"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="@string/back" />
|
android:layout_marginEnd="8dp"
|
||||||
|
android:text="@string/qr_pick_image"
|
||||||
|
app:icon="@drawable/ic_image" />
|
||||||
|
|
||||||
|
<com.google.android.material.button.MaterialButton
|
||||||
|
android:id="@+id/btnFlashlight"
|
||||||
|
style="@style/Widget.Material3.Button.IconButton.Filled.Tonal"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
app:icon="@drawable/ic_flashlight_anim_on" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user