MitrahSoft is an Adobe offcial partner MitrahSoft is Lucee offcial partner & core contributing developer

Solution For ReactNative Barcode Scanning Mobile App Orientation Problems

React Native is a JavaScript framework for writing real, natively rendering mobile applications for iOS and Android.Similar to React for the Web, React Native applications are written using a mixture of JavaScript and XML-esque markup, known as JSX. Then, under the hood, the React Native "bridge" invokes the native rendering APIs in Objective-C (for iOS) or Java (for Android). Thus, your application will render using real mobile UI components, not webviews, and will look and feel like any other mobile application. React Native also exposes JavaScript interfaces for platform APIs, so your React Native apps can access platform features like the phone camera, or the user's location.

To create a react-native application:
  • Run the following command in command prompt
  • react-native init myApp

Smartphone Cameras:

Now a days smartphone cameras are not only used for snapping photos or recording videos, but also for many purposes like Scanning Documents and Barcodes, Translating Text from one language to another, Augmented Reality Entertainment, etc... The list does not end here and it gets bigger day by day. So use of camera has become very vital these days.

We may also need to implement camera in our native mobile applications. By default, our smartphone manufacturers lock the orientation of the default camera, but the icons used alone will change according to orientation. But the package in react-native does not lock the orientation by default, which may cause the app unstable often. So we need to lock the orientation and we can change the icons alone according to orientation.

In order to use camera in our app we need to install any camera package. One of the most famous and widely used & stable package is react-native-camera.

Installation:
  • npm install react-native-camera --save
  • react-native link react-native-camera
Orientation:

We need a separate package which allows us to lock the orientation of our app & one of the most famous and most used & stable package for it is react-native-orientation-locker. One more advantage of this package is, we could lock the orientation of particular page or component alone.

Real-time Usage:
  • There might be need to lock the orientation of a single page alone.
  • We might need to design UI inside the camera component but do not want the responsiveness to be spoiled.
Installation:
  • npm install react-native-orientation-locker --save
  • react-native link react-native-orientation-locker
Configuration:

Please refer configuration for ios and android configuration.

Implementation:

We need to import the package in order to use the functions available in that package, so import the camera and orientation package.

import Orientation from "react-native-orientation-locker"; import { RNCamera } from 'react-native-camera';

After importing the initial step which needs to be done is, locking the orientation of the particular page or component to Portrait. There are few built-in functions in react-native-orientation-locker.

  • lockToPortrait()
  • lockToLandscape()
  • lockToLandscapeLeft()
  • lockToLandscapeRight()

We could use any of these functions based on our requirement, here we need to lock the screen in Portrait, so we will stick to lockToPortrait(). This must be invoked in constructor or componentWillMount as it should be triggered before rendering. As these functions come with react-native-orientation-locker, we need to invoke using the object in which we had imported the package(Orientation).

Next, Create orientation events to find the device orientation. Create the listener in either constructor or componentWillMount, as it should be initialized before the component is rendered.

Listener:

When device orientation is changed, callback function will be called. When lockToXXX is called, callback function also can be called. It can return either

  • PORTRAIT
  • LANDSCAPE-LEFT
  • LANDSCAPE-RIGHT
  • PORTRAIT-UPSIDEDOWN
  • UNKNOWN

Do not forget to remove the event listener and unlock the orientation when the component is removed. Remove it in componentWillUnmount as it will be invoked when the component is being removed.

Rendering the camera:
Rendering capture and flash buttons based on device's orientation in the camera: