출력 변환
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
CameraX 사용 사례의 출력은 버퍼와 변환 정보 두 가지입니다. 버퍼는 바이트 배열이고 변환 정보는 최종 사용자에게 표시하기 전에 버퍼를 자르고 회전하는 방법입니다. 변환을 적용하는 방법은 버퍼 형식에 따라 달라집니다.
ImageCapture
ImageCapture
사용 사례의 경우 디스크에 저장하기 전에 자르기 직사각형 버퍼가 적용되고 회전은 EXIF 데이터에 저장됩니다. 앱에서 필요한 추가 작업은 없습니다.
미리보기
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;
}
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-08-27(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-08-27(UTC)"],[],[],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```"]]