[AA-28] Upload PAN Screen to display camera preview within a part of the screen (#25)
* AA-28 | Sandhya | Add CameraKit for image capture * AA-28 | Sandhya | Make provision to display preview of the captured image * AA-28 | Sandhya | Fix preview image UI style * AA-28 | Sandhya | Fix preview image UI style * AA-28 | Sandhya | Fix nullables and add error handling * AA-28 | Sandhya | Move bitmap hand;ing to utils * AA-28 | Sandhya | Extract prep UI to another method
This commit is contained in:
committed by
Satish Prasad
parent
23e2dbac04
commit
4d365faed6
@@ -52,4 +52,8 @@ dependencies {
|
||||
implementation 'com.android.support:design:29.0.2'
|
||||
implementation 'com.jakewharton.timber:timber:4.7.1'
|
||||
implementation 'com.navi.medici.utils:amortization:1.1-SNAPSHOT'
|
||||
implementation 'com.camerakit:camerakit:1.0.0-beta3.11'
|
||||
implementation 'com.camerakit:jpegkit:0.1.0'
|
||||
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.0'
|
||||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'
|
||||
}
|
||||
|
||||
@@ -1,35 +1,31 @@
|
||||
package com.navi.medici.androidCustomerApp.ui.activities
|
||||
|
||||
import android.Manifest
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Bitmap
|
||||
import android.os.Bundle
|
||||
import android.provider.MediaStore
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import com.navi.medici.androidCustomerApp.R
|
||||
import com.navi.medici.androidCustomerApp.databinding.ActivityUploadPanBinding
|
||||
import com.navi.medici.androidCustomerApp.preferences.PreferenceManager
|
||||
import com.navi.medici.androidCustomerApp.viewModels.PreliminaryOfferViewModel
|
||||
import com.navi.medici.androidCustomerApp.viewModels.UploadPanViewModel
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
|
||||
import com.camerakit.CameraKitView
|
||||
import android.view.View
|
||||
import com.navi.medici.androidCustomerApp.R
|
||||
import com.navi.medici.androidCustomerApp.utils.createBitmap
|
||||
import java.lang.Exception
|
||||
|
||||
class UploadPanActivity : AppCompatActivity() {
|
||||
private val requestImageCapture: Int = 12
|
||||
private val permissionsCode = 121
|
||||
private lateinit var binding: ActivityUploadPanBinding
|
||||
private lateinit var uploadPanViewModel: UploadPanViewModel
|
||||
private lateinit var bitmap: Bitmap
|
||||
private lateinit var panImageFile: File
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -41,16 +37,35 @@ class UploadPanActivity : AppCompatActivity() {
|
||||
)
|
||||
if (!checkPermissions()) {
|
||||
requestPermissions()
|
||||
} else {
|
||||
takePicture()
|
||||
}
|
||||
binding.skipPanUploadButton.setOnClickListener {
|
||||
val intent = Intent(this, InputPanActivity::class.java)
|
||||
startActivity(intent)
|
||||
}
|
||||
binding.captureButton.setOnClickListener {
|
||||
binding.imageView.captureImage(object : CameraKitView.ImageCallback {
|
||||
override fun onImage(cameraKitView: CameraKitView?, capturedImage: ByteArray?) {
|
||||
panImageFile = File(applicationContext.cacheDir, "pan_image")
|
||||
panImageFile.createNewFile()
|
||||
try {
|
||||
val fos = FileOutputStream(panImageFile)
|
||||
fos.write(capturedImage)
|
||||
fos.flush()
|
||||
fos.close()
|
||||
displayPreview(capturedImage)
|
||||
} catch (e: Exception) {
|
||||
Toast.makeText(
|
||||
this@UploadPanActivity, e.message,
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
binding.uploadButton.setOnClickListener {
|
||||
if (binding.checkbox.isChecked) {
|
||||
createImageFileAndUpload()
|
||||
if (binding.consentCheckbox.isChecked) {
|
||||
uploadPanViewModel.uploadPan(panImageFile, this)
|
||||
uploadPanViewModel.panUploaded.observe(this, panUploadedObserver())
|
||||
} else {
|
||||
Toast.makeText(
|
||||
@@ -62,6 +77,49 @@ class UploadPanActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun displayPreview(capturedImage: ByteArray?) {
|
||||
prepareUI()
|
||||
capturedImage?.let {
|
||||
binding.previewImage.setImageBitmap(
|
||||
createBitmap(
|
||||
capturedImage, binding.previewImage.getWidth(),
|
||||
binding.previewImage.getHeight()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private fun prepareUI() {
|
||||
binding.captureButton.isEnabled = false
|
||||
binding.uploadButton.isEnabled = true
|
||||
binding.imageView.visibility = View.GONE
|
||||
binding.previewImage.visibility = View.VISIBLE
|
||||
binding.consentCheckbox.visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
binding.imageView.onStart()
|
||||
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
binding.imageView.onResume()
|
||||
|
||||
}
|
||||
|
||||
public override fun onPause() {
|
||||
binding.imageView.onPause()
|
||||
super.onPause()
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
binding.imageView.onStop()
|
||||
super.onStop()
|
||||
}
|
||||
|
||||
|
||||
private fun panUploadedObserver(): Observer<Boolean> {
|
||||
return Observer {
|
||||
if (it) {
|
||||
@@ -99,7 +157,11 @@ class UploadPanActivity : AppCompatActivity() {
|
||||
permissionsCode -> {
|
||||
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)
|
||||
) {
|
||||
takePicture()
|
||||
binding.imageView.onRequestPermissionsResult(
|
||||
requestCode,
|
||||
permissions,
|
||||
grantResults
|
||||
);
|
||||
} else {
|
||||
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
@@ -107,33 +169,4 @@ class UploadPanActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun takePicture() {
|
||||
val intent: Intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
|
||||
startActivityForResult(intent, requestImageCapture)
|
||||
}
|
||||
|
||||
@SuppressLint("MissingSuperCall")
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
if (requestCode == requestImageCapture && resultCode == Activity.RESULT_OK) {
|
||||
bitmap = data?.getExtras()?.get("data") as Bitmap
|
||||
binding.imageView.setImageBitmap(bitmap)
|
||||
binding.uploadButton.isEnabled = true
|
||||
}
|
||||
}
|
||||
|
||||
private fun createImageFileAndUpload() {
|
||||
val panImageFile = File(applicationContext.cacheDir, "pan_image")
|
||||
panImageFile.createNewFile()
|
||||
|
||||
val outputStream = ByteArrayOutputStream()
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 0, outputStream)
|
||||
|
||||
val fos = FileOutputStream(panImageFile)
|
||||
fos.write(outputStream.toByteArray())
|
||||
fos.flush()
|
||||
fos.close()
|
||||
|
||||
uploadPanViewModel.uploadPan(panImageFile, this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.navi.medici.androidCustomerApp.utils
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
|
||||
|
||||
fun createBitmap(image: ByteArray, width: Int, height: Int): Bitmap {
|
||||
val bmp = BitmapFactory.decodeByteArray(image, 0, image.size)
|
||||
return Bitmap.createScaledBitmap(bmp, width, height, false)
|
||||
}
|
||||
@@ -12,13 +12,13 @@
|
||||
android:id="@+id/logo_image"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="32dp"
|
||||
android:layout_marginTop="32dp"
|
||||
android:layout_marginEnd="32dp"
|
||||
android:adjustViewBounds="true"
|
||||
android:contentDescription="@string/app_logo"
|
||||
android:src="@drawable/navi_logo_unit_cmyk"
|
||||
app:layout_constrainedWidth="true"
|
||||
android:layout_marginStart="32dp"
|
||||
android:layout_marginTop="32dp"
|
||||
android:layout_marginEnd="32dp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
@@ -31,33 +31,66 @@
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="32dp"
|
||||
android:text="@string/upload_pan"
|
||||
app:layout_constraintBottom_toTopOf="@id/image_layout"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="@+id/logo_image"
|
||||
app:layout_constraintTop_toBottomOf="@+id/logo_image" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/image_view"
|
||||
<LinearLayout
|
||||
android:id="@+id/image_layout"
|
||||
android:layout_width="0dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_height="200sp"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:background="@drawable/img_placeholder"
|
||||
android:layout_height="300dp"
|
||||
android:orientation="vertical"
|
||||
app:layout_constraintBottom_toTopOf="@id/skip_pan_upload_button"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/upload_pan_label_text" />
|
||||
app:layout_constraintTop_toBottomOf="@+id/upload_pan_label_text">
|
||||
|
||||
<com.camerakit.CameraKitView
|
||||
android:id="@+id/image_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="300dp"
|
||||
android:adjustViewBounds="true"
|
||||
android:keepScreenOn="true"
|
||||
app:camera_facing="back"
|
||||
app:camera_flash="auto"
|
||||
app:camera_focus="continuous"
|
||||
app:camera_permissions="camera"/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/preview_image"
|
||||
android:layout_width="340dp"
|
||||
android:layout_height="264dp"
|
||||
android:visibility="invisible"
|
||||
android:layout_gravity="center"
|
||||
app:layout_constraintBottom_toTopOf="@id/skip_pan_upload_button"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/upload_pan_label_text" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<Button
|
||||
android:id="@+id/skip_pan_upload_button"
|
||||
android:layout_width="0dp"
|
||||
android:textSize="18sp"
|
||||
android:background="@android:color/transparent"
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="@color/colorGray"
|
||||
android:background="@android:color/transparent"
|
||||
android:text="@string/skip_upload_enter_pan"
|
||||
android:textColor="@color/colorGray"
|
||||
android:textSize="18sp"
|
||||
app:layout_constraintEnd_toEndOf="@+id/upload_pan_label_text"
|
||||
app:layout_constraintStart_toStartOf="@+id/upload_pan_label_text"
|
||||
app:layout_constraintTop_toBottomOf="@+id/image_view" />
|
||||
app:layout_constraintTop_toBottomOf="@+id/image_layout" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/capture_button"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:text="@string/capture"
|
||||
app:layout_constraintEnd_toEndOf="@+id/skip_pan_upload_button"
|
||||
app:layout_constraintStart_toStartOf="@+id/skip_pan_upload_button"
|
||||
app:layout_constraintTop_toBottomOf="@+id/skip_pan_upload_button" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/upload_button"
|
||||
@@ -71,7 +104,8 @@
|
||||
app:layout_constraintTop_toBottomOf="@+id/skip_pan_upload_button" />
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/checkbox"
|
||||
android:id="@+id/consent_checkbox"
|
||||
android:visibility="invisible"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/pan_consent"
|
||||
|
||||
@@ -71,6 +71,7 @@
|
||||
<string name="ok">OK</string>
|
||||
<string name="enter_amount">Enter Amount</string>
|
||||
<string name="pan_format_error">PAN is not in proper format</string>
|
||||
<string name="capture">Capture</string>
|
||||
<string-array name="reason_list">
|
||||
<item>Home Loan</item>
|
||||
<item>Person loan</item>
|
||||
|
||||
Reference in New Issue
Block a user