|
1 | 1 | # react-native-live-detect-edges |
2 | 2 |
|
3 | | -Camera Live Detect Edges |
| 3 | +📸 **React Native Live Document Edge Detection** |
4 | 4 |
|
5 | | -## Installation |
| 5 | +A high-performance, real-time document edge detection and crop library for React Native (iOS & Android). |
6 | 6 |
|
| 7 | +--- |
| 8 | + |
| 9 | +## 📱 Demo |
| 10 | + |
| 11 | +| iOS (Live Detection) | Android (Live Detection) | |
| 12 | +| :--------------------------------------: | :------------------------------------------: | |
| 13 | +| <img src="screenshots/video1-ios.gif" /> | <img src="screenshots/video1-android.gif" /> | |
| 14 | + |
| 15 | +| iOS (Manual Crop) | Android (Manual Crop) | |
| 16 | +| :--------------------------------------: | :------------------------------------------: | |
| 17 | +| <img src="screenshots/video2-ios.gif" /> | <img src="screenshots/video2-android.gif" /> | |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## 🚀 Features |
| 22 | + |
| 23 | +- **Real-time Edge Detection**: Detects document boundaries instantly from the camera feed. |
| 24 | +- **Cross-Platform**: Fully supported on **iOS** and **Android**. |
| 25 | +- **High Quality Capture**: Captures high-resolution images of the document. |
| 26 | +- **Perspective Correction**: Automatically crops and warps the detected document to a flat rectangle. |
| 27 | +- **Customizable UI**: Customize the overlay color, stroke width, and more. |
| 28 | +- **TypeScript Support**: First-class TypeScript definitions included. |
| 29 | + |
| 30 | +## 📦 Installation |
7 | 31 |
|
8 | 32 | ```sh |
9 | 33 | npm install react-native-live-detect-edges |
| 34 | +# or |
| 35 | +yarn add react-native-live-detect-edges |
10 | 36 | ``` |
11 | 37 |
|
| 38 | +### iOS Setup |
| 39 | + |
| 40 | +Don't forget to install the pods: |
| 41 | + |
| 42 | +```sh |
| 43 | +cd ios && pod install |
| 44 | +``` |
12 | 45 |
|
13 | | -## Usage |
| 46 | +Add the following key to your `Info.plist` to request camera permission: |
| 47 | + |
| 48 | +```xml |
| 49 | +<key>NSCameraUsageDescription</key> |
| 50 | +<string>We need access to your camera to scan documents.</string> |
| 51 | +``` |
| 52 | + |
| 53 | +### Android Setup |
| 54 | + |
| 55 | +Ensure you have the camera permission in `AndroidManifest.xml` (the library adds it, but your app must request it at runtime): |
| 56 | + |
| 57 | +```xml |
| 58 | +<uses-permission android:name="android.permission.CAMERA" /> |
| 59 | +``` |
| 60 | + |
| 61 | +## 💻 Usage |
| 62 | + |
| 63 | +### 1. The Scanner View (`LiveDetectEdgesView`) |
| 64 | + |
| 65 | +This component renders the camera preview with the live edge detection overlay. |
| 66 | + |
| 67 | +```tsx |
| 68 | +import { LiveDetectEdgesView } from 'react-native-live-detect-edges'; |
| 69 | + |
| 70 | +<LiveDetectEdgesView |
| 71 | + style={{ flex: 1 }} |
| 72 | + overlayColor="rgba(0, 255, 0, 0.5)" |
| 73 | + overlayStrokeWidth={4} |
| 74 | +/>; |
| 75 | +``` |
14 | 76 |
|
| 77 | +### 2. Capturing and Cropping (`takePhoto`) |
15 | 78 |
|
16 | | -```js |
17 | | -import { LiveDetectEdgesView } from "react-native-live-detect-edges"; |
| 79 | +Use the `takePhoto` method to capture the current document. It returns the full image and the detected corner points. |
18 | 80 |
|
19 | | -// ... |
| 81 | +```tsx |
| 82 | +import { takePhoto } from 'react-native-live-detect-edges'; |
20 | 83 |
|
21 | | -<LiveDetectEdgesView color="tomato" /> |
| 84 | +const handleCapture = async () => { |
| 85 | + try { |
| 86 | + const result = await takePhoto(); |
| 87 | + console.log('Original Image:', result.originalImage.uri); |
| 88 | + console.log('Cropped Image:', result.image.uri); // Auto-cropped if detected |
| 89 | + } catch (error) { |
| 90 | + console.error('Capture failed:', error); |
| 91 | + } |
| 92 | +}; |
22 | 93 | ``` |
23 | 94 |
|
| 95 | +### 3. Manual Cropping (`cropImage`) |
| 96 | + |
| 97 | +If you want to manually adjust the crop points (e.g., in a custom UI), use `cropImage`. |
| 98 | + |
| 99 | +```tsx |
| 100 | +import { cropImage } from 'react-native-live-detect-edges'; |
| 101 | + |
| 102 | +const result = await cropImage({ |
| 103 | + imageUri: 'file:///path/to/image.jpg', |
| 104 | + quad: { |
| 105 | + topLeft: { x: 100, y: 100 }, |
| 106 | + topRight: { x: 400, y: 100 }, |
| 107 | + bottomRight: { x: 400, y: 500 }, |
| 108 | + bottomLeft: { x: 100, y: 500 }, |
| 109 | + }, |
| 110 | +}); |
| 111 | + |
| 112 | +console.log('New Cropped URI:', result.uri); |
| 113 | +``` |
| 114 | + |
| 115 | +### 4. Advanced Usage: Drag & Drop Crop UI |
| 116 | + |
| 117 | +To obtain the `quad` (Quadrilateral) coordinates for manual cropping, you typically need a UI that allows users to drag corner points over the image. |
| 118 | + |
| 119 | +We provide a full example of such a UI using `react-native-gesture-handler` and `react-native-reanimated` in the example app. |
| 120 | + |
| 121 | +👉 **Check out the implementation here:** [example/src/screens/CropScreen.tsx](example/src/screens/CropScreen.tsx) |
| 122 | + |
| 123 | +## 🧩 API Reference |
| 124 | + |
| 125 | +### `<LiveDetectEdgesView />` Props |
| 126 | + |
| 127 | +| Prop | Type | Default | Description | |
| 128 | +| -------------------- | ----------- | ------------------------ | --------------------------------------------------------------------------------------------------------------- | |
| 129 | +| `overlayColor` | `string` | `"rgba(0, 255, 0, 0.5)"` | The color of the edge detection overlay (stroke). | |
| 130 | +| `overlayFillColor` | `string` | `undefined` | The fill color of the detected area. Defaults to a semi-transparent version of `overlayColor` if not specified. | |
| 131 | +| `overlayStrokeWidth` | `number` | `4` | The thickness of the detection lines. | |
| 132 | +| `style` | `ViewStyle` | - | Standard React Native style prop. | |
| 133 | + |
| 134 | +### Methods |
| 135 | + |
| 136 | +#### `takePhoto(): Promise<TakePhotoResult>` |
| 137 | + |
| 138 | +Captures the current frame, attempts to detect edges, and saves both the original and a cropped version. |
| 139 | + |
| 140 | +**Returns:** |
| 141 | + |
| 142 | +- `originalImage`: `{ uri, width, height }` |
| 143 | +- `image`: `{ uri, width, height }` (The auto-cropped result) |
| 144 | +- `detectedPoints`: `Quadrilateral` (Corner points in image coordinates) |
| 145 | + |
| 146 | +#### `cropImage(params): Promise<CropImageResult>` |
| 147 | + |
| 148 | +Performs a perspective transform on an image given specific corner points. |
| 149 | + |
| 150 | +**Params:** |
| 151 | + |
| 152 | +- `imageUri`: `string` (Local file path) |
| 153 | +- `quad`: `Quadrilateral` (`topLeft`, `topRight`, `bottomRight`, `bottomLeft`) |
| 154 | + |
| 155 | +## 🤝 Contributing |
| 156 | + |
| 157 | +See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow. |
| 158 | + |
| 159 | +## 👏 Acknowledgements |
24 | 160 |
|
25 | | -## Contributing |
| 161 | +Special thanks to the authors of these amazing libraries: |
26 | 162 |
|
27 | | -- [Development workflow](CONTRIBUTING.md#development-workflow) |
28 | | -- [Sending a pull request](CONTRIBUTING.md#sending-a-pull-request) |
29 | | -- [Code of conduct](CODE_OF_CONDUCT.md) |
| 163 | +- **Android**: Big thanks to [pynicolas](https://github.com/pynicolas) for [FairScan](https://github.com/pynicolas/FairScan). This library provided the core inspiration and logic for the Android implementation. |
| 164 | +- **iOS**: Huge shoutout to the team at [WeTransfer](https://github.com/WeTransfer) and all contributors of [WeScan](https://github.com/WeTransferArchive/WeScan) for building such a robust foundation. |
30 | 165 |
|
31 | | -## License |
| 166 | +## �📄 License |
32 | 167 |
|
33 | 168 | MIT |
34 | 169 |
|
|
0 commit comments