#178, image_matrix_limit を元にResizeConfigを作る部分で平方ピクセルとピクセルを混同してるバグがあった。3.4.2以降でもアカウント設定のリサイズ指定を尊重するようにした。
This commit is contained in:
parent
f8f964cbfb
commit
18a638433c
|
@ -256,7 +256,7 @@ class ActColumnCustomize : AppCompatActivity(), View.OnClickListener, ColorPicke
|
|||
this,
|
||||
fileUri,
|
||||
size,
|
||||
skipIfNoNeedToResizeAndRotate = true
|
||||
skipIfNoNeedToResizeAndRotate = true,
|
||||
)
|
||||
if (bitmap != null) {
|
||||
try {
|
||||
|
|
|
@ -386,28 +386,6 @@ val appSettingRoot = AppSettingItem(null, SettingType.Section, R.string.app_sett
|
|||
|
||||
section(R.string.post) {
|
||||
|
||||
// spinner(PrefI.ipResizeImage, R.string.resize_image) { activity ->
|
||||
// ActPost.resizeConfigList.map {
|
||||
// when (it.type) {
|
||||
// ResizeType.None -> activity.getString(R.string.dont_resize)
|
||||
// ResizeType.LongSide -> activity.getString(R.string.long_side_pixel, it.size)
|
||||
// ResizeType.SquarePixel -> activity.getString(
|
||||
// R.string.resize_square_pixels,
|
||||
// it.size * it.size,
|
||||
// it.size
|
||||
// )
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// text(PrefS.spMediaSizeMax, R.string.media_attachment_max_byte_size, InputTypeEx.number)
|
||||
// text(PrefS.spMovieSizeMax, R.string.media_attachment_max_byte_size_movie, InputTypeEx.number)
|
||||
// text(
|
||||
// PrefS.spMediaSizeMaxPixelfed,
|
||||
// R.string.media_attachment_max_byte_size_pixelfed,
|
||||
// InputTypeEx.number
|
||||
// )
|
||||
|
||||
spinner(
|
||||
PrefI.ipRefreshAfterToot,
|
||||
R.string.refresh_after_toot,
|
||||
|
|
|
@ -63,11 +63,12 @@ fun ColumnViewHolder.loadBackgroundImage(iv: ImageView, url: String?) {
|
|||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
createResizedBitmap(
|
||||
activity, url.toUri(),
|
||||
activity,
|
||||
url.toUri(),
|
||||
when {
|
||||
screenW > screenH -> screenW
|
||||
else -> screenH
|
||||
}
|
||||
},
|
||||
)
|
||||
} catch (ex: Throwable) {
|
||||
ColumnViewHolder.log.trace(ex)
|
||||
|
|
|
@ -30,6 +30,7 @@ import java.io.*
|
|||
import java.util.*
|
||||
import java.util.concurrent.CancellationException
|
||||
import kotlin.coroutines.coroutineContext
|
||||
import kotlin.math.min
|
||||
|
||||
class AttachmentRequest(
|
||||
val account: SavedAccount,
|
||||
|
@ -295,10 +296,8 @@ class AttachmentUploader(
|
|||
}
|
||||
}
|
||||
val mediaConfig = ti.configuration?.jsonObject("media_attachments")
|
||||
val imageResizeConfig = mediaConfig?.int("image_matrix_limit")
|
||||
?.takeIf { it > 0 }
|
||||
?.let { ResizeConfig(ResizeType.SquarePixel, it) }
|
||||
?: account.getResizeConfig()
|
||||
val serverMaxSqPixel = mediaConfig?.int("image_matrix_limit")?.takeIf { it > 0 }
|
||||
val imageResizeConfig = account.getResizeConfig()
|
||||
|
||||
// 入力データの変換など
|
||||
val opener = createOpener(
|
||||
|
@ -308,17 +307,21 @@ class AttachmentUploader(
|
|||
mediaConfig = mediaConfig,
|
||||
imageResizeConfig = imageResizeConfig,
|
||||
postAttachment = pa,
|
||||
serverMaxSqPixel = serverMaxSqPixel,
|
||||
)
|
||||
|
||||
val mediaSizeMax = when {
|
||||
mimeType.startsWith("video") || mimeType.startsWith("audio") ->
|
||||
mimeType.startsWith("video") || mimeType.startsWith("audio") -> min(
|
||||
account.getMovieMaxBytes(ti),
|
||||
mediaConfig?.int("video_size_limit")
|
||||
?.takeIf { it > 0 }
|
||||
?: account.getMovieMaxBytes(ti)
|
||||
?.takeIf { it > 0 } ?: Int.MAX_VALUE,
|
||||
)
|
||||
|
||||
else -> mediaConfig?.int("image_size_limit")
|
||||
?.takeIf { it > 0 }
|
||||
?: account.getImageMaxBytes(ti)
|
||||
else -> min(
|
||||
account.getImageMaxBytes(ti),
|
||||
mediaConfig?.int("image_size_limit")
|
||||
?.takeIf { it > 0 } ?: Int.MAX_VALUE,
|
||||
)
|
||||
}
|
||||
|
||||
if (opener.contentLength > mediaSizeMax) {
|
||||
|
@ -561,8 +564,9 @@ class AttachmentUploader(
|
|||
account: SavedAccount,
|
||||
uri: Uri,
|
||||
mimeType: String,
|
||||
mediaConfig: JsonObject? = null,
|
||||
imageResizeConfig: ResizeConfig,
|
||||
serverMaxSqPixel: Int? = null,
|
||||
mediaConfig: JsonObject? = null,
|
||||
postAttachment: PostAttachment? = null,
|
||||
): InputStreamOpener {
|
||||
when {
|
||||
|
@ -572,7 +576,8 @@ class AttachmentUploader(
|
|||
uri,
|
||||
mimeType,
|
||||
imageResizeConfig,
|
||||
postAttachment,
|
||||
postAttachment = postAttachment,
|
||||
serverMaxSqPixel = serverMaxSqPixel,
|
||||
)
|
||||
} catch (ex: Throwable) {
|
||||
log.w(ex, "createResizedImageOpener failed. fall back to original image.")
|
||||
|
@ -585,8 +590,9 @@ class AttachmentUploader(
|
|||
uri,
|
||||
mimeType,
|
||||
imageResizeConfig,
|
||||
postAttachment,
|
||||
forcePng = true
|
||||
postAttachment = postAttachment,
|
||||
forcePng = true,
|
||||
serverMaxSqPixel = serverMaxSqPixel,
|
||||
)
|
||||
|
||||
// 動画のトランスコード(失敗したらオリジナルデータにフォールバックする)
|
||||
|
@ -610,6 +616,7 @@ class AttachmentUploader(
|
|||
uri: Uri,
|
||||
mimeType: String,
|
||||
imageResizeConfig: ResizeConfig,
|
||||
serverMaxSqPixel: Int?,
|
||||
postAttachment: PostAttachment? = null,
|
||||
forcePng: Boolean = false,
|
||||
): InputStreamOpener {
|
||||
|
@ -628,7 +635,8 @@ class AttachmentUploader(
|
|||
context,
|
||||
uri,
|
||||
imageResizeConfig,
|
||||
skipIfNoNeedToResizeAndRotate = !forcePng
|
||||
skipIfNoNeedToResizeAndRotate = !forcePng,
|
||||
serverMaxSqPixel = serverMaxSqPixel
|
||||
) ?: error("createResizedBitmap returns null.")
|
||||
postAttachment?.progress = context.getString(R.string.attachment_handling_compress)
|
||||
try {
|
||||
|
@ -832,7 +840,7 @@ class AttachmentUploader(
|
|||
account,
|
||||
src.uri,
|
||||
mimeType,
|
||||
imageResizeConfig = ResizeConfig(ResizeType.SquarePixel, 400)
|
||||
imageResizeConfig = ResizeConfig(ResizeType.SquarePixel, 400),
|
||||
)
|
||||
|
||||
val mediaSizeMax = 1000000
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package jp.juggler.util
|
||||
|
||||
//import it.sephiroth.android.library.exif2.ExifInterface
|
||||
import android.content.Context
|
||||
import android.graphics.*
|
||||
import androidx.exifinterface.media.ExifInterface
|
||||
import android.net.Uri
|
||||
import androidx.annotation.StringRes
|
||||
//import it.sephiroth.android.library.exif2.ExifInterface
|
||||
import androidx.exifinterface.media.ExifInterface
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.InputStream
|
||||
import kotlin.math.max
|
||||
|
@ -81,23 +81,39 @@ class ResizeConfig(
|
|||
ResizeType.None -> type.toString()
|
||||
else -> "$type,$size"
|
||||
}
|
||||
|
||||
override fun toString() = "ResizeConfig($spec)"
|
||||
}
|
||||
|
||||
private fun PointF.limitBySqPixel(aspect: Float, maxSqPixels: Float): PointF {
|
||||
val currentSqPixels = x * y
|
||||
return when {
|
||||
maxSqPixels <= 0 -> this
|
||||
currentSqPixels <= maxSqPixels -> this
|
||||
else -> {
|
||||
val y = sqrt(maxSqPixels / aspect)
|
||||
val x = aspect * y
|
||||
PointF(x, y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun createResizedBitmap(
|
||||
context: Context,
|
||||
uri: Uri,
|
||||
sizeLongSide: Int,
|
||||
skipIfNoNeedToResizeAndRotate: Boolean = false
|
||||
) =
|
||||
createResizedBitmap(
|
||||
context,
|
||||
uri,
|
||||
when {
|
||||
sizeLongSide <= 0 -> ResizeConfig(ResizeType.None, 0)
|
||||
else -> ResizeConfig(ResizeType.LongSide, sizeLongSide)
|
||||
},
|
||||
skipIfNoNeedToResizeAndRotate = skipIfNoNeedToResizeAndRotate
|
||||
)
|
||||
serverMaxSqPixel: Int? = null,
|
||||
skipIfNoNeedToResizeAndRotate: Boolean = false,
|
||||
) = createResizedBitmap(
|
||||
context,
|
||||
uri,
|
||||
when {
|
||||
sizeLongSide <= 0 -> ResizeConfig(ResizeType.None, 0)
|
||||
else -> ResizeConfig(ResizeType.LongSide, sizeLongSide)
|
||||
},
|
||||
serverMaxSqPixel = serverMaxSqPixel,
|
||||
skipIfNoNeedToResizeAndRotate = skipIfNoNeedToResizeAndRotate
|
||||
)
|
||||
|
||||
fun createResizedBitmap(
|
||||
context: Context,
|
||||
|
@ -108,10 +124,13 @@ fun createResizedBitmap(
|
|||
// リサイズ指定
|
||||
resizeConfig: ResizeConfig,
|
||||
|
||||
// 真の場合、リサイズも回転も必要ないならnullを返す
|
||||
skipIfNoNeedToResizeAndRotate: Boolean = false
|
||||
// サーバ側の最大平方ピクセル
|
||||
serverMaxSqPixel: Int? = null,
|
||||
|
||||
): Bitmap? {
|
||||
// 真の場合、リサイズも回転も必要ないならnullを返す
|
||||
skipIfNoNeedToResizeAndRotate: Boolean = false,
|
||||
|
||||
): Bitmap? {
|
||||
|
||||
try {
|
||||
|
||||
|
@ -142,10 +161,9 @@ fun createResizedBitmap(
|
|||
|
||||
/// 出力サイズの計算
|
||||
val sizeSpec = resizeConfig.size.toFloat()
|
||||
val dstSize: PointF = when (resizeConfig.type) {
|
||||
var dstSize: PointF = when (resizeConfig.type) {
|
||||
|
||||
ResizeType.None ->
|
||||
srcSize
|
||||
ResizeType.None -> srcSize
|
||||
|
||||
ResizeType.LongSide ->
|
||||
if (max(srcSize.x, srcSize.y) <= resizeConfig.size) {
|
||||
|
@ -164,17 +182,11 @@ fun createResizedBitmap(
|
|||
}
|
||||
}
|
||||
|
||||
ResizeType.SquarePixel -> {
|
||||
val maxPixels = sizeSpec * sizeSpec
|
||||
val currentPixels = srcSize.x * srcSize.y
|
||||
if (currentPixels <= maxPixels) {
|
||||
srcSize
|
||||
} else {
|
||||
val y = sqrt(maxPixels / aspect)
|
||||
val x = aspect * y
|
||||
PointF(x, y)
|
||||
}
|
||||
}
|
||||
ResizeType.SquarePixel -> srcSize.limitBySqPixel(aspect, sizeSpec * sizeSpec)
|
||||
}
|
||||
|
||||
if (serverMaxSqPixel != null && serverMaxSqPixel > 0) {
|
||||
dstSize = dstSize.limitBySqPixel(aspect, serverMaxSqPixel.toFloat())
|
||||
}
|
||||
|
||||
val dstSizeInt = Point(
|
||||
|
@ -184,12 +196,24 @@ fun createResizedBitmap(
|
|||
|
||||
val resizeRequired = dstSizeInt.x != srcSize.x.toInt() || dstSizeInt.y != srcSize.y.toInt()
|
||||
|
||||
log.i("createResizedBitmap: rc=${
|
||||
resizeConfig
|
||||
}, src=${
|
||||
srcSize
|
||||
}, dst=${
|
||||
dstSizeInt
|
||||
}, ori=${
|
||||
orientation
|
||||
}, resizeRequired=${
|
||||
resizeRequired
|
||||
}")
|
||||
|
||||
// リサイズも回転も必要がない場合
|
||||
if (skipIfNoNeedToResizeAndRotate &&
|
||||
(orientation == null || orientation == 1) &&
|
||||
!resizeRequired
|
||||
) {
|
||||
log.d("createResizedBitmap: no need to resize or rotate")
|
||||
log.w("createResizedBitmap: no need to resize or rotate.")
|
||||
return null
|
||||
}
|
||||
|
||||
|
|
|
@ -658,10 +658,6 @@
|
|||
android:labelFor="@+id/etMediaSizeMax"
|
||||
android:text="@string/media_attachment_max_byte_size" />
|
||||
|
||||
<TextView
|
||||
style="@style/setting_row_label"
|
||||
android:text="@string/option_deprecated_mastodon342" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/etMediaSizeMax"
|
||||
style="@style/setting_edit_text"
|
||||
|
@ -677,10 +673,6 @@
|
|||
style="@style/setting_row_label"
|
||||
android:text="@string/resize_image" />
|
||||
|
||||
<TextView
|
||||
style="@style/setting_row_label"
|
||||
android:text="@string/option_deprecated_mastodon342" />
|
||||
|
||||
<Spinner
|
||||
android:id="@+id/spResizeImage"
|
||||
style="@style/setting_row_button"
|
||||
|
|
Loading…
Reference in New Issue