Skip to content

Commit 8c16952

Browse files
committed
fix: capture with viewTag
1 parent 23f9193 commit 8c16952

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

android/src/main/java/com/documentscanner/DocumentScannerModule.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,19 @@ public String getName() {
2222
}
2323

2424
@ReactMethod
25-
public void capture(){
26-
MainView view = MainView.getInstance();
27-
view.capture();
25+
public void capture(final int viewTag) {
26+
final ReactApplicationContext context = getReactApplicationContext();
27+
UIManagerModule uiManager = context.getNativeModule(UIManagerModule.class);
28+
uiManager.addUIBlock(new UIBlock() {
29+
@Override
30+
public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
31+
try {
32+
MainView view = (MainView) nativeViewHierarchyManager.resolveView(viewTag);
33+
view.capture();
34+
} catch (Exception e) {
35+
e.printStackTrace();
36+
}
37+
}
38+
});
2839
}
2940
}

src/index.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import React from 'react'
22
import {
33
DeviceEventEmitter,
4+
findNodeHandle,
5+
NativeModules,
46
Platform,
57
requireNativeComponent,
6-
ViewStyle
7-
} from 'react-native'
8+
ViewStyle } from 'react-native'
89

910
const RNPdfScanner = requireNativeComponent('RNPdfScanner')
11+
const ScannerManager: any = NativeModules.RNPdfScannerManager
1012

1113
export interface PictureTaken {
1214
rectangleCoordinates?: object;
@@ -91,15 +93,27 @@ class PdfScanner extends React.Component<PdfScannerProps> {
9193
}
9294

9395
capture () {
94-
let native: any = this.ref.current
95-
if (native) { native.capture() }
96+
if (this._scannerHandle) {
97+
ScannerManager.capture(this._scannerHandle)
98+
}
9699
}
97100

98-
ref = React.createRef();
101+
_scannerRef: any = null;
102+
_scannerHandle: number | null = null;
103+
_setReference = (ref: any) => {
104+
if (ref) {
105+
this._scannerRef = ref
106+
this._scannerHandle = findNodeHandle(ref)
107+
} else {
108+
this._scannerRef = null
109+
this._scannerHandle = null
110+
}
111+
};
112+
99113
render () {
100114
return (
101115
<RNPdfScanner
102-
ref={this.ref}
116+
ref={this._setReference}
103117
{...this.props}
104118
onPictureTaken={this.sendOnPictureTakenEvent.bind(this)}
105119
onRectangleDetect={this.sendOnRectangleDetectEvent.bind(this)}

0 commit comments

Comments
 (0)