feat: Add photo preview with caption functionality in MediaPickerBottomSheet

This commit is contained in:
2026-01-30 01:05:30 +05:00
parent 8c30fc3549
commit 6720057ebc
7 changed files with 508 additions and 67 deletions

View File

@@ -216,4 +216,65 @@ object MediaUtils {
""
}
}
/**
* Получить размеры изображения из Uri без полной загрузки в память
*/
fun getImageDimensions(context: Context, uri: Uri): Pair<Int, Int> {
return try {
val options = BitmapFactory.Options().apply {
inJustDecodeBounds = true // Не загружаем в память, только размеры
}
context.contentResolver.openInputStream(uri)?.use { inputStream ->
BitmapFactory.decodeStream(inputStream, null, options)
}
var width = options.outWidth
var height = options.outHeight
// Учитываем масштабирование (как в uriToBase64Image)
if (width > MAX_IMAGE_SIZE || height > MAX_IMAGE_SIZE) {
val ratio = width.toFloat() / height.toFloat()
if (width > height) {
width = MAX_IMAGE_SIZE
height = (MAX_IMAGE_SIZE / ratio).toInt()
} else {
height = MAX_IMAGE_SIZE
width = (MAX_IMAGE_SIZE * ratio).toInt()
}
}
Log.d(TAG, "📐 Image dimensions: ${width}x${height}")
Pair(width, height)
} catch (e: Exception) {
Log.e(TAG, "📐 Failed to get image dimensions", e)
Pair(0, 0)
}
}
/**
* Получить размеры изображения из Base64 строки
*/
fun getImageDimensionsFromBase64(base64: String): Pair<Int, Int> {
return try {
// Убираем data URI prefix если есть
val base64Data = if (base64.contains(",")) {
base64.substringAfter(",")
} else {
base64
}
val bytes = Base64.decode(base64Data, Base64.DEFAULT)
val options = BitmapFactory.Options().apply {
inJustDecodeBounds = true
}
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, options)
Log.d(TAG, "📐 Image dimensions from base64: ${options.outWidth}x${options.outHeight}")
Pair(options.outWidth, options.outHeight)
} catch (e: Exception) {
Log.e(TAG, "📐 Failed to get image dimensions from base64", e)
Pair(0, 0)
}
}
}