Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

RNCamera on Android fits rather than fills to view #1539

Closed
almost opened this issue May 3, 2018 · 8 comments
Closed

RNCamera on Android fits rather than fills to view #1539

almost opened this issue May 3, 2018 · 8 comments

Comments

@almost
Copy link
Contributor

almost commented May 3, 2018

RTCCamera used to have an option between fit and fill for the layout that would control (this aspect prop, which now means something different on RNCamera). RNCamera on Android just does fit, which means if the view isn't the same aspect as the camera then you get black bars. RNCamera on iOS seems to do the opposite which is a pain.

Ideally this would be set via a prop but if that isn't done it should at least be made consistent (my preference would be to always fill)

@almost
Copy link
Contributor Author

almost commented May 3, 2018

As a workaround I'm currently doing it in my own Javascript:

  _onLayout = ({ nativeEvent: { layout: { x, y, width, height } } }) => {
    this.setState({ width, height });
  };

  render() {
    const { width, height } = this.state;

    let style;
    // https://github.com/react-native-community/react-native-camera/issues/1539
    if (Platform.OS === "android") {
      let viewfinderHeight = 0,
        viewfinderWidth = 0,
        viewFinderPaddingX = 0,
        viewFinderPaddingY = 0;

      if (width && height) {
        const ratio = 4 / 3;
        if (ratio * height < width) {
          viewfinderHeight = width / ratio;
          viewfinderWidth = width;
        } else {
          viewfinderWidth = ratio * height;
          viewfinderHeight = height;
        }
        viewFinderPaddingX = (width - viewfinderWidth) / 2;
        viewFinderPaddingY = (height - viewfinderHeight) / 2;
      }
      style = {
        position: "absolute",
        width: viewfinderWidth,
        height: viewfinderHeight,
        left: viewFinderPaddingX,
        top: viewFinderPaddingY
      };
    } else {
      style = { flex: 1 };
    }

    const cameraStyle = width && height ? {} : null;
    return (
      <View
        style={[{ overflow: "hidden" }, this.props.style]}
        onLayout={this._onLayout}
      >
        <RNCamera
          style={style}
          type={this.props.frontCamera ? "front" : "back"}
          flashMode={this.props.flashMode}
          onCameraReady={this._cameraReady}
          ref={this._gotRef}
        />
      </View>
    );
  }

@IssueHuntBot
Copy link

@issuehuntfest has funded $20.00 to this issue. See it on IssueHunt

@stale
Copy link

stale bot commented Jun 1, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You may also mark this issue as a "discussion" and i will leave this open.

@stale stale bot added the stale There has been a lack of activity on this issue and it may be closed soon. label Jun 1, 2019
@IssueHuntBot
Copy link

@EskelCz has funded $20.00 to this issue.


@stale stale bot removed the stale There has been a lack of activity on this issue and it may be closed soon. label Jun 3, 2019
@bneigher
Copy link

@almost your solution worked for me. Times like this make me love the internet

@MateusAndrade
Copy link
Collaborator

Since the issue has been solved apllying @almost solution, im closing this issue. If anyone stills having problems with this, please give a look at: #1539 (comment)

@alessandroraffa
Copy link

Hello. Just wandering what's the purpose of

const cameraStyle

Thanks in advance

@alessandroraffa
Copy link

...And also the example value of

this.props.style

Thanks again

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants