React Native Android get device's ratio












0















I'm using Camera that comes from expo package and I'm having trouble with camera view distortion. My phone has 2:1 ratio, which is non-standard.



When I use getSupportedRatiosAsync method of the camera, I get all kinds of ratios like 1:1, 2:1, 4:3, 16:9, however only 2:1 looks good.



How can I select a ratio that fits device's natural resolution? Is there a way, to access device's preferred ratio? Or is there any hack around it, like always selecting 16:9 and having the camera component add black margins, where the actual device's ratio is not fitting the 16:9?



EDIT:



My phone resolution is 2196x1080, therefore correct ratio should be 2:1. But I'm unable to come up with a function that would compute 2196x1080=>2:1, since (2 * 1080) !== 2196



Is there any way how to infer best possible ratio even for such stupid resolutions?










share|improve this question

























  • "My phone resolution is 2196x1080, therefore correct ratio should be 2:1." - The camera ratio is not necessarily the same size as the device screen ratio. Take a look at your native camera app - does it have a status bar? Does it have space for the button used to take the picture? If yes to either of these, then that explains the discepency between your phone display resolution and your camera resolution.

    – markmoxx
    Dec 12 '18 at 14:41
















0















I'm using Camera that comes from expo package and I'm having trouble with camera view distortion. My phone has 2:1 ratio, which is non-standard.



When I use getSupportedRatiosAsync method of the camera, I get all kinds of ratios like 1:1, 2:1, 4:3, 16:9, however only 2:1 looks good.



How can I select a ratio that fits device's natural resolution? Is there a way, to access device's preferred ratio? Or is there any hack around it, like always selecting 16:9 and having the camera component add black margins, where the actual device's ratio is not fitting the 16:9?



EDIT:



My phone resolution is 2196x1080, therefore correct ratio should be 2:1. But I'm unable to come up with a function that would compute 2196x1080=>2:1, since (2 * 1080) !== 2196



Is there any way how to infer best possible ratio even for such stupid resolutions?










share|improve this question

























  • "My phone resolution is 2196x1080, therefore correct ratio should be 2:1." - The camera ratio is not necessarily the same size as the device screen ratio. Take a look at your native camera app - does it have a status bar? Does it have space for the button used to take the picture? If yes to either of these, then that explains the discepency between your phone display resolution and your camera resolution.

    – markmoxx
    Dec 12 '18 at 14:41














0












0








0








I'm using Camera that comes from expo package and I'm having trouble with camera view distortion. My phone has 2:1 ratio, which is non-standard.



When I use getSupportedRatiosAsync method of the camera, I get all kinds of ratios like 1:1, 2:1, 4:3, 16:9, however only 2:1 looks good.



How can I select a ratio that fits device's natural resolution? Is there a way, to access device's preferred ratio? Or is there any hack around it, like always selecting 16:9 and having the camera component add black margins, where the actual device's ratio is not fitting the 16:9?



EDIT:



My phone resolution is 2196x1080, therefore correct ratio should be 2:1. But I'm unable to come up with a function that would compute 2196x1080=>2:1, since (2 * 1080) !== 2196



Is there any way how to infer best possible ratio even for such stupid resolutions?










share|improve this question
















I'm using Camera that comes from expo package and I'm having trouble with camera view distortion. My phone has 2:1 ratio, which is non-standard.



When I use getSupportedRatiosAsync method of the camera, I get all kinds of ratios like 1:1, 2:1, 4:3, 16:9, however only 2:1 looks good.



How can I select a ratio that fits device's natural resolution? Is there a way, to access device's preferred ratio? Or is there any hack around it, like always selecting 16:9 and having the camera component add black margins, where the actual device's ratio is not fitting the 16:9?



EDIT:



My phone resolution is 2196x1080, therefore correct ratio should be 2:1. But I'm unable to come up with a function that would compute 2196x1080=>2:1, since (2 * 1080) !== 2196



Is there any way how to infer best possible ratio even for such stupid resolutions?







android react-native android-camera react-native-android expo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 21:03







Albert Nemec

















