Skip to content

Commit e5f59b6

Browse files
Zain SajjadZain Sajjad
Zain Sajjad
authored and
Zain Sajjad
committed
Android Integration
1 parent b0f9017 commit e5f59b6

File tree

14 files changed

+594
-0
lines changed

14 files changed

+594
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pbxproj -text

.gitignore

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
# OSX
3+
#
4+
.DS_Store
5+
6+
# node.js
7+
#
8+
node_modules/
9+
npm-debug.log
10+
yarn-error.log
11+
12+
13+
# Xcode
14+
#
15+
build/
16+
*.pbxuser
17+
!default.pbxuser
18+
*.mode1v3
19+
!default.mode1v3
20+
*.mode2v3
21+
!default.mode2v3
22+
*.perspectivev3
23+
!default.perspectivev3
24+
xcuserdata
25+
*.xccheckout
26+
*.moved-aside
27+
DerivedData
28+
*.hmap
29+
*.ipa
30+
*.xcuserstate
31+
project.xcworkspace
32+
33+
34+
# Android/IntelliJ
35+
#
36+
build/
37+
.idea
38+
.gradle
39+
local.properties
40+
*.iml
41+
42+
# BUCK
43+
buck-out/
44+
\.buckd/
45+
*.keystore
46+

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# react-native-text-detector
2+
3+
## Getting started
4+
5+
`$ npm install react-native-text-detector --save`
6+
7+
### Mostly automatic installation
8+
9+
`$ react-native link react-native-text-detector`
10+
11+
### Manual installation
12+
13+
#### iOS
14+
15+
1. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
16+
2. Go to `node_modules``react-native-text-detector` and add `RNTextDetector.xcodeproj`
17+
3. In XCode, in the project navigator, select your project. Add `libRNTextDetector.a` to your project's `Build Phases``Link Binary With Libraries`
18+
4. Run your project (`Cmd+R`)<
19+
20+
#### Android
21+
22+
1. Open up `android/app/src/main/java/[...]/MainActivity.java`
23+
24+
- Add `import com.fetchsky.RNTextDetector.RNTextDetectorPackage;` to the imports at the top of the file
25+
- Add `new RNTextDetectorPackage()` to the list returned by the `getPackages()` method
26+
27+
2. Append the following lines to `android/settings.gradle`:
28+
```
29+
include ':react-native-text-detector'
30+
project(':react-native-text-detector').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-text-detector/android')
31+
```
32+
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
33+
```
34+
compile project(':react-native-text-detector')
35+
```
36+
37+
## Usage
38+
39+
```javascript
40+
import RNTextDetector from "react-native-text-detector";
41+
42+
// TODO: What to do with the module?
43+
RNTextDetector;
44+
```

android/build.gradle

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
buildscript {
3+
repositories {
4+
jcenter()
5+
}
6+
7+
dependencies {
8+
classpath 'com.android.tools.build:gradle:1.3.1'
9+
}
10+
}
11+
12+
apply plugin: 'com.android.library'
13+
14+
android {
15+
compileSdkVersion 27
16+
buildToolsVersion "23.0.1"
17+
18+
defaultConfig {
19+
minSdkVersion 16
20+
targetSdkVersion 27
21+
versionCode 1
22+
versionName "1.0"
23+
}
24+
lintOptions {
25+
abortOnError false
26+
}
27+
}
28+
29+
repositories {
30+
mavenCentral()
31+
}
32+
33+
dependencies {
34+
implementation 'com.google.firebase:firebase-core:+'
35+
implementation 'com.google.firebase:firebase-ml-vision:+'
36+
compile 'com.facebook.react:react-native:+'
37+
}
38+

