Skip to content
Snippets Groups Projects
Commit 444e85d1 authored by Agsha Athalla Nurkareem's avatar Agsha Athalla Nurkareem
Browse files

fix: scan camera

parent a26068eb
No related merge requests found
...@@ -47,6 +47,7 @@ class GraphPage : Fragment(R.layout.fragment_graph_page) { ...@@ -47,6 +47,7 @@ class GraphPage : Fragment(R.layout.fragment_graph_page) {
pieChart.setUsePercentValues(true) pieChart.setUsePercentValues(true)
pieChart.legend.isEnabled = false pieChart.legend.isEnabled = false
pieChart.description.isEnabled = false
progressBarTumbas.max = 100 progressBarTumbas.max = 100
transactionRepository = TransactionRepositoryImplement(db.transactionDao(), requireContext()) transactionRepository = TransactionRepositoryImplement(db.transactionDao(), requireContext())
......
...@@ -2,11 +2,10 @@ package com.example.bondoman ...@@ -2,11 +2,10 @@ package com.example.bondoman
import android.app.Activity import android.app.Activity
import android.content.ContentValues import android.content.ContentValues
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.net.ConnectivityManager import android.graphics.Bitmap
import android.net.NetworkCapabilities import android.graphics.Matrix
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
...@@ -51,6 +50,8 @@ class ScanPage : Fragment() { ...@@ -51,6 +50,8 @@ class ScanPage : Fragment() {
private val binding get() = _binding!! private val binding get() = _binding!!
private var imageCapture: ImageCapture? = null private var imageCapture: ImageCapture? = null
private val PICK_IMAGE_REQUEST = 2 private val PICK_IMAGE_REQUEST = 2
private var cameraOn = false
private var imagePath = ""
val db by lazy { TransactionDB(requireContext()) } val db by lazy { TransactionDB(requireContext()) }
...@@ -65,9 +66,12 @@ class ScanPage : Fragment() { ...@@ -65,9 +66,12 @@ class ScanPage : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
binding.nextButton.visibility = View.GONE
binding.hasilFoto.visibility = View.GONE
binding.retryButton.visibility = View.GONE
if (allPermissionsGranted()) { if (allPermissionsGranted()) {
startCamera() startCamera()
cameraOn = true
} else { } else {
ActivityCompat.requestPermissions( ActivityCompat.requestPermissions(
requireActivity(), REQUIRED_PERMISSIONS, 10 requireActivity(), REQUIRED_PERMISSIONS, 10
...@@ -75,13 +79,34 @@ class ScanPage : Fragment() { ...@@ -75,13 +79,34 @@ class ScanPage : Fragment() {
} }
binding.shutterButton.setOnClickListener { binding.shutterButton.setOnClickListener {
takePhoto() if (cameraOn){
takePhoto()
} else {
cameraOn = true
binding.nextButton.visibility = View.GONE
binding.hasilFoto.visibility = View.GONE
binding.retryButton.visibility = View.GONE
binding.previewView.visibility = View.VISIBLE
}
} }
binding.selectButton.setOnClickListener { binding.selectButton.setOnClickListener {
selectPhoto() selectPhoto()
} }
binding.nextButton.setOnClickListener {
sendImageToAPI(imagePath)
}
binding.retryButton.setOnClickListener {
cameraOn = true
binding.nextButton.visibility = View.GONE
binding.hasilFoto.visibility = View.GONE
binding.retryButton.visibility = View.GONE
binding.previewView.visibility = View.VISIBLE
binding.shutterButton.visibility = View.VISIBLE
}
} }
private fun selectPhoto() { private fun selectPhoto() {
...@@ -151,8 +176,18 @@ class ScanPage : Fragment() { ...@@ -151,8 +176,18 @@ class ScanPage : Fragment() {
val dataIndex = it.getColumnIndexOrThrow(MediaStore.Images.Media.DATA) val dataIndex = it.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
if (it.moveToFirst()) { if (it.moveToFirst()) {
val path = it.getString(dataIndex) val path = it.getString(dataIndex)
val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, savedUri)
val rotationMat = Matrix().apply { postRotate(90f) }
val rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, rotationMat, true)
binding.hasilFoto.setImageBitmap(rotatedBitmap)
binding.hasilFoto.visibility = View.VISIBLE
binding.previewView.visibility = View.GONE
binding.nextButton.visibility = View.VISIBLE
binding.retryButton.visibility = View.VISIBLE
binding.shutterButton.visibility = View.GONE
imagePath = path
Log.d("Camera", "File path: $path") Log.d("Camera", "File path: $path")
sendImageToAPI(path)
} }
} }
cursor?.close() cursor?.close()
...@@ -218,12 +253,13 @@ class ScanPage : Fragment() { ...@@ -218,12 +253,13 @@ class ScanPage : Fragment() {
val retro = Retrofit.getInstance().create(EndpointScan::class.java) val retro = Retrofit.getInstance().create(EndpointScan::class.java)
Log.d("Scan", "pathname: ${pathname}") Log.d("Scan", "pathname: ${pathname}")
val file = File(pathname) val file = File(pathname)
val requestBody = RequestBody.create(MediaType.parse("image/jpeg"), file) val requestBody = RequestBody.create(MediaType.parse("image/jpeg"), file)
val filePart = MultipartBody.Part.createFormData("file", file.name, requestBody) val filePart = MultipartBody.Part.createFormData("file", file.name, requestBody)
Log.d("Scan", "filePart: ${filePart}") Log.d("Scan", "filePart: ${filePart}")
Toast.makeText(requireContext(), "Mroses gambar..", Toast.LENGTH_SHORT).show()
lifecycleScope.launch { lifecycleScope.launch {
try { try {
val response = retro.scan("Bearer $token", filePart) val response = retro.scan("Bearer $token", filePart)
...@@ -286,12 +322,13 @@ class ScanPage : Fragment() { ...@@ -286,12 +322,13 @@ class ScanPage : Fragment() {
var count = 0 var count = 0
for (item in data) { for (item in data) {
val price = item.price * item.qty
db.transactionDao().addTransaction( db.transactionDao().addTransaction(
Transaction( Transaction(
0, 0,
payloadJson.optString("nim"), payloadJson.optString("nim"),
item.name, item.name,
item.price.toString(), price.toString(),
"Pengeluaran", "Pengeluaran",
"", "",
SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(System.currentTimeMillis()) SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(System.currentTimeMillis())
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
/> />
<LinearLayout <FrameLayout
android:id="@+id/layout_scan" android:id="@+id/layout_scan"
android:layout_width="270dp" android:layout_width="270dp"
android:layout_height="350dp" android:layout_height="350dp"
...@@ -30,7 +30,13 @@ ...@@ -30,7 +30,13 @@
android:id="@+id/previewView" android:id="@+id/previewView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />
</LinearLayout>
<ImageView
android:id="@+id/hasil_Foto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
</FrameLayout>
<Button <Button
android:id="@+id/shutter_button" android:id="@+id/shutter_button"
...@@ -44,15 +50,40 @@ ...@@ -44,15 +50,40 @@
android:contentDescription="@string/scan_take_picture" android:contentDescription="@string/scan_take_picture"
/> />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="@id/layout_scan"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="40dp"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lanjut"
android:id="@+id/next_button"
android:background="@drawable/custom_button"
android:textColor="@color/white"
android:layout_marginRight="40dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ulang"
android:id="@+id/retry_button"
android:background="@drawable/select_button"/>
</LinearLayout>
<Button <Button
android:id="@+id/select_button" android:id="@+id/select_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="20dp" android:layout_marginTop="130dp"
android:text="@string/select_photos" android:text="@string/select_photos"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/shutter_button" app:layout_constraintTop_toBottomOf="@id/layout_scan"
android:background="@drawable/select_button"/> android:background="@drawable/select_button"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
\ 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