asked Nov 25 '18 at 13:09









Albert NemecAlbert Nemec

1461310




1461310













  • "My phone resolution is 2196x1080, therefore correct ratio should be 2:1." - The camera ratio is not necessarily the same size as the device screen ratio. Take a look at your native camera app - does it have a status bar? Does it have space for the button used to take the picture? If yes to either of these, then that explains the discepency between your phone display resolution and your camera resolution.

    – markmoxx
    Dec 12 '18 at 14:41



















  • "My phone resolution is 2196x1080, therefore correct ratio should be 2:1." - The camera ratio is not necessarily the same size as the device screen ratio. Take a look at your native camera app - does it have a status bar? Does it have space for the button used to take the picture? If yes to either of these, then that explains the discepency between your phone display resolution and your camera resolution.

    – markmoxx
    Dec 12 '18 at 14:41

















"My phone resolution is 2196x1080, therefore correct ratio should be 2:1." - The camera ratio is not necessarily the same size as the device screen ratio. Take a look at your native camera app - does it have a status bar? Does it have space for the button used to take the picture? If yes to either of these, then that explains the discepency between your phone display resolution and your camera resolution.

– markmoxx
Dec 12 '18 at 14:41





"My phone resolution is 2196x1080, therefore correct ratio should be 2:1." - The camera ratio is not necessarily the same size as the device screen ratio. Take a look at your native camera app - does it have a status bar? Does it have space for the button used to take the picture? If yes to either of these, then that explains the discepency between your phone display resolution and your camera resolution.

– markmoxx
Dec 12 '18 at 14:41












2 Answers
2






active

oldest

votes


















1














On Samsung S9, the screen aspect ratio of 18.5:9 is very close to 2:1. And if you don't hide the navigation bar, the area devoted to your camera preview is probably even closer to the suported 2:1.



But if you want this to work on all devices with their different screen aspect ratios and different supported camera aspect ratios, you must crop the preview to your window, see e.g. https://github.com/waitopiggu/rn-camera-android-cropping-test.



As for choosing the best camera aspect ratio, you are right that exact match may not be acvailable; let's find the one that is closest to what we want:



const wantedRatio = height/width
var bestRatio = 0;
var bestRatioError = 100000;
for (i in ratios) {
const r = ratios[i].split(":")
if (abs(wantedRatio - r[0]/r[1]) < bestRatioError) {
bestRatioError = abs(wantedRatio - r[0]/r[1])
bestRatio = ratios[i]
}
}

this.setState({
bestRatio
})





share|improve this answer


























  • I'm sure the idea is good, but the code doesn't work. For example r is an Array and you're trying to subtract it from wantedRatio which is a number. Could you please fix it?

    – Albert Nemec
    Nov 25 '18 at 23:26











  • Oops, that's what happens wihen trying to copy/paste and clean up in the same time ;)

    – Alex Cohn
    Nov 25 '18 at 23:33



















1














Firstly get device dimension(resolution、ration) using https://facebook.github.io/react-native/docs/dimensions.



Then you could try this:



import React from 'react';
import {
Platform
} from 'react-native';
import {
RNCamera
} from 'react-native-camera';

const DESIRED_RATIO = "2:1"; //suppose the result of first step is 2:1

class MyCameraComponent extends Component {
state = {}

prepareRatio = async () => {
if (Platform.OS === 'android' && this.cam) {
const ratios = await this.cam.getSupportedRatiosAsync();

// See if the current device has your desired ratio, otherwise get the maximum supported one
// Usually the last element of "ratios" is the maximum supported ratio
const ratio = ratios.find((ratio) => ratio === DESIRED_RATIO) || ratios[ratios.length - 1];

this.setState({
ratio
});
}
}

render() {
return ( <
RNCamera ref = {
(cam) => this.cam = cam
}
onCameraReady = {
this.prepareRatio
} // You can only get the supported ratios when the camera is mounted
ratio = {
this.state.ratio
}
/>
);
}
}





