הפלט של תרחיש לדוגמה של CameraX כפול: מאגר הנתונים הזמני והטרנספורמציה מידע. מאגר הנתונים הזמני הוא מערך בייטים ופרטי הטרנספורמציה הם האופן שבו מאגר הנתונים הזמני צריך לחתוך ולסובב את התמונה לפני שהיא תוצג למשתמשי הקצה. איך ליישם את תלויה בפורמט של מאגר הנתונים הזמני.
ImageCapture
בתרחיש לדוגמה ImageCapture
, מאגר ה-rect של החיתוך מיושם לפני השמירה בדיסק והסיבוב נשמר בנתוני ה-Exif. אין צורך לבצע פעולה נוספת באפליקציה.
תצוגה מקדימה
בתרחיש לדוגמה Preview
, אפשר לקבל את פרטי הטרנספורמציה באמצעות קריאה ל-SurfaceRequest.setTransformationInfoListener()
.
בכל פעם שהטרנספורמציה מתעדכנת, המתקשר מקבל
SurfaceRequest.TransformationInfo
לאובייקט.
האופן שבו ניתן להחיל את פרטי הטרנספורמציה תלוי במקור
Surface
, ולרוב הוא לא טריווי. אם המטרה היא רק להציג את התצוגה המקדימה, משתמשים ב-PreviewView
. PreviewView
היא תצוגה בהתאמה אישית שמטפלת באופן אוטומטי בטרנספורמציה. לשימושים מתקדמים, אם צריך לערוך את התצוגה המקדימה
בשידור חי, למשל באמצעות OpenGL, בודקים את דוגמת הקוד בבדיקת הליבה של CameraX.
app.
טרנספורמציה של קואורדינטות
עוד משימה נפוצה היא לעבוד עם הקואורדינטות במקום עם מאגר הנתונים הזמני, כמו שרטוט של תיבה סביב הפנים שזוהו בתצוגה המקדימה. במקרים כאלה, צריך לשנות את הקואורדינטות של הפנים שזוהו מניתוח התמונה תצוגה מקדימה.
קטע הקוד הבא יוצר מטריקס שממפה קואורדינטות של ניתוח תמונה לקווי אורך ורוחב של 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; }