Skip to content

Commit

Permalink
Merge pull request #65 from team-JMT/feat/bitmap_resizing
Browse files Browse the repository at this point in the history
비트맵 이미지를 리사이징하여 파일로 저장하는 기능 구현 및 적용
  • Loading branch information
dogdduddy authored Sep 6, 2023
2 parents 7694e10 + 73ee78c commit 06261fd
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,26 @@
package org.gdsc.presentation.login

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.setFragmentResultListener
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.bumptech.glide.Glide
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import org.gdsc.presentation.BaseFragment
import org.gdsc.presentation.databinding.FragmentSignUpCompleteBinding
import org.gdsc.presentation.utils.BitmapUtils.getCompressedBitmapFromUri
import org.gdsc.presentation.utils.BitmapUtils.saveBitmapToFile
import org.gdsc.presentation.utils.checkMediaPermissions
import org.gdsc.presentation.utils.findPath
import org.gdsc.presentation.utils.showMediaPermissionsDialog
import org.gdsc.presentation.view.MainActivity
import java.io.File


class SignUpCompleteFragment : BaseFragment() {
Expand Down Expand Up @@ -79,15 +65,22 @@ class SignUpCompleteFragment : BaseFragment() {

binding.nextBtn.setOnClickListener {
viewModel.profileImageState.value.let {
if(it.isNullOrEmpty())
if(it.isEmpty())
viewModel.requestSignUpWithoutImage { moveToMain() }
else {
val file = File(it.toUri().findPath(requireContext()))
val requestFile = RequestBody.create(MediaType.parse("image/png"), file)
val body =
MultipartBody.Part.createFormData("profileImg", file.name, requestFile)
viewModel.requestSignUpWithImage(body) {
moveToMain()
val file = it.toUri()
.getCompressedBitmapFromUri(requireContext())
?.saveBitmapToFile(requireContext(), "profile.jpg")

if (file != null) {
val requestFile = RequestBody.create(MediaType.parse("image/png"), file)
val body =
MultipartBody.Part.createFormData("profileImg", file.name, requestFile)
viewModel.requestSignUpWithImage(body) {
moveToMain()
}
} else {
// TODO: Exception Handling
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.gdsc.presentation.utils

import android.content.ContentResolver
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.net.Uri
import android.os.Environment
import androidx.exifinterface.media.ExifInterface
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream

object BitmapUtils {

fun Bitmap.saveBitmapToFile(context: Context, fileName: String): File {
val directory = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
directory?.mkdirs()
val file = File(directory, fileName)

var outputStream: FileOutputStream? = null
try {
outputStream = FileOutputStream(file)
this.compress(Bitmap.CompressFormat.JPEG, 90, outputStream)
outputStream.flush()
this.recycle()
return file
} catch (e: IOException) {
e.printStackTrace()
} finally {
outputStream?.close()
}

return file
}

private fun Bitmap.rotateBitmap(angle: Float): Bitmap {
val matrix = Matrix()
matrix.postRotate(angle)
return Bitmap.createBitmap(
this, 0, 0, this.width, this.height, matrix, true
)
}

private fun getOrientationOfImage(inputStream: InputStream): Int {
val exif = try {
ExifInterface(inputStream)
} catch (e: IOException) {
e.printStackTrace()
return -1
}
val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1)
if (orientation != -1) {
when (orientation) {
ExifInterface.ORIENTATION_ROTATE_90 -> return 90
ExifInterface.ORIENTATION_ROTATE_180 -> return 180
ExifInterface.ORIENTATION_ROTATE_270 -> return 270
}
}
return 0
}

fun Uri.getCompressedBitmapFromUri(context: Context): Bitmap? {
val resolver: ContentResolver = context.contentResolver
var inputStream: InputStream? = null
try {

val options = BitmapFactory.Options().apply {
inSampleSize = 2
}

inputStream = resolver.openInputStream(this)

val bitmap = requireNotNull(BitmapFactory.decodeStream(inputStream, null, options))

val orientation = getOrientationOfImage(inputStream!!).toFloat()

return bitmap.rotateBitmap(orientation)
} catch (e: Exception) {
e.printStackTrace()
} finally {
inputStream?.close()
}
return null
}

}





Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ import org.gdsc.domain.Empty
import org.gdsc.presentation.BaseFragment
import org.gdsc.presentation.R
import org.gdsc.presentation.databinding.FragmentRegisterRestaurantBinding
import org.gdsc.presentation.utils.BitmapUtils.getCompressedBitmapFromUri
import org.gdsc.presentation.utils.BitmapUtils.saveBitmapToFile
import org.gdsc.presentation.utils.addAfterTextChangedListener
import org.gdsc.presentation.utils.repeatWhenUiStarted
import org.gdsc.presentation.utils.animateShrinkWidth
import org.gdsc.presentation.utils.checkMediaPermissions
import org.gdsc.presentation.utils.findPath
import org.gdsc.presentation.view.MainActivity
import org.gdsc.presentation.view.WebViewActivity
import org.gdsc.presentation.view.custom.FoodCategoryBottomSheetDialog
import org.gdsc.presentation.view.restaurantregistration.adapter.RegisterRestaurantAdapter
import org.gdsc.presentation.view.restaurantregistration.viewmodel.RegisterRestaurantViewModel
import java.io.File

@AndroidEntryPoint
class RegisterRestaurantFragment : BaseFragment() {
Expand Down Expand Up @@ -218,20 +218,28 @@ class RegisterRestaurantFragment : BaseFragment() {
if (navArgs.targetRestaurantId == -1) {
val pictures = mutableListOf<MultipartBody.Part>()

list.forEach {
list.forEachIndexed { index, sUri ->

val file = File(it.toUri().findPath(requireContext()))
sUri.toUri()
.getCompressedBitmapFromUri(context)
?.saveBitmapToFile(context, "$index.jpg")?.let { imageFile ->

val requestFile =
RequestBody.create(MediaType.parse("image/png"), file)
val body =
MultipartBody.Part.createFormData(
"pictures",
file.name,
requestFile
)
val requestFile =
RequestBody.create(
MediaType.parse("image/png"),
imageFile
)

val body =
MultipartBody.Part.createFormData(
"pictures",
imageFile.name,
requestFile
)

pictures.add(body)
pictures.add(body)

}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ class RegisterRestaurantViewModel @Inject constructor(
_foodCategoryState.value = FoodCategoryItem(FoodCategory.fromName(it.category))
_drinkPossibilityState.value = it.canDrinkLiquor
_recommendDrinkTextState.value = it.goWellWithLiquor
_recommendMenuListState.value = it.recommendMenu.split("#").drop(1).apply {
println(" isRecommendMenuFullState 테스트 초기화 : ${this}")
}
_recommendMenuListState.value = it.recommendMenu.split("#").drop(1)
_introductionTextState.value = it.introduce
_restaurantPlaceName.value = it.name
_restaurantLocationId.value = restaurantId.toString()
Expand Down

0 comments on commit 06261fd

Please sign in to comment.