Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pulsejet/memories
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Apr 11, 2024
2 parents 3211c8f + 2d49aa5 commit b872c33
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
7 changes: 5 additions & 2 deletions android/app/src/main/java/gallery/memories/NativeX.kt
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,12 @@ class NativeX(private val mCtx: MainActivity) {
)
)
} else if (path.matches(API.IMAGE_PREVIEW)) {
makeResponse(image.getPreview(parts[3].toLong()), "image/jpeg")
val x = request.url.getQueryParameter("x")?.toInt()
val y = request.url.getQueryParameter("y")?.toInt()
makeResponse(image.getPreview(parts[3].toLong(), x, y), "image/jpeg")
} else if (path.matches(API.IMAGE_FULL)) {
makeResponse(image.getFull(parts[3]), "image/jpeg")
val size = request.url.getQueryParameter("size")?.toInt()
makeResponse(image.getFull(parts[3], size), "image/jpeg")
} else if (path.matches(API.SHARE_URL)) {
makeResponse(dlService!!.shareUrl(URLDecoder.decode(parts[4], "UTF-8")))
} else if (path.matches(API.SHARE_BLOB)) {
Expand Down
51 changes: 35 additions & 16 deletions android/app/src/main/java/gallery/memories/service/ImageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import androidx.media3.common.util.UnstableApi
import gallery.memories.mapper.SystemImage
import java.io.ByteArrayOutputStream

@UnstableApi class ImageService(private val mCtx: Context, private val query: TimelineQuery) {
@UnstableApi
class ImageService(private val mCtx: Context, private val query: TimelineQuery) {
/**
* Get a preview image for a given image ID
* @param id The image ID
* @return The preview image as a JPEG byte array
*/
@Throws(Exception::class)
fun getPreview(id: Long): ByteArray {
fun getPreview(id: Long, x: Int?, y: Int?): ByteArray {
val sysImgs = SystemImage.getByIds(mCtx, listOf(id))
if (sysImgs.isEmpty()) {
throw Exception("Image not found")
Expand All @@ -26,14 +27,20 @@ import java.io.ByteArrayOutputStream
var h = sysImgs[0].height.toInt()
var w = sysImgs[0].width.toInt()

// cap to 1024x1024
if (h > 1024 || w > 1024) {
val scale = 1024.0 / Math.max(h, w)
h = (h * scale).toInt()
w = (w * scale).toInt()
// cap to x/y if provided, keeping aspect ratio
if (x != null && y != null) {
// calculate the aspect ratio
val aspect = w.toFloat() / h.toFloat()
if (x.toFloat() / y.toFloat() < aspect) {
w = x
h = (x.toFloat() / aspect).toInt()
} else {
w = (y.toFloat() * aspect).toInt()
h = y
}
}

val bitmap =
var bitmap =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val uri = sysImgs[0].uri
mCtx.contentResolver.loadThumbnail(
Expand All @@ -45,19 +52,19 @@ import java.io.ByteArrayOutputStream
MediaStore.Images.Thumbnails.getThumbnail(
mCtx.contentResolver, id, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND, null
)
?: MediaStore.Video.Thumbnails.getThumbnail(
mCtx.contentResolver, id, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND, null
)
?: throw Exception("Thumbnail not found")
?: MediaStore.Video.Thumbnails.getThumbnail(
mCtx.contentResolver, id, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND, null
)
?: throw Exception("Thumbnail not found")
}

val stream = ByteArrayOutputStream()

// resize to the desired dimensions
val resized = Bitmap.createScaledBitmap(bitmap, w, h, true)
bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true)

// compress to JPEG
resized.compress(Bitmap.CompressFormat.JPEG, 90, stream)
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream)

return stream.toByteArray()
}
Expand All @@ -68,22 +75,34 @@ import java.io.ByteArrayOutputStream
* @return The full image as a JPEG byte array
*/
@Throws(Exception::class)
fun getFull(auid: String): ByteArray {
fun getFull(auid: String, size: Int?): ByteArray {
val sysImgs = query.getSystemImagesByAUIDs(listOf(auid))
if (sysImgs.isEmpty()) {
throw Exception("Image not found")
}

val uri = sysImgs[0].uri

val bitmap =
var bitmap =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.decodeBitmap(ImageDecoder.createSource(mCtx.contentResolver, uri))
} else {
MediaStore.Images.Media.getBitmap(mCtx.contentResolver, uri)
?: throw Exception("Image not found")
}

val stream = ByteArrayOutputStream()

// resize to the desired dimensions if provided, keeping aspect ratio
if (size != null) {
val scale = size.toFloat() / Math.max(bitmap.width, bitmap.height)
if (scale < 1) {
val w = (bitmap.width * scale).toInt()
val h = (bitmap.height * scale).toInt()
bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true)
}
}

bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream)
return stream.toByteArray()
}
Expand Down

0 comments on commit b872c33

Please sign in to comment.