Gross hack to make things sort of work

This commit is contained in:
Ivan Malison 2024-11-10 17:51:15 -07:00
parent 17f675657e
commit 5b52acda26
2 changed files with 57 additions and 9 deletions

View File

@ -25,9 +25,58 @@ import kotlinx.coroutines.withContext
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Matrix import android.graphics.Matrix
fun Bitmap.rotateBitmap(degrees: Float): Bitmap { fun rotateBitmap90CounterClockwise(source: Bitmap): Bitmap {
val matrix = android.graphics.Matrix().apply { postRotate(degrees) } val width = source.width
return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true) val height = source.height
// Create a new Bitmap with swapped width and height
val rotatedBitmap = Bitmap.createBitmap(height, width, source.config)
for (y in 0 until height) {
for (x in 0 until width) {
// Set the pixel in the new position
rotatedBitmap.setPixel(y, width - 1 - x, source.getPixel(x, y))
}
}
return rotatedBitmap
}
fun Bitmap.transformBitmap(orientation: Orientation): Bitmap {
return when (orientation) {
Orientation.PORTRAIT -> this // No transformation needed
Orientation.LANDSCAPE_LEFT -> {
// Transpose (swap width and height)
val transposedBitmap = Bitmap.createBitmap(height, width, config)
for (y in 0 until height) {
for (x in 0 until width) {
transposedBitmap.setPixel(y, width - 1 - x, getPixel(x, y))
}
}
transposedBitmap
}
Orientation.PORTRAIT_UPSIDE_DOWN -> {
// Invert vertically and horizontally (180-degree rotation)
val invertedBitmap = Bitmap.createBitmap(width, height, config)
for (y in 0 until height) {
for (x in 0 until width) {
invertedBitmap.setPixel(width - 1 - x, height - 1 - y, getPixel(x, y))
}
}
invertedBitmap
}
Orientation.LANDSCAPE_RIGHT -> {
// Transpose (swap width and height) and invert vertically
val transposedBitmap = Bitmap.createBitmap(height, width, config)
for (y in 0 until height) {
for (x in 0 until width) {
transposedBitmap.setPixel(height - 1 - y, x, getPixel(x, y))
}
}
transposedBitmap
}
}
} }
@ -97,10 +146,11 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) :
suspend fun getBitmap(): Bitmap? = withContext(Dispatchers.Main) { suspend fun getBitmap(): Bitmap? = withContext(Dispatchers.Main) {
val frame = holder.getSurfaceFrame() val frame = holder.getSurfaceFrame()
val width = frame.width() val width = frame.width()
val height = frame.height() val height = frame.height()
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) val bitmap = Bitmap.createBitmap(height, width, Bitmap.Config.ARGB_8888)
// Use a coroutine to suspend until the PixelCopy request is complete // Use a coroutine to suspend until the PixelCopy request is complete
suspendCancellableCoroutine<Bitmap?> { continuation -> suspendCancellableCoroutine<Bitmap?> { continuation ->
@ -109,9 +159,7 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) :
bitmap, bitmap,
{ copyResult -> { copyResult ->
if (copyResult == PixelCopy.SUCCESS) { if (copyResult == PixelCopy.SUCCESS) {
val rotationDegrees = inputOrientation.toDegrees().toFloat() continuation.resume(rotateBitmap90CounterClockwise(bitmap))
val rotatedBitmap = bitmap.rotateBitmap(rotationDegrees)
continuation.resume(bitmap)
} else { } else {
continuation.resumeWithException( continuation.resumeWithException(
RuntimeException("PixelCopy failed with error code $copyResult") RuntimeException("PixelCopy failed with error code $copyResult")
@ -120,7 +168,7 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) :
}, },
Handler(Looper.getMainLooper()) Handler(Looper.getMainLooper())
) )
} }
} }

View File

@ -11,7 +11,7 @@ class FileUtils {
companion object { companion object {
fun writeBitmapTofile(bitmap: Bitmap, file: File, quality: Int) { fun writeBitmapTofile(bitmap: Bitmap, file: File, quality: Int) {
FileOutputStream(file).use { stream -> FileOutputStream(file).use { stream ->
bitmap.compress(Bitmap.CompressFormat.PNG, quality, stream) bitmap.compress(Bitmap.CompressFormat.JPEG, 50, stream)
} }
} }