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

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

التقاط الصور

بالنسبة إلى حالة الاستخدام ImageCapture، يتم تطبيق المخزن المؤقت في مستطيل الاقتصاص قبل الحفظ. على القرص ويتم حفظ التدوير في بيانات Exif. لا تتوفر المزيد من الإجراء المطلوب من التطبيق.

معاينة

بالنسبة إلى حالة استخدام Preview، يمكنك الحصول على معلومات التحويل عن طريق يتصل SurfaceRequest.setTransformationInfoListener() وفي كل مرة يتم فيها تعديل التحويل، يتلقّى المتصل رسالة جديدة SurfaceRequest.TransformationInfo الخاص بك.

تعتمد كيفية تطبيق معلومات التحويل على مصدر Surface، وهي عادةً غير بسيطة. إذا كان الهدف هو عرض المعاينة: استخدِم PreviewView. PreviewView هي طريقة عرض مخصّصة يتم تلقائيًا مع التحويل. بالنسبة إلى الاستخدامات المتقدّمة، عند الحاجة إلى تعديل المعاينة في ساحة المشاركات، كما هو الحال في OpenGL، يمكنك الاطّلاع على نموذج الرموز في اختبار CameraX الأساسي التطبيق.

تحويل الإحداثيات

ومن المهام الشائعة الأخرى العمل باستخدام الإحداثيات بدلاً من المورد الاحتياطي، مثل مثل رسم مربّع حول الوجه الذي تم اكتشافه في المعاينة. في مثل هذه الحالات، لتحويل إحداثيات الوجه الذي تم اكتشافه من تحليل الصور معاينة.

ينشئ مقتطف الرمز التالي مصفوفة يتم تعيينها من تحليل الصور الإحداثيات إلى إحداثيات PreviewView. لتحويل إحداثيات (س، ص) مع 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;
}