diff --git a/README.md b/README.md index 0502bd9..e000db6 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ No need to waste time on CameraX and ML Kit app anymore. This library will speed ```sh dependencies { ... - implementation 'com.github.furkanturkn:camerax-mlkit-pack:1.0.9' + implementation 'com.github.furkanturkn:camerax-mlkit-pack:1.1.0' } ```
diff --git a/app/build.gradle b/app/build.gradle index 7f8ff81..ec3d001 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -48,5 +48,5 @@ dependencies { implementation "androidx.camera:camera-view:1.2.1" implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.5.1" -// implementation 'com.github.furkanturkn:camerax-mlkit-pack:1.0.8' +// implementation 'com.github.furkanturkn:camerax-mlkit-pack:1.1.0' } \ No newline at end of file diff --git a/app/src/androidTest/java/com/furkan/cameraxmlkitpackexample/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/furkan/cameraxmlkitpackexample/ExampleInstrumentedTest.kt deleted file mode 100644 index ddacec7..0000000 --- a/app/src/androidTest/java/com/furkan/cameraxmlkitpackexample/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.furkan.cameraxmlkitpackexample - -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.ext.junit.runners.AndroidJUnit4 - -import org.junit.Test -import org.junit.runner.RunWith - -import org.junit.Assert.* - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("com.furkan.cameraxmlkitpackexample", appContext.packageName) - } -} \ No newline at end of file diff --git a/app/src/main/java/com/furkan/cameraxmlkitpackexample/MainActivity.kt b/app/src/main/java/com/furkan/cameraxmlkitpackexample/MainActivity.kt index 2755229..398850f 100644 --- a/app/src/main/java/com/furkan/cameraxmlkitpackexample/MainActivity.kt +++ b/app/src/main/java/com/furkan/cameraxmlkitpackexample/MainActivity.kt @@ -1,24 +1,34 @@ package com.furkan.cameraxmlkitpackexample import android.content.Intent -import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button +import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { + + companion object { + const val REQUEST_CODE_PERMISSIONS = 10 + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + val btnQrReaderActivity: Button = findViewById(R.id.btnQrReaderActivity) val btnQrReaderFragment: Button = findViewById(R.id.btnQrReaderFragment) - btnQrReaderActivity.setOnClickListener{ + + btnQrReaderActivity.setOnClickListener { val qrReaderActivityIntent = Intent(applicationContext, QrActivity::class.java) startActivity(qrReaderActivityIntent) } - btnQrReaderFragment.setOnClickListener{ + + btnQrReaderFragment.setOnClickListener { val qrReaderFragment = QrDialogFragment() qrReaderFragment.show(supportFragmentManager, "QR_FRAGMENT") } + + } } \ No newline at end of file diff --git a/app/src/main/java/com/furkan/cameraxmlkitpackexample/QrActivity.kt b/app/src/main/java/com/furkan/cameraxmlkitpackexample/QrActivity.kt index 0534544..822ace5 100644 --- a/app/src/main/java/com/furkan/cameraxmlkitpackexample/QrActivity.kt +++ b/app/src/main/java/com/furkan/cameraxmlkitpackexample/QrActivity.kt @@ -15,25 +15,25 @@ import androidx.lifecycle.lifecycleScope import com.furkan.camerax_mlkit_pack.CameraxManager import com.furkan.camerax_mlkit_pack.core.ReaderType import com.furkan.camerax_mlkit_pack.core.state.FlashStatus +import com.furkan.cameraxmlkitpackexample.MainActivity.Companion.REQUEST_CODE_PERMISSIONS import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch class QrActivity : AppCompatActivity() { - private val REQUIRED_PERMISSIONS = + private val requiredPermissions = mutableListOf( Manifest.permission.CAMERA ).toTypedArray() - private val REQUEST_CODE_PERMISSIONS = 10 - private var cameraxManager: CameraxManager? = null + private lateinit var previewView: PreviewView private lateinit var focusRing: ImageView private lateinit var btnFlash: Button private lateinit var ivCapturePreview: ImageView - lateinit var btnCapturePhoto: Button - lateinit var btnChangeCameraType: Button + private lateinit var btnCapturePhoto: Button + private lateinit var btnChangeCameraType: Button private lateinit var btnStartCamera: Button private lateinit var btnStopCamera: Button private lateinit var btnStartReading: Button @@ -79,16 +79,6 @@ class QrActivity : AppCompatActivity() { } - private fun checkCameraPermission() { - if (allPermissionsGranted()) { - initCameraManager() - } else { - ActivityCompat.requestPermissions( - this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS - ) - } - } - private fun initViews() { previewView = findViewById(R.id.previewView) focusRing = findViewById(R.id.focusRing) @@ -104,6 +94,7 @@ class QrActivity : AppCompatActivity() { } private fun initCameraManager() { + cameraxManager = CameraxManager.getInstance( this, null, @@ -135,6 +126,7 @@ class QrActivity : AppCompatActivity() { FlashStatus.ENABLED -> { btnFlash.setBackgroundResource(R.drawable.baseline_flash_on_24) } + FlashStatus.DISABLED -> { btnFlash.setBackgroundResource(R.drawable.baseline_flash_off_24) } @@ -154,8 +146,18 @@ class QrActivity : AppCompatActivity() { cameraxManager?.destroyReferences() } - //[START] Permission Check - private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all { + //region Permission Check + private fun checkCameraPermission() { + if (allPermissionsGranted()) { + initCameraManager() + } else { + ActivityCompat.requestPermissions( + this, requiredPermissions, REQUEST_CODE_PERMISSIONS + ) + } + } + + private fun allPermissionsGranted() = requiredPermissions.all { ContextCompat.checkSelfPermission( baseContext, it ) == PackageManager.PERMISSION_GRANTED @@ -179,5 +181,5 @@ class QrActivity : AppCompatActivity() { } } } - //[END] Permission Check + //endregion Permission Check } \ No newline at end of file diff --git a/app/src/main/java/com/furkan/cameraxmlkitpackexample/QrDialogFragment.kt b/app/src/main/java/com/furkan/cameraxmlkitpackexample/QrDialogFragment.kt index 28c4691..3a28e46 100644 --- a/app/src/main/java/com/furkan/cameraxmlkitpackexample/QrDialogFragment.kt +++ b/app/src/main/java/com/furkan/cameraxmlkitpackexample/QrDialogFragment.kt @@ -3,21 +3,28 @@ package com.furkan.cameraxmlkitpackexample import android.Manifest import android.content.pm.PackageManager import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast -import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import androidx.fragment.app.DialogFragment -import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope import com.furkan.camerax_mlkit_pack.CameraxManager import com.furkan.camerax_mlkit_pack.core.ReaderType import com.furkan.camerax_mlkit_pack.core.state.FlashStatus -import kotlinx.android.synthetic.main.fragment_qr_dialog.* -import kotlinx.android.synthetic.main.fragment_qr_dialog.view.* +import com.furkan.cameraxmlkitpackexample.MainActivity.Companion.REQUEST_CODE_PERMISSIONS +import kotlinx.android.synthetic.main.fragment_qr_dialog.view.btnCapturePhotoFragment +import kotlinx.android.synthetic.main.fragment_qr_dialog.view.btnChangeCameraTypeFragment +import kotlinx.android.synthetic.main.fragment_qr_dialog.view.btnFlashFragment +import kotlinx.android.synthetic.main.fragment_qr_dialog.view.btnStartCameraFragment +import kotlinx.android.synthetic.main.fragment_qr_dialog.view.btnStartReadingFragment +import kotlinx.android.synthetic.main.fragment_qr_dialog.view.btnStopCameraFragment +import kotlinx.android.synthetic.main.fragment_qr_dialog.view.btnStopReadingFragment +import kotlinx.android.synthetic.main.fragment_qr_dialog.view.focusRingFragment +import kotlinx.android.synthetic.main.fragment_qr_dialog.view.ivCapturePreviewFragment +import kotlinx.android.synthetic.main.fragment_qr_dialog.view.previewViewFragment +import kotlinx.android.synthetic.main.fragment_qr_dialog.view.tvReadResultFragment import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -25,14 +32,11 @@ import kotlinx.coroutines.launch class QrDialogFragment : DialogFragment(R.layout.fragment_qr_dialog) { private lateinit var rootView: View - private var cameraxManager: CameraxManager? = null - - private val REQUIRED_PERMISSIONS = - mutableListOf( - Manifest.permission.CAMERA - ).toTypedArray() + private val requiredPermissions = mutableListOf( + Manifest.permission.CAMERA + ).toTypedArray() - private val REQUEST_CODE_PERMISSIONS = 10 + private var cameraxManager: CameraxManager? = null override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? @@ -74,15 +78,8 @@ class QrDialogFragment : DialogFragment(R.layout.fragment_qr_dialog) { return rootView } - private fun checkCameraPermission() { - if (allPermissionsGranted()) { - initCameraManager(this) - } else { - requestPermissions( - REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS - ) - } - } + + private fun initCameraManager(qrDialogFragment: QrDialogFragment) { cameraxManager = CameraxManager.getInstance( requireContext(), @@ -115,6 +112,7 @@ class QrDialogFragment : DialogFragment(R.layout.fragment_qr_dialog) { FlashStatus.ENABLED -> { rootView.btnFlashFragment.setBackgroundResource(R.drawable.baseline_flash_on_24) } + FlashStatus.DISABLED -> { rootView.btnFlashFragment.setBackgroundResource(R.drawable.baseline_flash_off_24) } @@ -128,34 +126,33 @@ class QrDialogFragment : DialogFragment(R.layout.fragment_qr_dialog) { } } } - /*private fun checkCameraPermission() { + + override fun onDestroy() { + super.onDestroy() + cameraxManager?.destroyReferences() + } + + //region Permission Check + private fun checkCameraPermission() { if (allPermissionsGranted()) { initCameraManager(this) } else { - ActivityCompat.requestPermissions( - this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS + requestPermissions( + requiredPermissions, REQUEST_CODE_PERMISSIONS ) } - }*/ - - override fun onStart() { - super.onStart() } - override fun onDestroy() { - super.onDestroy() - cameraxManager?.destroyReferences() - } - //[START] Permission Check - private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all { + private fun allPermissionsGranted() = requiredPermissions.all { ContextCompat.checkSelfPermission( requireContext(), it ) == PackageManager.PERMISSION_GRANTED } override fun onRequestPermissionsResult( - requestCode: Int, permissions: Array