android/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.fetchsky.RNTextDetector">
4+
5+
</manifest>
6+
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
2+
package com.fetchsky.RNTextDetector;
3+
4+
import android.graphics.Rect;
5+
import android.support.annotation.NonNull;
6+
import android.util.Log;
7+
8+
import com.facebook.react.bridge.Arguments;
9+
import com.facebook.react.bridge.Promise;
10+
import com.facebook.react.bridge.ReactApplicationContext;
11+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
12+
import com.facebook.react.bridge.ReactMethod;
13+
import com.facebook.react.bridge.WritableArray;
14+
import com.facebook.react.bridge.WritableMap;
15+
16+
import com.google.android.gms.tasks.OnFailureListener;
17+
import com.google.android.gms.tasks.OnSuccessListener;
18+
import com.google.android.gms.tasks.Task;
19+
import com.google.firebase.ml.vision.FirebaseVision;
20+
import com.google.firebase.ml.vision.common.FirebaseVisionImage;
21+
import com.google.firebase.ml.vision.text.FirebaseVisionText;
22+
import com.google.firebase.ml.vision.text.FirebaseVisionTextDetector;
23+
24+
import java.io.IOException;
25+
26+
public class RNTextDetectorModule extends ReactContextBaseJavaModule {
27+
28+
private final ReactApplicationContext reactContext;
29+
30+
public RNTextDetectorModule(ReactApplicationContext reactContext) {
31+
super(reactContext);
32+
this.reactContext = reactContext;
33+
}
34+
35+
@ReactMethod
36+
public void textRecognize(String uri, final Promise promise) {
37+
try {
38+
image = FirebaseVisionImage.fromFilePath(this.reactContext, android.net.Uri.parse(uri));
39+
Task<FirebaseVisionText> result =
40+
detector.detectInImage(image)
41+
.addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
42+
@Override
43+
public void onSuccess(FirebaseVisionText firebaseVisionText) {
44+
promise.resolve(getDataAsArray(firebaseVisionText));
45+
}
46+
})
47+
.addOnFailureListener(
48+
new OnFailureListener() {
49+
@Override
50+
public void onFailure(@NonNull Exception e) {
51+
e.printStackTrace();
52+
promise.reject(e);
53+
}
54+
});;
55+
} catch (IOException e) {
56+
promise.reject(e);
57+
e.printStackTrace();
58+
}
59+
}
60+
61+
/**
62+
* Converts firebaseVisionText into a map
63+
*
64+
* @param firebaseVisionText
65+
* @return
66+
*/
67+
private WritableArray getDataAsArray(FirebaseVisionText firebaseVisionText) {
68+
WritableArray data = Arguments.createArray();
69+
WritableMap info = Arguments.createMap();
70+
WritableMap coordinates = Arguments.createMap();
71+
72+
for (FirebaseVisionText.Block block: firebaseVisionText.getBlocks()) {
73+
info = Arguments.createMap();
74+
coordinates = Arguments.createMap();
75+
76+
Rect boundingBox = block.getBoundingBox();
77+
78+
coordinates.putInt("top", boundingBox.top);
79+
coordinates.putInt("left", boundingBox.left);
80+
coordinates.putInt("width", boundingBox.width());
81+
coordinates.putInt("height", boundingBox.height());
82+
83+
info.putMap("bounding", coordinates);
84+
info.putString("text", block.getText());
85+
data.pushMap(info);
86+
}
87+
88+
return data;
89+
}
90+
91+
92+
@Override
93+
public String getName() {
94+
return "RNTextDetector";
95+
}
96+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
package com.fetchsky.RNTextDetector;
3+
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
import com.facebook.react.ReactPackage;
9+
import com.facebook.react.bridge.NativeModule;
10+
import com.facebook.react.bridge.ReactApplicationContext;
11+
import com.facebook.react.uimanager.ViewManager;
12+
import com.facebook.react.bridge.JavaScriptModule;
13+
14+
public class RNTextDetectorPackage implements ReactPackage {
15+
@Override
16+
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
17+
return Arrays.<NativeModule>asList(new RNTextDetectorModule(reactContext));
18+
}
19+
20+
// Deprecated from RN 0.47
21+
public List<Class<? extends JavaScriptModule>> createJSModules() {
22+
return Collections.emptyList();
23+
}
24+
25+
@Override
26+
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
27+
return Collections.emptyList();
28+
}
29+
}

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
import { NativeModules } from 'react-native';
3+
4+
const { RNTextDetector } = NativeModules;
5+
6+
export default RNTextDetector;

ios/RNTextDetector.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
#if __has_include("RCTBridgeModule.h")
3+
#import "RCTBridgeModule.h"
4+
#else
5+
#import <React/RCTBridgeModule.h>
6+
#endif
7+
8+
@interface RNTextDetector : NSObject <RCTBridgeModule>
9+
10+
@end
11+

ios/RNTextDetector.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
#import "RNTextDetector.h"
3+
4+
@implementation RNTextDetector
5+
6+
- (dispatch_queue_t)methodQueue
7+
{
8+
return dispatch_get_main_queue();
9+
}
10+
RCT_EXPORT_MODULE()
11+
12+
@end
13+

ios/RNTextDetector.podspec

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
2+
3+
Pod::Spec.new do |s|
4+
s.name = "RNTextDetector"
5+
s.version = package['version']
6+
s.summary = package['description']
7+
s.description = package['description']
8+
s.license = package['license']
9+
s.author = package['author']
10+
s.homepage = package['homepage']
11+
s.platform = :ios, "7.0"
12+
s.source = { :git => "https://github.com/zsajjad/RNTextDetector.git", :tag => "master" }
13+
s.source_files = "*.{h,m}"
14+
s.requires_arc = true
15+
16+
s.dependency "React"
17+
end
18+
19+

0 commit comments

Comments
 (0)