share|improve this answer
























  • The step two I'm familiar with. It's the step one that's bugs me. I can write height / width function, that'd give me the ratio. But for example my screen is not exactly 2:1.. it's smthg like 2.1:1 or some hell like that.

    – Albert Nemec
    Nov 25 '18 at 14:08











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467776%2freact-native-android-get-devices-ratio%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














On Samsung S9, the screen aspect ratio of 18.5:9 is very close to 2:1. And if you don't hide the navigation bar, the area devoted to your camera preview is probably even closer to the suported 2:1.



But if you want this to work on all devices with their different screen aspect ratios and different supported camera aspect ratios, you must crop the preview to your window, see e.g. https://github.com/waitopiggu/rn-camera-android-cropping-test.



As for choosing the best camera aspect ratio, you are right that exact match may not be acvailable; let's find the one that is closest to what we want:



const wantedRatio = height/width
var bestRatio = 0;
var bestRatioError = 100000;
for (i in ratios) {
const r = ratios[i].split(":")
if (abs(wantedRatio - r[0]/r[1]) < bestRatioError) {
bestRatioError = abs(wantedRatio - r[0]/r[1])
bestRatio = ratios[i]
}
}

this.setState({
bestRatio
})





share|improve this answer


























  • I'm sure the idea is good, but the code doesn't work. For example r is an Array and you're trying to subtract it from wantedRatio which is a number. Could you please fix it?

    – Albert Nemec
    Nov 25 '18 at 23:26











  • Oops, that's what happens wihen trying to copy/paste and clean up in the same time ;)

    – Alex Cohn
    Nov 25 '18 at 23:33
















1














On Samsung S9, the screen aspect ratio of 18.5:9 is very close to 2:1. And if you don't hide the navigation bar, the area devoted to your camera preview is probably even closer to the suported 2:1.



But if you want this to work on all devices with their different screen aspect ratios and different supported camera aspect ratios, you must crop the preview to your window, see e.g. https://github.com/waitopiggu/rn-camera-android-cropping-test.



As for choosing the best camera aspect ratio, you are right that exact match may not be acvailable; let's find the one that is closest to what we want:



const wantedRatio = height/width
var bestRatio = 0;
var bestRatioError = 100000;
for (i in ratios) {
const r = ratios[i].split(":")
if (abs(wantedRatio - r[0]/r[1]) < bestRatioError) {
bestRatioError = abs(wantedRatio - r[0]/r[1])
bestRatio = ratios[i]
}
}

this.setState({
bestRatio
})





share|improve this answer


























  • I'm sure the idea is good, but the code doesn't work. For example r is an Array and you're trying to subtract it from wantedRatio which is a number. Could you please fix it?

    – Albert Nemec
    Nov 25 '18 at 23:26











  • Oops, that's what happens wihen trying to copy/paste and clean up in the same time ;)

    – Alex Cohn
    Nov 25 '18 at 23:33














1












1








1







On Samsung S9, the screen aspect ratio of 18.5:9 is very close to 2:1. And if you don't hide the navigation bar, the area devoted to your camera preview is probably even closer to the suported 2:1.



But if you want this to work on all devices with their different screen aspect ratios and different supported camera aspect ratios, you must crop the preview to your window, see e.g. https://github.com/waitopiggu/rn-camera-android-cropping-test.



As for choosing the best camera aspect ratio, you are right that exact match may not be acvailable; let's find the one that is closest to what we want:



const wantedRatio = height/width
var bestRatio = 0;
var bestRatioError = 100000;
for (i in ratios) {
const r = ratios[i].split(":")
if (abs(wantedRatio - r[0]/r[1]) < bestRatioError) {
bestRatioError = abs(wantedRatio - r[0]/r[1])
bestRatio = ratios[i]
}
}

this.setState({
bestRatio
})





share|improve this answer















On Samsung S9, the screen aspect ratio of 18.5:9 is very close to 2:1. And if you don't hide the navigation bar, the area devoted to your camera preview is probably even closer to the suported 2:1.



But if you want this to work on all devices with their different screen aspect ratios and different supported camera aspect ratios, you must crop the preview to your window, see e.g. https://github.com/waitopiggu/rn-camera-android-cropping-test.



