Socket
Socket
Sign inDemoInstall

rn-ocr-lib

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    rn-ocr-lib

React native library to perform OCR on images


Version published
Weekly downloads
12
Maintainers
1
Created
Weekly downloads
 

Readme

Source

rn-ocr-lib

React native library to perform OCR on images.

Note:

  • For now only available for Android
  • iOS version is being built

Installation

npm install rn-ocr-lib

Setup

Android

Create folder app/src/main/assets/tessdata. Inside tessdata place ${lang}.traineddata.

Usage

import React, { useEffect } from 'react';
import {
  StyleSheet,
  View,
  Button,
  useWindowDimensions,
  Text,
  NativeEventEmitter,
  NativeModules,
  ScrollView,
} from 'react-native';
import { getText, DataInputType } from 'rn-ocr-lib';

const styles = StyleSheet.create({
  container: {
    flexDirection: 'column',
    gap: 10,
  },
  result: {
    marginTop: 10,
  }
});

// replace with valid base64 string - https://base64.guru/converter/encode/image
const IMAGE_DATA: string = "data:image/png;base64,iVBORw0...";

const App = () => {
  const [res, setRes] = useState("");
  // const [progress, setProgress] = useState(0);


  const { height } = useWindowDimensions();

  const handleOCR = () => {
    getText(IMAGE_DATA, DataInputType.base64);
  }

  useEffect(() => {
    const eventEmitter = new NativeEventEmitter(NativeModules.RnOcrLib);

    eventEmitter.addListener('finished', (event) => {
      // setProgress(100);
      setText(event.text);
    });

    // add progress to percent
    // eventEmitter.addListener('progress', (event) => {
    //   setProgress(event.percent);
    // });

    return () => {
      eventEmitter.removeAllListeners('finished');
      eventEmitter.removeAllListeners('progress');
    };
  }, []);

  return (
    <View style={styles.container}>
      <Button title="Do OCR" onPress={handleOCR} />
      <Image
        style={{ height: height * 0.5 }}
        source={{
          uri: IMAGE_DATA
        }}
      />
      {!!res && <Text style={styles.result}>{res}</Text>}
    </View>
  );
};

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library

Keywords

FAQs

Last updated on 15 May 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc