Ausgabe transformieren

Ein CameraX-Anwendungsfall liefert zwei Ergebnisse: der Zwischenspeicher und die Transformation. Informationen. Der Zwischenspeicher ist ein Byte-Array und die Transformationsinformationen geben an, wie der Zwischenspeicher sollten zugeschnitten und gedreht werden, bevor sie Endnutzern angezeigt werden. So wenden Sie die hängt vom Format des Zwischenspeichers ab.

Bilderfassung

Für den Anwendungsfall ImageCapture wird der Zwischenspeicher für das Rechteck vor dem Speichern angewendet und die Rotation wird in den EXIF-Daten gespeichert. Es gibt keine weiteren die in der App erforderlich sind.

Vorschau

Für den Anwendungsfall Preview erhalten Sie die Transformationsinformationen folgendermaßen: Anrufen SurfaceRequest.setTransformationInfoListener() Bei jeder Aktualisierung der Transformation erhält der Aufrufer eine neue SurfaceRequest.TransformationInfo -Objekt enthält.

Wie die Transformationsinformationen angewendet werden, hängt von der Quelle der Surface und ist normalerweise nicht trivial. Wenn das Ziel darin besteht, einfach die Vorschau ansehen, verwenden Sie PreviewView. PreviewView ist eine benutzerdefinierte Ansicht, die automatisch wickelt die Transformation ab. Für die erweiterte Verwendung, wenn Sie die Vorschau bearbeiten müssen wie z. B. mit OpenGL, sehen Sie sich das Codebeispiel im CameraX Core Test an. App.

Koordinaten umwandeln

Eine weitere häufige Aufgabe besteht darin, mit den Koordinaten anstelle des Zwischenspeichers zu arbeiten, z. B. indem Sie in der Vorschau einen Rahmen um das erkannte Gesicht ziehen. In solchen Fällen die Koordinaten des erkannten Gesichts von der Bildanalyse in in der Vorschau ansehen.

Mit dem folgenden Code-Snippet wird eine Matrix für die Bildanalyse erstellt in PreviewView-Koordinaten umwandeln. Um die Koordinaten (x, y) zu transformieren mit einem Matrix, siehe 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;
}