As for choosing the best camera aspect ratio, you are right that exact match may not be acvailable; let's find the one that is closest to what we want:



const wantedRatio = height/width
var bestRatio = 0;
var bestRatioError = 100000;
for (i in ratios) {
const r = ratios[i].split(":")
if (abs(wantedRatio - r[0]/r[1]) < bestRatioError) {
bestRatioError = abs(wantedRatio - r[0]/r[1])
bestRatio = ratios[i]
}
}

this.setState({
bestRatio
})






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 23:32

























answered Nov 25 '18 at 21:17









Alex CohnAlex Cohn

41.7k553191




41.7k553191













  • I'm sure the idea is good, but the code doesn't work. For example r is an Array and you're trying to subtract it from wantedRatio which is a number. Could you please fix it?

    – Albert Nemec
    Nov 25 '18 at 23:26











  • Oops, that's what happens wihen trying to copy/paste and clean up in the same time ;)

    – Alex Cohn
    Nov 25 '18 at 23:33



















  • I'm sure the idea is good, but the code doesn't work. For example r is an Array and you're trying to subtract it from wantedRatio which is a number. Could you please fix it?

    – Albert Nemec
    Nov 25 '18 at 23:26











  • Oops, that's what happens wihen trying to copy/paste and clean up in the same time ;)

    – Alex Cohn
    Nov 25 '18 at 23:33

















I'm sure the idea is good, but the code doesn't work. For example r is an Array and you're trying to subtract it from wantedRatio which is a number. Could you please fix it?

– Albert Nemec
Nov 25 '18 at 23:26





I'm sure the idea is good, but the code doesn't work. For example r is an Array and you're trying to subtract it from wantedRatio which is a number. Could you please fix it?

– Albert Nemec
Nov 25 '18 at 23:26













Oops, that's what happens wihen trying to copy/paste and clean up in the same time ;)

– Alex Cohn
Nov 25 '18 at 23:33





Oops, that's what happens wihen trying to copy/paste and clean up in the same time ;)

– Alex Cohn
Nov 25 '18 at 23:33













1














Firstly get device dimension(resolution、ration) using https://facebook.github.io/react-native/docs/dimensions.



Then you could try this:



import React from 'react';
import {
Platform
} from 'react-native';
import {
RNCamera
} from 'react-native-camera';

const DESIRED_RATIO = "2:1"; //suppose the result of first step is 2:1

class MyCameraComponent extends Component {
state = {}

prepareRatio = async () => {
if (Platform.OS === 'android' && this.cam) {
const ratios = await this.cam.getSupportedRatiosAsync();

// See if the current device has your desired ratio, otherwise get the maximum supported one
// Usually the last element of "ratios" is the maximum supported ratio
const ratio = ratios.find((ratio) => ratio === DESIRED_RATIO) || ratios[ratios.length - 1];

this.setState({
ratio
});
}
}

render() {
return ( <
RNCamera ref = {
(cam) => this.cam = cam
}
onCameraReady = {
this.prepareRatio
} // You can only get the supported ratios when the camera is mounted
ratio = {
this.state.ratio
}
/>
);
}
}





share|improve this answer
























  • The step two I'm familiar with. It's the step one that's bugs me. I can write height / width function, that'd give me the ratio. But for example my screen is not exactly 2:1.. it's smthg like 2.1:1 or some hell like that.

    – Albert Nemec
    Nov 25 '18 at 14:08
















1














Firstly get device dimension(resolution、ration) using https://facebook.github.io/react-native/docs/dimensions.



Then you could try this:



import React from 'react';
import {
Platform
} from 'react-native';
import {
RNCamera
} from 'react-native-camera';

const DESIRED_RATIO = "2:1"; //suppose the result of first step is 2:1

