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

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

ImageCapture

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

झलक देखें

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

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

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

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

नीचे दिया गया कोड स्निपेट, एक मैट्रिक्स बनाता है. यह मैट्रिक्स, इमेज के विश्लेषण के निर्देशांकों को PreviewView निर्देशांकों से मैप करता है. (x, y) निर्देशांकों को Matrix की मदद से बदलने के लिए, Matrix.mapPoints() देखें.

Kotlin

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
}

Java

Matrix getMappingMatrix(ImageProxy imageProxy, PreviewView previewView) {
   Rect cropRect = imageProxy.getCropRect();
   int rotationDegrees = imageProxy.getImageInfo().getRotationDegrees();
   Matrix matrix = new Matrix();

   // A float array of the source vertices (crop rect) in clockwise order.
   float[] source = {
       cropRect.left,
       cropRect.top,
       cropRect.right,
       cropRect.top,
       cropRect.right,
       cropRect.bottom,
       cropRect.left,
       cropRect.bottom
   };

   // A float array of the destination vertices in clockwise order.
   float[] destination = {
       0f,
       0f,
       previewView.getWidth(),
       0f,
       previewView.getWidth(),
       previewView.getHeight(),
       0f,
       previewView.getHeight()
   };

   // 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.
   int vertexSize = 2;
   // The destination needs to be shifted 1 vertex for every 90° rotation.
   int shiftOffset = rotationDegrees / 90 * vertexSize;
   float[] tempArray = destination.clone();
   for (int toIndex = 0; toIndex < source.length; toIndex++) {
       int fromIndex = (toIndex + shiftOffset) % source.length;
       destination[toIndex] = tempArray[fromIndex];
   }
   matrix.setPolyToPoly(source, 0, destination, 0, 4);
   return matrix;
}