L'output di un caso d'uso di CameraX è duplice: il buffer e le informazioni sulla trasformazione. Il buffer è un array di byte e le informazioni sulla trasformazione indicano il modo in cui il buffer deve essere ritagliato e ruotato prima di essere mostrato agli utenti finali. Il modo in cui applicare la trasformazione dipende dal formato del buffer.
Acquisizione di immagini
Per il caso d'uso ImageCapture
, il buffer rettangolo di ritaglio viene applicato prima del salvataggio
su disco e la rotazione viene salvata nei dati EXIF. Non è necessario alcun intervento aggiuntivo
da parte dell'app.
Anteprima
Per il caso d'uso Preview
, puoi ottenere le informazioni sulla trasformazione
chiamando
SurfaceRequest.setTransformationInfoListener()
.
Ogni volta che la trasformazione viene aggiornata, il chiamante riceve un nuovo oggetto SurfaceRequest.TransformationInfo
.
Il modo in cui applicare le informazioni sulla trasformazione dipende dall'origine di
Surface
e in genere non è banale. Se l'obiettivo è semplicemente visualizzare
l'anteprima, utilizza PreviewView
. PreviewView
è una vista personalizzata che
gestisce automaticamente la trasformazione. Per utilizzi avanzati, quando devi modificare il flusso di anteprima, ad esempio con OpenGL, guarda l'esempio di codice nell'app di test principale di CameraX.
Trasformare le coordinate
Un'altra attività comune è lavorare con le coordinate anziché con il buffer, come disegnare un riquadro intorno al volto rilevato in anteprima. In questi casi, devi trasformare le coordinate del volto rilevato dall'analisi dell'immagine all'anteprima.
Il seguente snippet di codice crea una matrice che mappa dalle coordinate di analisi dell'immagine alle coordinate di PreviewView
. Per trasformare le coordinate (x, y)
con un elemento Matrix
, consulta
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; }