项目作者: Michaelvilleneuve

项目描述 :
Perform custom crop, resizing and perspective correction 📐🖼
高级语言: Java
项目地址: git://github.com/Michaelvilleneuve/react-native-perspective-image-cropper.git


React Native perspective image cropper 📐🖼

A component that allows you to perform custom image crop and perspective correction !

Demo image

Designed to work with React Native Document Scanner

https://github.com/Michaelvilleneuve/react-native-document-scanner

Demo gif

Installation 🚀🚀

$ npm install react-native-perspective-image-cropper --save

$ react-native link react-native-perspective-image-cropper

This library uses react-native-svg, you must install it too. See https://github.com/react-native-community/react-native-svg for more infos.

Android Only

If you do not already have openCV installed in your project, add this line to your settings.gradle

  1. include ':openCVLibrary310'
  2. project(':openCVLibrary310').projectDir = new File(rootProject.projectDir,'../node_modules/react-native-perspective-image-cropper/android/openCVLibrary310')

Crop image

  • First get component ref
  1. <CustomCrop ref={ref => (this.customCrop = ref)} />
  • Then call :
  1. this.customCrop.crop();

Props

Props Type Required Description
updateImage Func Yes Returns the cropped image and the coordinates of the cropped image in the initial photo
rectangleCoordinates Object see usage No Object to predefine an area to crop (an already detected image for example)
initialImage String Yes Base64 encoded image you want to be cropped
height Number Yes Height of the image (will probably disappear in the future
width Number Yes Width of the image (will probably disappear in the future
overlayColor String No Color of the cropping area overlay
overlayStrokeColor String No Color of the cropping area stroke
overlayStrokeWidth Number No Width of the cropping area stroke
handlerColor String No Color of the handlers
enablePanStrict Bool No Enable pan on X axis, and Y axis

Usage

  1. import CustomCrop from "react-native-perspective-image-cropper";
  2. class CropView extends Component {
  3. componentWillMount() {
  4. Image.getSize(image, (width, height) => {
  5. this.setState({
  6. imageWidth: width,
  7. imageHeight: height,
  8. initialImage: image,
  9. rectangleCoordinates: {
  10. topLeft: { x: 10, y: 10 },
  11. topRight: { x: 10, y: 10 },
  12. bottomRight: { x: 10, y: 10 },
  13. bottomLeft: { x: 10, y: 10 }
  14. }
  15. });
  16. });
  17. }
  18. updateImage(image, newCoordinates) {
  19. this.setState({
  20. image,
  21. rectangleCoordinates: newCoordinates
  22. });
  23. }
  24. crop() {
  25. this.customCrop.crop();
  26. }
  27. render() {
  28. return (
  29. <View>
  30. <CustomCrop
  31. updateImage={this.updateImage.bind(this)}
  32. rectangleCoordinates={this.state.rectangleCoordinates}
  33. initialImage={this.state.initialImage}
  34. height={this.state.imageHeight}
  35. width={this.state.imageWidth}
  36. ref={ref => (this.customCrop = ref)}
  37. overlayColor="rgba(18,190,210, 1)"
  38. overlayStrokeColor="rgba(20,190,210, 1)"
  39. handlerColor="rgba(20,150,160, 1)"
  40. enablePanStrict={false}
  41. />
  42. <TouchableOpacity onPress={this.crop.bind(this)}>
  43. <Text>CROP IMAGE</Text>
  44. </TouchableOpacity>
  45. </View>
  46. );
  47. }
  48. }