项目作者: reime005

项目描述 :
React Hooks and API wrapper for the react-native-camera module. GitHub Actions CI/CD + Automatic Release
高级语言: JavaScript
项目地址: git://github.com/reime005/react-native-camera-hooks.git
创建时间: 2019-04-10T20:17:46Z
项目社区:https://github.com/reime005/react-native-camera-hooks

开源协议:MIT License

下载





Hooks for React Native Camera





React Native Camera Hooks provides you with functionality to use the React Native Camera API with Functional Components.

npm
GitHub
CircleCI
Deploy

  1. import { View, TouchableOpacity, TouchableWithoutFeedback } from 'react-native';
  2. import { RNCamera } from 'react-native-camera';
  3. import { useCamera } from 'react-native-camera-hooks';
  4. const FunctionalComponentExample = ({ initialProps }) => {
  5. const [
  6. { cameraRef, type, ratio, autoFocus, autoFocusPoint, isRecording },
  7. {
  8. toggleFacing,
  9. touchToFocus,
  10. textRecognized,
  11. facesDetected,
  12. recordVideo,
  13. setIsRecording,
  14. },
  15. ] = useCamera(initialProps);
  16. return (
  17. <View style={{ flex: 1 }}>
  18. <RNCamera
  19. ref={cameraRef}
  20. autoFocusPointOfInterest={autoFocusPoint.normalized}
  21. type={type}
  22. ratio={ratio}
  23. style={{ flex: 1 }}
  24. autoFocus={autoFocus}
  25. onTextRecognized={textRecognized}
  26. onFacesDetected={facesDetected}
  27. ></RNCamera>
  28. <TouchableWithoutFeedback
  29. style={{
  30. flex: 1,
  31. }}
  32. onPress={touchToFocus}
  33. ></TouchableWithoutFeedback>
  34. <TouchableOpacity
  35. testID="button"
  36. onPress={toggleFacing}
  37. style={{ width: '100%', height: 45 }}>
  38. {type}
  39. </TouchableOpacity>
  40. {!isRecording && (
  41. <TouchableOpacity
  42. onPress={async () => {
  43. try {
  44. setIsRecording(true);
  45. const data = await recordVideo();
  46. console.warn(data);
  47. } catch (error) {
  48. console.warn(error);
  49. } finally {
  50. setIsRecording(false);
  51. }
  52. }}
  53. style={{ width: '100%', height: 45 }}
  54. />
  55. )}
  56. </View>
  57. );
  58. };

Features

  • React Hooks Support: Use React Native Camera with Functional Components
  • Wrapper around the Camera API, that makes the usage easier
  • TypeScript support

Constants are defined in constants and initalState.

Function Description
useCamera(initialState) Includes all camera hooks described below. See also the example above
useZoom(state) Zoom feature. Includes zoom, setZoom, zoomIn (increment by 0.01) and zoomOut (decrement by 0.1)
useToggleFacing(state, modes) Toggles between two values (front and back side of the camera). Includes type, toggleFacing.
useAutoFocus(state, modes) Toggles between two values (focus on or off). Includes autoFocus and toggleAutoFocus.
useWhiteBalance(state) Toggles between white balance values. Includes whiteBalance, toggleWB and setWhiteBalance.
useFlash(state) Toggles between flash modes. Includes flash, toggleFlash and setFlash.
useAutoFocusTouch(state) Touch to focus feature. Includes autoFocusPoint, touchToFocus (callback to be used in onPress for example) and setAutoFocusPoint.
useTextRecognition(state) Text recognition feature. Includes textBlocks, setTextblocks and textRecognized (callback).
useFaceDetection(state) Face detection feature. Includes faces, setFaces and facesDetected (callback).
useBarcodeDetection(state) Barcode detection feature. Includes barcodes, setBarcodes and barcodeRecognized (callback).
takePicture({ cameraRef }, options) Function to take a picture. Returns a Promise with the result. defaultPictureTakeOptions can also be imported from the same file.
recordVideo({ cameraRef }, options) Function to record a video. Returns a Promise with the result. defaultVideoRecordOptions can also be imported from the same file.
stopRecording({ cameraRef }) Function to stop recording. Returns a Promise.
pausePreview({ cameraRef }) Function to pause the camera preview. Returns a Promise with the result as a boolean.
resumePreview({ cameraRef }) Function to resume the camera preview. Returns a Promise with the result as a boolean.


Installation

To install react-native-camera-hooks, do either

  1. npm install --save react-native-camera-hooks

or

  1. yarn add react-native-camera-hooks

Note that this requires a react-native version > 0.59 which supports React Hooks. Also, react-native-camera has to be installed.


TODO

  • Improve TypeScript support