تحويل المخرجات

يتضمّن ناتج حالة استخدام CameraX جزأين: المخزن المؤقت ومعلومات التحويل. المخزن المؤقت هو مصفوفة بايت، ومعلومات التحويل هي كيفية اقتصاص المخزن المؤقت وتدويره قبل عرضه على المستخدمين النهائيين. تعتمد طريقة تطبيق عملية التحويل على تنسيق المخزن المؤقت.

ImageCapture

بالنسبة إلى حالة الاستخدام 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;
}