Die Ausgabe eines CameraX-Anwendungsfalls besteht aus zwei Teilen: dem Puffer und den Transformationsinformationen. Der Puffer ist ein Byte-Array und die Transformationsinformationen geben an, wie der Puffer zugeschnitten und gedreht werden soll, bevor er Endnutzern angezeigt wird. Wie die Transformation angewendet wird, hängt vom Format des Puffers ab.
ImageCapture
Beim Anwendungsfall ImageCapture
wird der Puffer für das Zuschneiderechteck vor dem Speichern auf der Festplatte angewendet und die Drehung wird in den Exif-Daten gespeichert. Die App muss nichts weiter tun.
Vorschau
Für den Anwendungsfall Preview
können Sie die Transformationsinformationen abrufen, indem Sie SurfaceRequest.setTransformationInfoListener()
aufrufen.
Jedes Mal, wenn die Transformation aktualisiert wird, erhält der Aufrufer ein neues SurfaceRequest.TransformationInfo
-Objekt.
Wie Sie die Transformationsinformationen anwenden, hängt von der Quelle der Surface
ab und ist in der Regel nicht trivial. Wenn das Ziel nur darin besteht, die Vorschau anzuzeigen, verwenden Sie PreviewView
. PreviewView
ist eine benutzerdefinierte Ansicht, in der die Transformation automatisch erfolgt. Für erweiterte Anwendungsfälle, in denen Sie den Vorschaustream bearbeiten müssen, z. B. mit OpenGL, sehen Sie sich das Codebeispiel in der CameraX Core-Test-App an.
Koordinaten transformieren
Eine weitere gängige Aufgabe ist die Arbeit mit den Koordinaten anstelle des Puffers, z. B. das Zeichnen eines Rechtecks um das erkannte Gesicht in der Vorschau. In solchen Fällen müssen Sie die Koordinaten des erkannten Gesichts aus der Bildanalyse in die Vorschau transformieren.
Mit dem folgenden Code-Snippet wird eine Matrix erstellt, die die Koordinaten der Bildanalyse den PreviewView
-Koordinaten zuordnet. Informationen zum Transformieren der (x, y)-Koordinaten mit einer Matrix
finden Sie unter 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; }