UNPKG

react-native-vision-camera-ocr-plus

Version:

React Native Vision Camera plugin for on-device text recognition (OCR) and translation using ML Kit. Maintained fork of react-native-vision-camera-text-recognition

250 lines (185 loc) โ€ข 7.28 kB
# ๐Ÿ“ท react-native-vision-camera-ocr-plus [![CI Status](https://github.com/jamenamcinteer/react-native-vision-camera-ocr-plus/actions/workflows/ci.yml/badge.svg)](https://github.com/jamenamcinteer/react-native-vision-camera-ocr-plus/actions/workflows/ci.yml) [![npm version](https://img.shields.io/npm/v/react-native-vision-camera-ocr-plus.svg)](https://www.npmjs.com/package/react-native-vision-camera-ocr-plus) A **React Native Vision Camera** frame processor for **on-device text recognition (OCR)** and **translation** using **ML Kit**. โœจ Actively maintained fork of [`react-native-vision-camera-text-recognition`](https://www.npmjs.com/package/react-native-vision-camera-text-recognition), with modern improvements, bug fixes, and support for the latest Vision Camera and React Native versions. --- ## ๐ŸŒŸ Why Use This Fork? The original packages are **no longer actively maintained**. This fork provides: - โœ… Ongoing maintenance and compatibility with **React Native 0.76+** and **Vision Camera v4+** - ๐Ÿง  **Translation support** (not just OCR) powered by ML Kit - ๐Ÿ›  **Improved stability and error handling** - ๐Ÿš€ **Faster processing** and frame optimization - ๐Ÿ“ฆ **TypeScript definitions** included - ๐Ÿงฉ Consistent API that works seamlessly with modern React Native projects --- ## ๐Ÿš€ Features - ๐Ÿงฉ Simple drop-in API - โšก Fast, accurate on-device OCR - ๐Ÿ“ฑ Works on **Android** and **iOS** - ๐ŸŒ Built-in translation via ML Kit - ๐Ÿ“ธ Recognize text from live camera or static photos - ๐Ÿช„ Written in **Kotlin** and **Swift** - ๐Ÿ”ง Compatible with `react-native-vision-camera` and `react-native-worklets-core` - ๐Ÿ”ฅ Compatible with Firebase --- ## ๐Ÿ’ป Installation > **Peer dependencies:** > You must have `react-native-vision-camera` and `react-native-worklets-core` installed. ```bash npm install react-native-vision-camera-ocr-plus # or yarn add react-native-vision-camera-ocr-plus ``` ### ๐Ÿ”ฅ Firebase Compatibility If you have Firebase in your project, you will need to set your iOS Deployment Target to at least 16.0. --- ## ๐Ÿ”„ Migration | Previous Package | Replacement | Notes | |------------------|-------------|-------| | `react-native-vision-camera-text-recognition` | โœ… `react-native-vision-camera-ocr-plus` | Drop-in replacement with fixes and updates | | `vision-camera-ocr` | โœ… `react-native-vision-camera-ocr-plus` | Actively maintained alternative | --- ## ๐Ÿ’ก Usage ๐Ÿ‘‰ See the [example app](https://github.com/jamenamcinteer/react-native-vision-camera-ocr-plus/tree/main/example) for a working demo. ### ๐Ÿ“š Live Text Recognition ```jsx import React, { useState } from 'react'; import { StyleSheet } from 'react-native'; import { useCameraDevice } from 'react-native-vision-camera'; import { Camera } from 'react-native-vision-camera-ocr-plus'; export default function App() { const [data, setData] = useState(null); const device = useCameraDevice('back'); return ( <> {!!device && ( <Camera style={StyleSheet.absoluteFill} device={device} isActive mode="recognize" options={{ language: 'latin' }} callback={(result) => setData(result)} /> )} </> ); } ``` --- ### ๐ŸŒ Translate Text in Real Time ```jsx import React, { useState } from 'react'; import { StyleSheet } from 'react-native'; import { useCameraDevice } from 'react-native-vision-camera'; import { Camera } from 'react-native-vision-camera-ocr-plus'; export default function App() { const [data, setData] = useState(null); const device = useCameraDevice('back'); return ( <> {!!device && ( <Camera style={StyleSheet.absoluteFill} device={device} isActive mode="translate" options={{ from: 'en', to: 'de' }} callback={(result) => setData(result)} /> )} </> ); } ``` --- ### โš™๏ธ Using a Frame Processor ```jsx import React from 'react'; import { StyleSheet } from 'react-native'; import { Camera, useCameraDevice, useFrameProcessor } from 'react-native-vision-camera'; import { useTextRecognition } from 'react-native-vision-camera-ocr-plus'; export default function App() { const device = useCameraDevice('back'); const { scanText } = useTextRecognition({ language: 'latin' }); const frameProcessor = useFrameProcessor((frame) => { 'worklet'; const data = scanText(frame); console.log('Detected text:', data); }, []); return ( <> {!!device && ( <Camera style={StyleSheet.absoluteFill} device={device} isActive frameProcessor={frameProcessor} mode="recognize" /> )} </> ); } ``` --- ## โš™๏ธ Options | Option | Type | Values | Default | |:-------|:-----|:--------|:---------| | `language` | `string` | `latin`, `chinese`, `devanagari`, `japanese`, `korean` | `latin` | | `mode` | `string` | `recognize`, `translate` | `recognize` | | `from`, `to` | `string` | See [Supported Languages](#-supported-languages) | `en`, `de` | --- ## ๐Ÿ–ผ Recognize Text from a Photo ```js import { PhotoRecognizer } from 'react-native-vision-camera-ocr-plus'; const result = await PhotoRecognizer({ uri: asset.uri, orientation: 'portrait', }); console.log(result); ``` > โš ๏ธ **Note (iOS only):** > The `orientation` option is available only on iOS and is recommended when using photos captured via the camera. | Property | Type | Values | Required | Default | Platform | |:----------|:------|:--------|:----------|:----------|:-----------| | `uri` | `string` | โ€” | โœ… Yes | โ€” | Android, iOS | | `orientation` | `string` | `portrait`, `portraitUpsideDown`, `landscapeLeft`, `landscapeRight` | โŒ No | `portrait` | iOS only | --- ## ๐Ÿงน Remove Unused Translation Models ```js import { RemoveLanguageModel } from 'react-native-vision-camera-ocr-plus'; await RemoveLanguageModel('en'); ``` --- ## ๐ŸŒ Supported Languages | Language | Code | Flag | |:----------|:------|:------| | Afrikaans | `af` | ๐Ÿ‡ฟ๐Ÿ‡ฆ | | Arabic | `ar` | ๐Ÿ‡ธ๐Ÿ‡ฆ | | Bengali | `bn` | ๐Ÿ‡ง๐Ÿ‡ฉ | | Chinese | `zh` | ๐Ÿ‡จ๐Ÿ‡ณ | | English | `en` | ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ‡ฌ๐Ÿ‡ง | | French | `fr` | ๐Ÿ‡ซ๐Ÿ‡ท | | German | `de` | ๐Ÿ‡ฉ๐Ÿ‡ช | | Hindi | `hi` | ๐Ÿ‡ฎ๐Ÿ‡ณ | | Japanese | `ja` | ๐Ÿ‡ฏ๐Ÿ‡ต | | Korean | `ko` | ๐Ÿ‡ฐ๐Ÿ‡ท | | Portuguese | `pt` | ๐Ÿ‡ต๐Ÿ‡น | | Russian | `ru` | ๐Ÿ‡ท๐Ÿ‡บ | | Spanish | `es` | ๐Ÿ‡ช๐Ÿ‡ธ | | ...and [many more](https://github.com/jamenamcinteer/react-native-vision-camera-ocr-plus/blob/next-release/src/types.ts). | --- ## ๐Ÿง  Contributing Contributions, feature requests, and bug reports are always welcome! Please open an [issue](https://github.com/jamenamcinteer/react-native-vision-camera-ocr-plus/issues) or [pull request](https://github.com/jamenamcinteer/react-native-vision-camera-ocr-plus/pulls). --- ## โ˜• Support the Project If this library helps you build awesome apps, consider supporting future maintenance and development ๐Ÿ’› - [๐Ÿ’– Sponsor on GitHub](https://github.com/sponsors/jamenamcinteer) Your support helps keep the package updated and open source โค๏ธ --- ## ๐Ÿ“„ License MIT ยฉ [Jamena McInteer](https://github.com/jamenamcinteer)