Improved QR code recognition when screen and camera orientation don't match

This commit is contained in:
klonfish 2018-03-19 21:37:35 +01:00
parent 2d59a99833
commit b249c1f36f

View file

@ -85,22 +85,38 @@ public final class CameraManager {
camera.setPreviewTexture(textureView.getSurfaceTexture());
final Camera.Parameters parameters = camera.getParameters();
cameraResolution = findBestPreviewSizeValue(parameters, textureView.getWidth(), textureView.getHeight());
final int width = textureView.getWidth();
final int height = textureView.getHeight();
final Camera.Parameters parameters = camera.getParameters();
cameraResolution = findBestPreviewSizeValue(parameters, width, height);
final int rawSize = Math.min(width * 2 / 3, height * 2 / 3);
final int frameSize = Math.max(MIN_FRAME_SIZE, Math.min(MAX_FRAME_SIZE, rawSize));
final int leftOffset = (width - frameSize) / 2;
final int topOffset = (height - frameSize) / 2;
frame = new Rect(leftOffset, topOffset, leftOffset + frameSize, topOffset + frameSize);
framePreview = new RectF(frame.left * cameraResolution.width / width,
frame.top * cameraResolution.height / height, frame.right * cameraResolution.width / width,
frame.bottom * cameraResolution.height / height);
float widthFactor;
float heightFactor;
Rect orientedFrame;
boolean isTexturePortrait = width < height;
boolean isCameraPortrait = cameraResolution.width < cameraResolution.height;
if (isTexturePortrait == isCameraPortrait) {
widthFactor = (float)cameraResolution.width / width;
heightFactor = (float)cameraResolution.height / height;
orientedFrame = new Rect(frame);
} else {
widthFactor = (float)cameraResolution.width / height;
heightFactor = (float)cameraResolution.height / width;
// Swap X and Y coordinates to flip frame to the same orientation as cameraResolution
orientedFrame = new Rect(frame.top, frame.left, frame.bottom, frame.right);
}
framePreview = new RectF(orientedFrame.left * widthFactor, orientedFrame.top * heightFactor,
orientedFrame.right * widthFactor, orientedFrame.bottom * heightFactor);
final String savedParameters = parameters == null ? null : parameters.flatten();