Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/seperate text recog module #36

Merged
merged 6 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions app/src/main/java/com/konkuk/liteweight/ui/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.konkuk.liteweight.ui

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
Expand All @@ -14,11 +18,20 @@ import androidx.navigation.ui.setupWithNavController
import com.konkuk.capture.ui.enroll.EnrollBottomSheetDialogFragment
import com.konkuk.liteweight.R
import com.konkuk.liteweight.databinding.ActivityMainBinding
import com.konkuk.liteweight.util.showPermissionContextPopup
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private val requestPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (isGranted) {
navigateCaptureDialog()
} else {
showPermissionContextPopup(Manifest.permission.CAMERA)
}
}

private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityMainBinding
Expand Down Expand Up @@ -58,7 +71,7 @@ class MainActivity : AppCompatActivity() {
private fun handleEvent(event: Event) {
when (event) {
Event.CaptureClickEvent -> {
navigateCaptureDialog()
requestCameraTask()
}
}
}
Expand All @@ -73,7 +86,25 @@ class MainActivity : AppCompatActivity() {

override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment_content_main)
return navController.navigateUp(appBarConfiguration) ||
super.onSupportNavigateUp()
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}

private fun requestCameraTask() {
when {
ContextCompat.checkSelfPermission(
this,
Manifest.permission.CAMERA,
) == PackageManager.PERMISSION_GRANTED -> {
navigateCaptureDialog()
}

shouldShowRequestPermissionRationale(Manifest.permission.CAMERA) -> {
showPermissionContextPopup("카메라")
}

else -> {
requestPermissionLauncher.launch(Manifest.permission.CAMERA)
}
}
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/com/konkuk/liteweight/util/Extensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.konkuk.liteweight.util

import android.content.Intent
import android.net.Uri
import android.provider.Settings
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity

fun AppCompatActivity.showPermissionContextPopup(permissionName: String) {
AlertDialog.Builder(this)
.setTitle("κΆŒν•œ μš”μ²­")
.setMessage("ν•΄λ‹Ή κΈ°λŠ₯을 μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ”\n $permissionName κΆŒν•œμ΄ ν•„μš”ν•©λ‹ˆλ‹€.")
.setPositiveButton("λ™μ˜ν•˜κΈ°") { _, _ ->
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.data = Uri.parse("package:$packageName")
startActivity(intent)
}
.setNegativeButton("μ·¨μ†Œν•˜κΈ°") { _, _ -> }
.create()
.show()
}
Binary file added common/src/main/res/drawable/camera3x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions common/src/main/res/drawable/ic_search.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="28dp"
android:height="28dp"
android:viewportWidth="28"
android:viewportHeight="28">
<path
android:pathData="M24.566,25.509C25.136,26.079 26.014,25.2 25.445,24.643L20.805,19.99C22.433,18.19 23.332,15.847 23.329,13.42C23.329,7.988 18.912,3.57 13.479,3.57C8.047,3.57 3.629,7.988 3.629,13.42C3.629,18.852 8.047,23.27 13.479,23.27C15.929,23.27 18.194,22.366 19.926,20.869L24.566,25.509ZM4.866,13.42C4.866,8.668 8.739,4.807 13.478,4.807C18.23,4.807 22.09,8.668 22.09,13.42C22.09,18.171 18.23,22.032 13.478,22.032C8.739,22.032 4.866,18.171 4.866,13.42Z"
android:fillColor="#E09F1F"/>
</vector>
9 changes: 9 additions & 0 deletions common/src/main/res/drawable/pen_pressed.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@color/back_white" />
<stroke
android:width="1dp"
android:color="@color/stroke_yello" />
<corners android:radius="20dp" />
</shape>
7 changes: 7 additions & 0 deletions common/src/main/res/drawable/selector_camera.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- selector κ΅¬ν˜„-->
<item android:state_selected="true" android:drawable="@drawable/camera_pressed"/>
<item android:state_selected="false" android:drawable="@drawable/camera_unpressed"/>
</selector>
7 changes: 7 additions & 0 deletions common/src/main/res/drawable/selector_pen.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- selector κ΅¬ν˜„-->
<item android:state_selected="true" android:drawable="@drawable/pen_pressed"/>
<item android:state_selected="false" android:drawable="@drawable/camera_unpressed"/>
</selector>
2 changes: 2 additions & 0 deletions feat/capture/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />

<application>
<activity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.konkuk.capture.ui.capture

import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
Expand All @@ -17,6 +18,8 @@ import com.konkuk.capture.ui.enroll.EnrollTextInput
import com.konkuk.capture.ui.enroll.EnrollTextInputViewModel.Companion.BITMAP_PICTURE_KEY
import com.konkuk.capture.ui.enroll.EnrollTextInputViewModel.Companion.OCR_RESULT_KEY
import com.konkuk.capture.ui.enroll.EnrollTextInputViewModel.Companion.URI_PICTURE_KEY
import java.text.SimpleDateFormat
import java.util.Date

class CaptureActivity : AppCompatActivity() {

Expand All @@ -29,17 +32,28 @@ class CaptureActivity : AppCompatActivity() {
private var uriPicture: Uri? = null

private val takePictureLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult(),
) {
if (it.resultCode == RESULT_OK) {
(it.data?.extras?.get("data") as Bitmap?)?.let { bitmap ->
bitmapPicture = bitmap
binding.ivNutritionInfo.setImageBitmap(bitmap)
processImageRecognize(InputImage.fromBitmap(bitmap, 0))
} ?: reCapture()
} else {
reCapture()
ActivityResultContracts.TakePicture(),
) { isSuccess ->
if (isSuccess) {
binding.ivNutritionInfo.setImageURI(uriPicture)
uriPicture?.let { uri ->
processImageRecognize(
InputImage.fromFilePath(
this@CaptureActivity,
uri,
),
)
}
}
}

private fun createImageFile(): Uri? {
val now = SimpleDateFormat("yyMMdd_HHmmss").format(Date())
val content = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, "img_$now.jpg")
put(MediaStore.Images.Media.MIME_TYPE, "image/jpg")
}
return contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, content)
}

