आउटपुट को बदलें

CameraX के इस्तेमाल के उदाहरण का आउटपुट दो तरह का होता है: बफ़र और ट्रांसफ़ॉर्मेशन की जानकारी. बफ़र, बाइट कलेक्शन होता है. वहीं, ट्रांसफ़ॉर्मेशन की जानकारी से यह पता चलता है कि असली उपयोगकर्ताओं को दिखाने से पहले, बफ़र को कैसे काटा और घुमाया जाना चाहिए. ट्रांसफ़ॉर्मेशन को लागू करने का तरीका, बफ़र के फ़ॉर्मैट पर निर्भर करता है.

ImageCapture

ImageCapture के इस्तेमाल के उदाहरण के लिए, डिस्क में सेव करने से पहले, क्रॉप रेक्ट बफ़र लागू किया जाता है. साथ ही, रोटेशन को Exif डेटा में सेव किया जाता है. ऐप्लिकेशन को कुछ और करने की ज़रूरत नहीं है.

झलक देखें

Preview के इस्तेमाल के उदाहरण के लिए, SurfaceRequest.setTransformationInfoListener() को कॉल करके, ट्रांसफ़ॉर्मेशन की जानकारी पाई जा सकती है. जब भी ट्रांसफ़ॉर्मेशन को अपडेट किया जाता है, तब कॉलर को एक नया SurfaceRequest.TransformationInfo ऑब्जेक्ट मिलता है.

ट्रांसफ़ॉर्मेशन की जानकारी को लागू करने का तरीका, Surface के सोर्स पर निर्भर करता है. आम तौर पर, यह तरीका आसान नहीं होता. अगर आपका मकसद सिर्फ़ झलक दिखाना है, तो PreviewView का इस्तेमाल करें. PreviewView एक कस्टम व्यू है, जो ट्रांसफ़ॉर्मेशन को अपने-आप हैंडल करता है. ऐडवांस इस्तेमाल के लिए, जब आपको झलक वाली स्ट्रीम में बदलाव करना हो, जैसे कि OpenGL के साथ, तो CameraX कोर टेस्ट ऐप्लिकेशन में कोड सैंपल देखें.

निर्देशांकों को बदलना

एक और सामान्य टास्क, बफ़र के बजाय कोऑर्डिनेट के साथ काम करना है. जैसे, झलक में पहचाने गए चेहरे के आस-पास बॉक्स बनाना. ऐसे मामलों में, आपको इमेज के विश्लेषण से पहचाने गए चेहरे के निर्देशांकों को, झलक में बदलना होगा.

यहां दिया गया कोड स्निपेट, एक ऐसा मैट्रिक्स बनाता है जो इमेज के विश्लेषण के कोऑर्डिनेट को PreviewView के कोऑर्डिनेट से मैप करता है. Matrix की मदद से (x, y) निर्देशांकों को बदलने के लिए, 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
}