Last active
May 16, 2020 08:58
-
-
Save kongkip/695159258864df8ecb861d869bdf5077 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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