Mentransformasi output

Output dari kasus penggunaan CameraX ada dua: buffer dan info transformasi. Buffer adalah array byte dan info transformasi adalah cara buffer harus dipangkas dan diputar sebelum ditampilkan kepada pengguna akhir. Cara menerapkan transformasi bergantung pada format buffer.

ImageCapture

Untuk kasus penggunaan ImageCapture, buffer crop rect diterapkan sebelum menyimpan ke disk dan rotasi disimpan dalam data Exif. Tidak ada tindakan tambahan yang diperlukan dari aplikasi.

Pratinjau

Untuk kasus penggunaan Preview, Anda dapat memperoleh informasi transformasi dengan memanggil SurfaceRequest.setTransformationInfoListener(). Setiap kali transformasi diperbarui, pemanggil akan menerima objek SurfaceRequest.TransformationInfo baru.

Cara menerapkan informasi transformasi bergantung pada sumber Surface, dan biasanya bersifat signifikan. Jika sasarannya adalah untuk hanya menampilkan pratinjau, gunakan PreviewView. PreviewView adalah tampilan kustom yang otomatis menangani transformasi. Untuk penggunaan lanjutan, saat Anda perlu mengedit streaming pratinjau, seperti pada OpenGL, lihat contoh kode di aplikasi uji inti CameraX.

Mentransformasi koordinat

Tugas umum lainnya adalah bekerja dengan koordinat, bukan buffer, seperti menggambar kotak di sekitar wajah yang terdeteksi dalam pratinjau. Dalam kasus seperti ini, Anda perlu mentransformasi koordinat wajah yang terdeteksi dari analisis gambar menjadi pratinjau.

Cuplikan kode berikut membuat matriks yang memetakan dari koordinat analisis gambar ke koordinat PreviewView. Untuk mentransformasikan koordinat (x, y) dengan Matrix, lihat Matrix.mapPoints().

fun getCorrectionMatrix(imageProxy: ImageProxy, previewView: PreviewView): Matrix {
    val cropRect = imageProxy.cropRect
    val rotationDegrees = imageProxy.imageInfo.rotationDegrees
    val matrix = Matrix()

    // A float array of the source vertices (crop rect) in clockwise order.
    val source = floatArrayOf(
        cropRect.left.toFloat(),
        cropRect.top.toFloat(),
        cropRect.right.toFloat(),
        cropRect.top.toFloat(),
        cropRect.right.toFloat(),
        cropRect.bottom.toFloat(),
        cropRect.left.toFloat(),
        cropRect.bottom.toFloat()
    )

    // A float array of the destination vertices in clockwise order.
    val destination = floatArrayOf(
        0f,
        0f,
        previewView.width.toFloat(),
        0f,
        previewView.width.toFloat(),
        previewView.height.toFloat(),
        0f,
        previewView.height.toFloat()
    )

    // The destination vertexes need to be shifted based on rotation degrees. The
    // rotation degree represents the clockwise rotation needed to correct the image.

    // Each vertex is represented by 2 float numbers in the vertices array.
    val vertexSize = 2
    // The destination needs to be shifted 1 vertex for every 90° rotation.
    val shiftOffset = rotationDegrees / 90 * vertexSize
    val tempArray = destination.clone()
    for (toIndex in source.indices) {
        val fromIndex = (toIndex + shiftOffset) % source.size
        destination[toIndex] = tempArray[fromIndex]
    }
    matrix.setPolyToPoly(source, 0, destination, 0, 4)
    return matrix
}