Skip to content

Instantly share code, notes, and snippets.

@kongkip
Last active May 16, 2020 08:58
Show Gist options
  • Save kongkip/695159258864df8ecb861d869bdf5077 to your computer and use it in GitHub Desktop.
Save kongkip/695159258864df8ecb861d869bdf5077 to your computer and use it in GitHub Desktop.
package com.example.funitureclassifier
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.media.Image
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageProxy
class ClassificationActivity: ImageAnalysis.Analyzer {
override fun analyze(imageProxy: ImageProxy?, rotationDegrees: Int) {
imageProxy?.image?.let {
val bitmap = rotateImage(
imageToBitmap(it),
rotationDegrees.toFloat()
)
}
}
// rotate image to its original position
private fun rotateImage(source: Bitmap, angle: Float): Bitmap {
val matrix = Matrix()
matrix.postRotate(angle)
return Bitmap.createBitmap(source, 0, 0, source.width, source.height, matrix, true)
}
// convert image to bitmap
private fun imageToBitmap(image: Image): Bitmap {
val buffer = image.planes[0].buffer
val bytes = ByteArray(buffer.capacity())
buffer.get(bytes)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.size, null)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment