-
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 #142 from Team-Motivoo/feat/bitmap-resize
[Feat] 비트맵 리사이징
- Loading branch information
Showing
6 changed files
with
135 additions
and
30 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
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