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

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

इमेज कैप्चर

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

झलक

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

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

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

एक अन्य सामान्य कार्य बफ़र के बजाय निर्देशांकों के साथ काम करना है, जैसे पहचानी गई इमेज की झलक में, उस चेहरे के चारों ओर बॉक्स बना रही होगी. ऐसे मामलों में, आपको जाने-पहचाने चेहरे के निर्देशांकों को चित्र विश्लेषण से बदलकर झलक देखें.

यह कोड स्निपेट, एक ऐसा मैट्रिक्स बनाता है जो इमेज के विश्लेषण से मैप होता है 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;
}