转换输出
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
CameraX 用例的输出由两部分组成:缓冲区和转换信息。缓冲区是一个字节数组,而转换信息指明在向最终用户显示缓冲区之前应如何对其进行剪裁和旋转。转换的应用方式取决于缓冲区的格式。
ImageCapture
对于 ImageCapture
用例,剪裁矩形缓冲区会先应用,然后再保存到磁盘,并且旋转信息会保存在 Exif 数据中。应用无需执行任何其他操作。
预览
对于 Preview
用例,您可以通过调用 SurfaceRequest.setTransformationInfoListener()
获取转换信息。每次更新转换信息时,调用方都会收到一个新的 SurfaceRequest.TransformationInfo
对象。
转换信息的应用方式取决于 Surface
的来源,通常比较琐碎。如果目标只是显示预览,请使用 PreviewView
。PreviewView
是自动处理转换信息的自定义视图。对于需要修改预览流(例如,使用 OpenGL 修改)的高级用途,请查看 CameraX 核心测试应用中的代码示例。
转换坐标
另一个常见任务是处理坐标(而不是缓冲区),例如预览时围绕检测到的人脸绘制一个框。在这种情况下,您需要将检测到的人脸的坐标从图片分析转换为预览。
以下代码段会创建一个矩阵,将用于图片分析的坐标映射到 PreviewView
坐标。如需使用 Matrix
转换 (x, y) 坐标,请参阅 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;
}
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Transform output\n\nThe output of a CameraX use case is twofold: the buffer and the transformation\ninfo. The buffer is a byte array and the transformation info is how the buffer\nshould be cropped and rotated before being shown to end users. How to apply the\ntransformation depends on the format of the buffer.\n\nImageCapture\n------------\n\nFor the `ImageCapture` use case, the crop rect buffer is applied before saving\nto disk and the rotation is saved in the Exif data. There is no additional\naction needed from the app.\n\nPreview\n-------\n\nFor the `Preview` use case, you can get the transformation information by\ncalling\n[`SurfaceRequest.setTransformationInfoListener()`](/reference/androidx/camera/core/SurfaceRequest#setTransformationInfoListener(java.util.concurrent.Executor,%20androidx.camera.core.SurfaceRequest.TransformationInfoListener)).\nEvery time the transformation is updated, the caller receives a new\n[`SurfaceRequest.TransformationInfo`](/reference/androidx/camera/core/SurfaceRequest.TransformationInfo)\nobject.\n\nHow to apply the transformation information depends on the source of the\n`Surface`, and is usually non-trivial. If the goal is to simply display the\npreview, use `PreviewView`. `PreviewView` is a custom view that automatically\nhandles transformation. For advanced uses, when you need to edit the preview\nstream, such as with OpenGL, look at the code sample in the [CameraX core test\napp](https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-main/camera/integration-tests/coretestapp/src/main/java/androidx/camera/integration/core).\n\nTransform coordinates\n---------------------\n\nAnother common task is to work with the coordinates instead of the buffer, such\nas drawing a box around the detected face in preview. In cases such as this, you\nneed to transform the coordinates of the detected face from image analysis to\npreview.\n\nThe following code snippet creates a matrix that maps from image analysis\ncoordinates to `PreviewView` coordinates. To transform the (x, y) coordinates\nwith a [`Matrix`](/reference/android/graphics/Matrix), see\n[`Matrix.mapPoints()`](/reference/android/graphics/Matrix#mapPoints(float%5B%5D)). \n\n### Kotlin\n\n```kotlin\nfun getCorrectionMatrix(imageProxy: ImageProxy, previewView: PreviewView) : Matrix {\n val cropRect = imageProxy.cropRect\n val rotationDegrees = imageProxy.imageInfo.rotationDegrees\n val matrix = Matrix()\n\n // A float array of the source vertices (crop rect) in clockwise order.\n val source = floatArrayOf(\n cropRect.left.toFloat(),\n cropRect.top.toFloat(),\n cropRect.right.toFloat(),\n cropRect.top.toFloat(),\n cropRect.right.toFloat(),\n cropRect.bottom.toFloat(),\n cropRect.left.toFloat(),\n cropRect.bottom.toFloat()\n )\n\n // A float array of the destination vertices in clockwise order.\n val destination = floatArrayOf(\n 0f,\n 0f,\n previewView.width.toFloat(),\n 0f,\n previewView.width.toFloat(),\n previewView.height.toFloat(),\n 0f,\n previewView.height.toFloat()\n )\n\n // The destination vertexes need to be shifted based on rotation degrees. The\n // rotation degree represents the clockwise rotation needed to correct the image.\n\n // Each vertex is represented by 2 float numbers in the vertices array.\n val vertexSize = 2\n // The destination needs to be shifted 1 vertex for every 90° rotation.\n val shiftOffset = rotationDegrees / 90 * vertexSize;\n val tempArray = destination.clone()\n for (toIndex in source.indices) {\n val fromIndex = (toIndex + shiftOffset) % source.size\n destination[toIndex] = tempArray[fromIndex]\n }\n matrix.setPolyToPoly(source, 0, destination, 0, 4)\n return matrix\n}\n```\n\n### Java\n\n```java\nMatrix getMappingMatrix(ImageProxy imageProxy, PreviewView previewView) {\n Rect cropRect = imageProxy.getCropRect();\n int rotationDegrees = imageProxy.getImageInfo().getRotationDegrees();\n Matrix matrix = new Matrix();\n\n // A float array of the source vertices (crop rect) in clockwise order.\n float[] source = {\n cropRect.left,\n cropRect.top,\n cropRect.right,\n cropRect.top,\n cropRect.right,\n cropRect.bottom,\n cropRect.left,\n cropRect.bottom\n };\n\n // A float array of the destination vertices in clockwise order.\n float[] destination = {\n 0f,\n 0f,\n previewView.getWidth(),\n 0f,\n previewView.getWidth(),\n previewView.getHeight(),\n 0f,\n previewView.getHeight()\n };\n\n // The destination vertexes need to be shifted based on rotation degrees.\n // The rotation degree represents the clockwise rotation needed to correct\n // the image.\n\n // Each vertex is represented by 2 float numbers in the vertices array.\n int vertexSize = 2;\n // The destination needs to be shifted 1 vertex for every 90° rotation.\n int shiftOffset = rotationDegrees / 90 * vertexSize;\n float[] tempArray = destination.clone();\n for (int toIndex = 0; toIndex \u003c source.length; toIndex++) {\n int fromIndex = (toIndex + shiftOffset) % source.length;\n destination[toIndex] = tempArray[fromIndex];\n }\n matrix.setPolyToPoly(source, 0, destination, 0, 4);\n return matrix;\n}\n```"]]