เอาต์พุตของ Use Case ของ CameraX มี 2 ส่วน ได้แก่ บัฟเฟอร์และข้อมูลการแปลง บัฟเฟอร์คืออาร์เรย์ไบต์ และข้อมูลการแปลงคือวิธีครอบตัดและหมุนบัฟเฟอร์ก่อนที่จะแสดงต่อผู้ใช้ปลายทาง วิธีใช้การแปลงจะขึ้นอยู่กับรูปแบบของบัฟเฟอร์
ImageCapture
สำหรับ Use Case ImageCapture ระบบจะใช้บัฟเฟอร์สี่เหลี่ยมผืนผ้าครอบตัดก่อนที่จะบันทึกลงดิสก์ และจะบันทึกการหมุนในข้อมูล Exif แอปไม่จำเป็นต้องดำเนินการเพิ่มเติม
แสดงตัวอย่าง
สำหรับ Use Case Preview คุณสามารถรับข้อมูลการแปลงได้โดยการ
เรียกใช้
SurfaceRequest.setTransformationInfoListener()
ทุกครั้งที่การแปลงได้รับการอัปเดต ผู้เรียกจะได้รับออบเจ็กต์ใหม่
SurfaceRequest.TransformationInfo
วิธีใช้ข้อมูลการแปลงจะขึ้นอยู่กับแหล่งที่มาของ Surface และมักจะเป็นเรื่องที่ซับซ้อน หากเป้าหมายคือการแสดงตัวอย่าง ให้ใช้ PreviewView PreviewView เป็นมุมมองที่กำหนดเองซึ่งจัดการการแปลงโดยอัตโนมัติ สำหรับการใช้งานขั้นสูง เมื่อคุณต้องการแก้ไขสตรีมตัวอย่าง เช่น ด้วย OpenGL ให้ดูตัวอย่างโค้ดในแอปทดสอบหลักของ CameraX
แปลงพิกัด
อีกงานที่พบบ่อยคือการทำงานกับพิกัดแทนบัฟเฟอร์ เช่น การวาดกรอบรอบใบหน้าที่ตรวจพบในตัวอย่าง ในกรณีเช่นนี้ คุณต้องแปลงพิกัดของใบหน้าที่ตรวจพบจากการวิเคราะห์รูปภาพเป็นตัวอย่าง
ข้อมูลโค้ดต่อไปนี้สร้างเมทริกซ์ที่แมปจากพิกัดการวิเคราะห์รูปภาพเป็นพิกัด 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; }