Skip to content
Snippets Groups Projects
Commit 725aca94 authored by althaafka's avatar althaafka
Browse files

feat: add camera preview

parent f35ed71e
3 merge requests!5pull main to feat/login,!4Feat/scanner,!2Feat/scanner
......@@ -36,6 +36,7 @@ android {
}
buildFeatures {
compose true
viewBinding true
}
composeOptions {
kotlinCompilerExtensionVersion '1.5.1'
......@@ -83,7 +84,6 @@ dependencies {
implementation "androidx.camera:camera-core:1.4.0-alpha04"
implementation "androidx.camera:camera-camera2:1.4.0-alpha04"
implementation "androidx.camera:camera-lifecycle:1.4.0-alpha04"
implementation "androidx.camera:camera-video:1.4.0-alpha04"
implementation "androidx.camera:camera-view:1.4.0-alpha04"
......
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-feature android:name="android.hardware.camera.any"/>
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
......@@ -12,6 +15,9 @@
android:supportsRtl="true"
android:theme="@style/Theme.BondoMan"
tools:targetApi="31">
<activity
android:name=".UploadBillActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true"
......
package com.example.bondoman
import android.Manifest
object Constants {
const val TAG = "cameraX"
const val REQUEST_CODE_PERMISSIONS = 123
val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA)
}
\ No newline at end of file
package com.example.bondoman
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.camera.core.CameraSelector
import androidx.camera.core.ImageCapture
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.content.ContextCompat
import com.example.bondoman.databinding.FragmentScannerBinding
import com.example.bondoman.Constants
import java.io.File
import java.lang.Exception
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [ScannerFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class ScannerFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
private var _binding: FragmentScannerBinding? = null
private val binding get() = _binding!!
private var imageCapture: ImageCapture? = null
private fun takePhoto(){
// val imageCapture = imageCapture?: return
// val photoFile = File(
// outputDirectory,
// "file.jpg"
// )
// startActivity(
// Intent(
// this.requireContext(),
//
// )
// )
}
private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())
cameraProviderFuture.addListener({
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
val preview = Preview.Builder().build().also { mPreview ->
mPreview.setSurfaceProvider(binding.cameraView.surfaceProvider)
}
imageCapture = ImageCapture.Builder().build()
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try {
cameraProvider.unbindAll()
cameraProvider.bindToLifecycle(
viewLifecycleOwner, cameraSelector, preview, imageCapture
)
} catch (e: Exception) {
Log.d(Constants.TAG, "Start camera error: ", e)
}
}, ContextCompat.getMainExecutor(requireContext()))
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_scanner, container, false)
_binding = FragmentScannerBinding.inflate(inflater, container, false)
if (!allPermissionGranted()) {
requestPermissions(
Constants.REQUIRED_PERMISSIONS,
Constants.REQUEST_CODE_PERMISSIONS
)
} else {
startCamera()
}
// binding.captureBtn.setOnClickListener{
// takePhoto()
// }
return binding.root
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ScannerFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
ScannerFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
private fun allPermissionGranted() = Constants.REQUIRED_PERMISSIONS.all {
ContextCompat.checkSelfPermission(
requireContext(), it
) == PackageManager.PERMISSION_GRANTED
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
\ No newline at end of file
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment