เปลี่ยนรูปแบบเอาต์พุต

เอาต์พุตของกรณีการใช้งาน CameraX คือ 2 ส่วน ได้แก่ บัฟเฟอร์และการเปลี่ยนรูปแบบ ข้อมูลเพิ่มเติม บัฟเฟอร์เป็นไบต์อาร์เรย์ และข้อมูลการแปลงคือบัฟเฟอร์ ควรครอบตัดและหมุนก่อนที่จะแสดงต่อผู้ใช้ปลายทาง วิธีใช้ การเปลี่ยนรูปแบบขึ้นอยู่กับรูปแบบของบัฟเฟอร์

จับภาพ

สําหรับ 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;
}