-
Notifications
You must be signed in to change notification settings - Fork 0
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] 비트맵 리사이징 #142
Merged
Merged
[Feat] 비트맵 리사이징 #142
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package sopt.motivoo.util | ||
|
||
import android.graphics.Bitmap | ||
import okhttp3.RequestBody | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
import java.io.ByteArrayOutputStream | ||
|
||
class BitmapRequestBody(private val bitmap: Bitmap) { | ||
|
||
/** | ||
* byte size = output.toByteArray().size | ||
*/ | ||
fun create(quality: Int = 100): RequestBody { | ||
val output = ByteArrayOutputStream() | ||
try { | ||
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, output) | ||
} catch (e: Exception) { | ||
e.message | ||
} finally { | ||
output.close() | ||
} | ||
return output.toByteArray().toRequestBody() | ||
} | ||
} |
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,92 @@ | ||
package sopt.motivoo.util | ||
|
||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import android.graphics.ImageDecoder | ||
import android.graphics.Matrix | ||
import android.media.ExifInterface | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.provider.MediaStore | ||
|
||
class BitmapUtil(private val context: Context) { | ||
private fun loadOrientation(uri: Uri): Int { | ||
var orientation = 0 | ||
val stream = context.contentResolver.openInputStream(uri) ?: return 0 | ||
try { | ||
val exifInterface = ExifInterface(stream) | ||
orientation = exifInterface.getAttributeInt( | ||
ExifInterface.TAG_ORIENTATION, | ||
ExifInterface.ORIENTATION_NORMAL | ||
) | ||
} catch (e: Exception) { | ||
e.message | ||
} finally { | ||
stream.close() | ||
} | ||
return orientation | ||
} | ||
|
||
/** | ||
* @param bounds : if ture, assign bitmap in memory. if false, no assign bitmap in memory | ||
* @param size : return image ratio | ||
*/ | ||
private fun decodeUriToBitmap(uri: Uri, bounds: Boolean = false, size: Int = 1): Bitmap? { | ||
var bitmap: Bitmap? = null | ||
val options = BitmapFactory.Options().apply { | ||
inJustDecodeBounds = bounds | ||
inSampleSize = size | ||
} | ||
|
||
val stream = context.contentResolver.openInputStream(uri) | ||
try { | ||
bitmap = BitmapFactory.decodeStream(stream, null, options) | ||
} catch (e: Exception) { | ||
e.message | ||
} finally { | ||
stream?.close() | ||
} | ||
|
||
return bitmap | ||
} | ||
|
||
private fun rotateBitmap(orientation: Int, bitmap: Bitmap?): Bitmap? = when (orientation) { | ||
ExifInterface.ORIENTATION_ROTATE_90 -> rotateImage(bitmap, 90f) | ||
ExifInterface.ORIENTATION_ROTATE_180 -> rotateImage(bitmap, 180f) | ||
ExifInterface.ORIENTATION_ROTATE_270 -> rotateImage(bitmap, 270f) | ||
else -> bitmap | ||
} | ||
|
||
private fun rotateImage(bitmap: Bitmap?, angle: Float): Bitmap? { | ||
val matrix = Matrix().apply { postRotate(angle) } | ||
return bitmap?.let { | ||
Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true) | ||
} | ||
} | ||
|
||
/** | ||
* Use BitmapFactory Bitmap Resize | ||
*/ | ||
fun createUriToBitmap(uri: Uri, bounds: Boolean = false, size: Int = 1): Bitmap? { | ||
val orientation = loadOrientation(uri) | ||
val bitmap = decodeUriToBitmap(uri, bounds, size) | ||
|
||
return rotateBitmap(orientation, bitmap) | ||
} | ||
|
||
/** | ||
* Use ImageDecoder Bitmap Resize, easy convert uri to bitmap | ||
*/ | ||
fun createUriToBitmap(uri: Uri): Bitmap = | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
val source = | ||
ImageDecoder.createSource(context.contentResolver, uri) | ||
ImageDecoder.decodeBitmap(source) | ||
} else { | ||
MediaStore.Images.Media.getBitmap( | ||
context.contentResolver, | ||
uri | ||
) | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이미지 회전은 왜 필요한건가욤??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
글고 궁금한게 리사이징 해도 이미지 해상도는 크게 변화 없나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@l2zh
이유는 단순합니다. 카메라로 사진 촬영 시, 기기내부 센서의 방향에 사진이 저장되는데 해당 사진 URI를 비트맵으로 전환하는 내부 메타데이터로 회전된 뱡향이 적용된 사진이 보여지게 됩니다.
그냥 똑바로 안찍어서 회전되어보입니다..ㅋㅋ
(ImageDecoder를 사용하면 상관없지만, BitmapFactory를 사용할 때 생기는 이슈입니다.)
이미지의 해상도는 낮아질 것 같습니다. 하지만 크게 변화가 없다고 생각합니다. 이유는 압축 파일의 File Extension 을 JPEG로 사용하였기에 한 번의 압축은 이미지의 품질 저하를 많이 일으키지 않습니다.
하지만 이미지를 로드할 때, 큰 ImageView 에 작은 용량 사진을 넣으면 품질 저하가 나타날지도..? 라는 생각인데 요건 테스트를 진행해보려고 합니당.
(당연한 이야기 같기도..)
일단 제가 크게 파일의 용량을 줄인 이유는 우리가 진행하는 서비스의 사진은 작게 보이기 때문입니다! 🐬
이준희 시험 화이팅~ 🔥 🔥
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@l2zh
추가적으로 테스트해봤습니다.
기본적인 사진촬영과 카메라 사진은 1~3MB 합니다. 이에 대한 위 로직은 사진읜 품질저하를 일으키지 않습니다. 해상도를 1/4 비율로 낮추긴했지만, 크게 품질저하를 느끼지 못했습니다.
하지만 스크린샷이나 어디서 가져온 캡처본은 크기가 작습니다.
테스트한 123KB 캡처본으로 한 결과, 우리 서비스에서는 드라마틱한 품질 저하를 볼 수 없긴 합니다만 품질 저하는 확실히 있습니다.
로 하면서 해상도의 비율을 1/4 로 줄였지만, 1/2로 줄여도 괜찮을 것 같아 수정했습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 그렇군요! 감사합니다~
형도 시험 화이팅...ㅎㅎ