Skip to content

Instantly share code, notes, and snippets.

@osamaqarem
Created January 8, 2021 07:06
Show Gist options
  • Save osamaqarem/6245d8480b402b9dcd1824fe4cd0a5dc to your computer and use it in GitHub Desktop.
Save osamaqarem/6245d8480b402b9dcd1824fe4cd0a5dc to your computer and use it in GitHub Desktop.
Android Reverse Camera Matrix
public Matrix calculateTextureMatrix(TextureView textureView, View previewView) {
Matrix matrix = new Matrix();
Camera.Size previewSize = mICamera.mCamera.getParameters().getPreviewSize();
float actualPreviewWidth = previewSize.width;
float actualPreviewHeight = previewSize.height;
float previewWidth = previewView.getWidth();
float previewHeight = previewView.getHeight();
int deviceAndCameraAngleDiff = mICamera.getCameraAngle(this);
float scaleX, scaleY;
if (deviceAndCameraAngleDiff % 180 == 0) {
scaleX = previewWidth / actualPreviewWidth;
scaleY = previewHeight / actualPreviewHeight;
} else {
scaleX = previewWidth / actualPreviewHeight;
scaleY = previewHeight / actualPreviewWidth;
}
float scale = Math.max(scaleX, scaleY);
matrix.postScale(
1 / scaleX * scale,
1 / scaleY * scale,
textureView.getWidth() / 2f,
textureView.getHeight() / 2f
);
return matrix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment