capacitor-community / capacitor-community/camera-preview

Preview size is not correctly calculated in Portrait mode

Open
#275 6 comments 1 reaction 0 assignees View on GitHub
Dominant language
Java
Stars
227
Forks
185
PR merge metrics
No merged PRs in 30d

Description

When I use camera preview in portrait mode on Android, the size/aspect ratio is not correctly calculated. The side edges of the preview are cut off, compared to what the regular camera shows.

I see these values in the log, which are obviously not correct, as they are the same values for width and height

optimal preview size: w: 1088 h: 1088

I believe this is because targetHeight is set to surface height, and then used to both discover available camera aspect ratios, and if no suitable one is not found, targetHeight only is used to find a match.

I made a quick test change to set targetHeight to surface width when in portrait mode, leaving the rest of the logic alone, and that seemed to work.

After this change, I see this in the log:

optimal preview size: w: 1920 h: 1080

and, the preview sides are now visible.

Code changed in Preview.java

private Camera.Size getOptimalPreviewSize(List sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
int targetHeight = h;
if (displayOrientation == 90 || displayOrientation == 270) {
targetRatio = (double) h / w;
**targetHeight = w;**
}

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.