class MyCameraComponent extends Component {
state = {}

prepareRatio = async () => {
if (Platform.OS === 'android' && this.cam) {
const ratios = await this.cam.getSupportedRatiosAsync();

// See if the current device has your desired ratio, otherwise get the maximum supported one
// Usually the last element of "ratios" is the maximum supported ratio
const ratio = ratios.find((ratio) => ratio === DESIRED_RATIO) || ratios[ratios.length - 1];

this.setState({
ratio
});
}
}

render() {
return ( <
RNCamera ref = {
(cam) => this.cam = cam
}
onCameraReady = {
this.prepareRatio
} // You can only get the supported ratios when the camera is mounted
ratio = {
this.state.ratio
}
/>
);
}
}





share|improve this answer
























  • The step two I'm familiar with. It's the step one that's bugs me. I can write height / width function, that'd give me the ratio. But for example my screen is not exactly 2:1.. it's smthg like 2.1:1 or some hell like that.

    – Albert Nemec
    Nov 25 '18 at 14:08














1












1








1







Firstly get device dimension(resolution、ration) using https://facebook.github.io/react-native/docs/dimensions.



Then you could try this:



import React from 'react';
import {
Platform
} from 'react-native';
import {
RNCamera
} from 'react-native-camera';

const DESIRED_RATIO = "2:1"; //suppose the result of first step is 2:1

class MyCameraComponent extends Component {
state = {}

prepareRatio = async () => {
if (Platform.OS === 'android' && this.cam) {
const ratios = await this.cam.getSupportedRatiosAsync();

// See if the current device has your desired ratio, otherwise get the maximum supported one
// Usually the last element of "ratios" is the maximum supported ratio
const ratio = ratios.find((ratio) => ratio === DESIRED_RATIO) || ratios[ratios.length - 1];

this.setState({
ratio
});
}
}

render() {
return ( <
RNCamera ref = {
(cam) => this.cam = cam
}
onCameraReady = {
this.prepareRatio
} // You can only get the supported ratios when the camera is mounted
ratio = {
this.state.ratio
}
/>
);
}
}





share|improve this answer













Firstly get device dimension(resolution、ration) using https://facebook.github.io/react-native/docs/dimensions.



Then you could try this:



import React from 'react';
import {
Platform
} from 'react-native';
import {
RNCamera
} from 'react-native-camera';

const DESIRED_RATIO = "2:1"; //suppose the result of first step is 2:1

class MyCameraComponent extends Component {
state = {}

prepareRatio = async () => {
if (Platform.OS === 'android' && this.cam) {
const ratios = await this.cam.getSupportedRatiosAsync();

// See if the current device has your desired ratio, otherwise get the maximum supported one
// Usually the last element of "ratios" is the maximum supported ratio
const ratio = ratios.find((ratio) => ratio === DESIRED_RATIO) || ratios[ratios.length - 1];

this.setState({
ratio
});
}
}

render() {
return ( <
RNCamera ref = {
(cam) => this.cam = cam
}
onCameraReady = {
this.prepareRatio
} // You can only get the supported ratios when the camera is mounted
ratio = {
this.state.ratio
}
/>
);
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 13:34









navylovernavylover

3,51031119




3,51031119













  • The step two I'm familiar with. It's the step one that's bugs me. I can write height / width function, that'd give me the ratio. But for example my screen is not exactly 2:1.. it's smthg like 2.1:1 or some hell like that.

    – Albert Nemec
    Nov 25 '18 at 14:08



















  • The step two I'm familiar with. It's the step one that's bugs me. I can write height / width function, that'd give me the ratio. But for example my screen is not exactly 2:1.. it's smthg like 2.1:1 or some hell like that.

    – Albert Nemec
    Nov 25 '18 at 14:08

















The step two I'm familiar with. It's the step one that's bugs me. I can write height / width function, that'd give me the ratio. But for example my screen is not exactly 2:1.. it's smthg like 2.1:1 or some hell like that.

– Albert Nemec
Nov 25 '18 at 14:08





The step two I'm familiar with. It's the step one that's bugs me. I can write height / width function, that'd give me the ratio. But for example my screen is not exactly 2:1.. it's smthg like 2.1:1 or some hell like that.

– Albert Nemec
Nov 25 '18 at 14:08


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467776%2freact-native-android-get-devices-ratio%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'