private val getPictureLauncher = registerForActivityResult(
Expand Down Expand Up @@ -70,7 +84,8 @@ class CaptureActivity : AppCompatActivity() {
val getPictureIntent = Intent(Intent.ACTION_GET_CONTENT).apply { type = "image/*" }

captureDialog = CaptureDialogFragment({
takePictureLauncher.launch(takePictureIntent)
uriPicture = createImageFile()
uriPicture?.let { uri -> takePictureLauncher.launch(uri) }
}, {
getPictureLauncher.launch(getPictureIntent)
}, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<corners android:radius="24dp" />

<solid android:color="@android:color/white" />

<stroke
android:width="1dp"
android:color="#BECADA" />

</shape>
7 changes: 3 additions & 4 deletions feat/capture/src/main/res/layout/activity_search.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/btn_back"
app:layout_constraintTop_toTopOf="@id/btn_back"
app:strokeColor="@color/main_blue"
app:strokeWidth="1dp">
app:strokeColor="@color/main_blue">

<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
Expand Down Expand Up @@ -86,9 +85,9 @@
<View
android:id="@+id/search_underline"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_height="1dp"
android:layout_marginTop="8dp"
android:background="@color/light_blue"
android:background="@color/main_blue"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cv_search" />
Expand Down
5 changes: 3 additions & 2 deletions feat/capture/src/main/res/layout/item_searched_food.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/rounded_corner_shape">
android:layout_marginHorizontal="28dp"
android:layout_marginVertical="14dp"
android:background="@drawable/rounded_corner_shape_lightgray">

<TextView
android:id="@+id/tv_name"
Expand Down
3 changes: 2 additions & 1 deletion feat/history/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<application>
<activity
android:name=".ui.history.statistic.HistoryStatisticsActivity"
android:exported="false" />
android:exported="false"
android:theme="@style/Theme.LiteWeight_Android.NoActionBar" />
</application>

</manifest>
21 changes: 21 additions & 0 deletions feat/personal/src/main/res/drawable/imageperson.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="178dp"
android:height="178dp"
android:viewportWidth="178"
android:viewportHeight="178">
<path
android:pathData="M89,89m-89,0a89,89 0,1 1,178 0a89,89 0,1 1,-178 0"
android:fillColor="#DCEDF9"/>
<path
android:pathData="M74.74,123.39C41.71,108.46 51.68,50.55 51.68,50.55C51.68,50.55 109.59,40.58 124.52,73.61C131.98,90.12 113.61,112.49 113.61,112.49C113.61,112.49 91.25,130.85 74.74,123.39Z"
android:strokeWidth="7"
android:fillColor="#00000000"
android:strokeColor="#0080FF"/>
<path
android:pathData="M73.66,72.53L89.64,88.51M127.6,126.47L99.63,98.5M99.63,98.5L105.21,76.93M99.63,98.5L89.64,88.51M89.64,88.51L70.33,91.83"
android:strokeLineJoin="round"
android:strokeWidth="7"
android:fillColor="#00000000"
android:strokeColor="#0080FF"
android:strokeLineCap="round"/>
</vector>
28 changes: 20 additions & 8 deletions feat/personal/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
xmlns:tools="http://schemas.android.com/tools">

<data>

<variable
name="vm"
type="com.konkuk.personal.ui.settings.SettingsViewModel" />
</data>


<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
Expand Down Expand Up @@ -39,28 +41,38 @@
android:layout_height="4dp"
android:layout_marginTop="20dp"
android:background="@color/light_blue"
android:visibility="invisible"
app:layout_constraintTop_toBottomOf="@id/iv_back_btn" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="24dp"
android:layout_marginHorizontal="24dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="24dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/horizontal_line1"
tools:layout_editor_absoluteX="24dp">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/imageperson" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="이름 λ˜λŠ” λ‹‰λ„€μž„" />

<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/ll_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:orientation="horizontal">

<TextView
Expand All @@ -86,15 +98,15 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="λ‚˜μ΄"
android:layout_marginTop="32dp"/>
android:layout_marginTop="32dp"
android:text="λ‚˜μ΄" />

<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/ll_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:orientation="horizontal">

<TextView
Expand All @@ -120,15 +132,15 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="성별"
android:layout_marginTop="32dp"/>
android:layout_marginTop="32dp"
android:text="성별" />

<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/ll_gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:orientation="horizontal">

<TextView
Expand Down