Commit 1a090336 authored by Dean's avatar Dean

Add ALPR photo shot by file

parent 69eb9e25
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mdd.mddsimulatorcallerapp">
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
......
package com.mdd.mddsimulatorcallerapp
import android.app.ActionBar
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
......@@ -77,4 +79,15 @@ class MainActivity : AppCompatActivity() {
return navController.navigateUp(appBarConfiguration)
|| super.onSupportNavigateUp()
}
// override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// Log.d("onActivityResult", "requestCode: "+requestCode)
// if(requestCode == SecondFragment.REQUEST_IMAGE_CAPTURE){
// Log.d("IMAGE_CAPTURE", "resultCode: "+resultCode)
// data?.let{
// Log.d("IMAGE_CAPTURE", data.extras.toString())
// }
// }
// super.onActivityResult(requestCode, resultCode, data)
// }
}
\ No newline at end of file
package com.mdd.mddsimulatorcallerapp
import android.app.Activity
import android.app.AlertDialog
import android.content.ActivityNotFoundException
import android.content.BroadcastReceiver
import android.content.Intent
import android.content.IntentFilter
import android.content.*
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import androidx.navigation.fragment.findNavController
import com.mdd.mddsimulatorcallerapp.databinding.FragmentSecondBinding
import java.io.File
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.*
/**
* A simple [Fragment] subclass as the second destination in the navigation.
*/
class SecondFragment : Fragment() {
companion object{
val REQUEST_IMAGE_CAPTURE = 1
}
private var _binding: FragmentSecondBinding? = null
......@@ -91,6 +104,9 @@ class SecondFragment : Fragment() {
}
}
}
binding.buttonThird.setOnClickListener {
dispatchTakePictureIntent()
}
}
override fun onDestroyView() {
......@@ -102,4 +118,104 @@ class SecondFragment : Fragment() {
.setMessage(e.message)
.show()
}
lateinit var currentPhotoPath: String
@Throws(IOException::class)
private fun createImageFile(): File {
// Create an image file name
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
// val storageDir: File? = context?.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
val storageDir: File? = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
"ALPR_${timeStamp}_", /* prefix */
".jpg", /* suffix */
storageDir /* directory */
).apply {
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = absolutePath
}
}
val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
Log.d("IMAGE_CAPTURE", "resultCode: "+result.resultCode)
if (result.resultCode == Activity.RESULT_OK) {
Log.d("IMAGE_CAPTURE", result.data.toString())
context?.let { dispatchALPRCamera(it) }
result.data?.let{
Log.d("IMAGE_CAPTURE", it.extras.toString())
}
}
}
private fun dispatchTakePictureIntent() {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
// Ensure that there's a camera activity to handle the intent
context?.let { con ->
takePictureIntent.resolveActivity(con.packageManager)?.also {
// Create the File where the photo should go
val photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
// Error occurred while creating the File
ex.printStackTrace()
null
}
// Continue only if the File was successfully created
photoFile?.also {
val photoURI: Uri = Uri.fromFile(it)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startForResult.launch(takePictureIntent)
// startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
}
}
}
}
}
private fun dispatchALPRCamera(act:Context){
// WAJIB -> compileSdkVersion 29, minSdkVersion 22, targetSdkVersion 29
val br = MyBroadcastReceiver()
MyBroadcastReceiver.typeReceiver = "ALPRCAMERA"
val receiverPath = "com.mdd.mddsimulatorcallerapp.ON_RESULT"
val filter = IntentFilter(receiverPath)
act.registerReceiver(br, filter)
val intent = Intent(Intent.ACTION_SEND)
intent.`package` = "co.id.mdd.mddalprcamera"
intent.addCategory(Intent.CATEGORY_DEFAULT)
intent.let { intent->
intent.putExtra("BroadcastReceiver", receiverPath)
intent.putExtra("ACTIVATION_SERVER_URL", "")
intent.putExtra("ACTIVATION_MASTER_OR_SLAVE_KEY", "")
try {
intent.putExtra("CONFIG_DETECT_MINSCORE", binding.configDetectMinscore.text.toString().toDouble())
}catch (ex:Exception){
ex.printStackTrace()
}
try {
intent.putExtra("CONFIG_RECOGN_MINSCORE", binding.configRecognMinscore.text.toString().toDouble())
}catch (ex:Exception){
ex.printStackTrace()
}
intent.putExtra("CONFIG_KLASS_LPCI_ENABLED", binding.configKLASSLPCIENABLED.isChecked)
intent.putExtra("CONFIG_KLASS_VBSR_ENABLED", binding.configKLASSVBSRENABLED.isChecked)
intent.putExtra("CONFIG_KLASS_VCR_ENABLED", binding.configKLASSVCRENABLED.isChecked)
intent.putExtra("CONFIG_KLASS_VMMR_ENABLED", binding.configKLASSVMMRENABLED.isChecked)
intent.putExtra("IMAGE_PATH", currentPhotoPath)
intent.putExtra("IMAGE_RETRY", 100)
intent.putExtra("GetAllJSON", true)
try {
startActivity(intent)
} catch (e: ActivityNotFoundException) {
showErrorMessage(e)
}
}
}
}
\ No newline at end of file
......@@ -81,4 +81,13 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textview_second" />
<Button
android:id="@+id/button_third"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ALPR CAMERA SINGLE PHOTO"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/button_second" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
Markdown is supported
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