|
@@ -1,5 +1,11 @@
|
|
|
package eu.kanade.tachiyomi.util.system
|
|
|
|
|
|
+import android.graphics.Bitmap
|
|
|
+import android.graphics.BitmapFactory
|
|
|
+import android.graphics.Canvas
|
|
|
+import android.graphics.Rect
|
|
|
+import java.io.ByteArrayInputStream
|
|
|
+import java.io.ByteArrayOutputStream
|
|
|
import java.io.InputStream
|
|
|
import java.net.URLConnection
|
|
|
|
|
@@ -68,4 +74,71 @@ object ImageUtil {
|
|
|
GIF("image/gif", "gif"),
|
|
|
WEBP("image/webp", "webp")
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Check whether the image is a double image (width > height), return the result and original stream
|
|
|
+ */
|
|
|
+ fun isDoublePage(imageStream: InputStream): Pair<Boolean, InputStream> {
|
|
|
+ val imageBytes = imageStream.readBytes()
|
|
|
+
|
|
|
+ val options = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
|
|
+ BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size, options)
|
|
|
+
|
|
|
+ return Pair(options.outWidth > options.outHeight, ByteArrayInputStream(imageBytes))
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Extract the 'side' part from imageStream and return it as InputStream.
|
|
|
+ */
|
|
|
+ fun splitInHalf(imageStream: InputStream, side: Side): InputStream {
|
|
|
+ val imageBytes = imageStream.readBytes()
|
|
|
+
|
|
|
+ val imageBitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
|
|
|
+ val height = imageBitmap.height
|
|
|
+ val width = imageBitmap.width
|
|
|
+
|
|
|
+ val singlePage = Rect(0, 0, width / 2, height)
|
|
|
+
|
|
|
+ val half = Bitmap.createBitmap(width / 2, height, Bitmap.Config.ARGB_8888)
|
|
|
+ val part = when (side) {
|
|
|
+ Side.RIGHT -> Rect(width - width / 2, 0, width, height)
|
|
|
+ Side.LEFT -> Rect(0, 0, width / 2, height)
|
|
|
+ }
|
|
|
+ val canvas = Canvas(half)
|
|
|
+ canvas.drawBitmap(imageBitmap, part, singlePage, null)
|
|
|
+ val output = ByteArrayOutputStream()
|
|
|
+ half.compress(Bitmap.CompressFormat.JPEG, 100, output)
|
|
|
+
|
|
|
+ return ByteArrayInputStream(output.toByteArray())
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Split the image into left and right parts, then merge them into a new image.
|
|
|
+ */
|
|
|
+ fun splitAndMerge(imageStream: InputStream): InputStream {
|
|
|
+ val imageBytes = imageStream.readBytes()
|
|
|
+
|
|
|
+ val imageBitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
|
|
|
+ val height = imageBitmap.height
|
|
|
+ val width = imageBitmap.width
|
|
|
+
|
|
|
+ val result = Bitmap.createBitmap(width / 2, height * 2, Bitmap.Config.ARGB_8888)
|
|
|
+ val canvas = Canvas(result)
|
|
|
+ // right -> upper
|
|
|
+ val rightPart = Rect(width - width / 2, 0, width, height)
|
|
|
+ val upperPart = Rect(0, 0, width / 2, height)
|
|
|
+ canvas.drawBitmap(imageBitmap, rightPart, upperPart, null)
|
|
|
+ // left -> bottom
|
|
|
+ val leftPart = Rect(0, 0, width / 2, height)
|
|
|
+ val bottomPart = Rect(0, height, width / 2, height * 2)
|
|
|
+ canvas.drawBitmap(imageBitmap, leftPart, bottomPart, null)
|
|
|
+
|
|
|
+ val output = ByteArrayOutputStream()
|
|
|
+ result.compress(Bitmap.CompressFormat.JPEG, 100, output)
|
|
|
+ return ByteArrayInputStream(output.toByteArray())
|
|
|
+ }
|
|
|
+
|
|
|
+ enum class Side {
|
|
|
+ RIGHT, LEFT
|
|
|
+ }
|
|
|
}
|