Created
January 8, 2021 07:06
-
-
Save osamaqarem/6245d8480b402b9dcd1824fe4cd0a5dc to your computer and use it in GitHub Desktop.
Android Reverse Camera Matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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