-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from Nexters/feature/image-upload
프로필 사진 required 제거
- Loading branch information
Showing
8 changed files
with
118 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
presentation/src/main/java/com/keyme/presentation/utils/ImageUploadUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.keyme.presentation.utils | ||
|
||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import android.net.Uri | ||
import android.util.Base64 | ||
import timber.log.Timber | ||
import java.io.ByteArrayOutputStream | ||
|
||
object ImageUploadUtil { | ||
|
||
fun getProfileImageString(context: Context, uri: Uri): String? { | ||
val resized = resize(context, uri, 360) | ||
val result = resized?.let { | ||
val compressed = compress(it) | ||
Timber.d("><> compressed: ${compressed.byteCount}") | ||
getStringImage(compressed) | ||
} | ||
|
||
return result | ||
} | ||
|
||
private fun resize(context: Context, uri: Uri, resizePx: Int): Bitmap? { | ||
val options = BitmapFactory.Options() | ||
|
||
BitmapFactory.decodeStream(context.contentResolver.openInputStream(uri), null, options) | ||
var width = options.outWidth | ||
var height = options.outHeight | ||
var samplesize = 1 | ||
while (width > resizePx || height > resizePx) { | ||
width /= 2 | ||
height /= 2 | ||
samplesize *= 2 | ||
} | ||
options.inSampleSize = samplesize | ||
|
||
val resized = BitmapFactory.decodeStream( | ||
context.contentResolver.openInputStream(uri), | ||
null, | ||
options, | ||
) | ||
|
||
return resized?.let { | ||
Bitmap.createScaledBitmap(resized, width, height, true) | ||
} | ||
} | ||
|
||
private fun compress(bitmap: Bitmap): Bitmap { | ||
val baos = ByteArrayOutputStream() | ||
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos) | ||
|
||
return BitmapFactory.decodeByteArray( | ||
baos.toByteArray(), | ||
0, | ||
baos.toByteArray().size, | ||
) | ||
} | ||
|
||
private fun getStringImage(bitmap: Bitmap): String { | ||
val baos = ByteArrayOutputStream() | ||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos) | ||
val imageBytes = baos.toByteArray() | ||
return Base64.encodeToString(imageBytes, Base64.DEFAULT) | ||
} | ||
} |