Skip to content

Commit 5ee81c5

Browse files
committed
feat(android): add saveOnDevice prop
1 parent d8049ea commit 5ee81c5

File tree

4 files changed

+54
-31
lines changed

4 files changed

+54
-31
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public void setOverlayColor(MainView view, String rgbaColor) {
6565
view.setOverlayColor(rgbaColor);
6666
}
6767

68+
@ReactProp(name = "saveOnDevice", defaultBoolean = false)
69+
public void setSaveOnDevice(MainView view, Boolean saveOnDevice) {
70+
view.setSaveOnDevice(saveOnDevice);
71+
}
72+
6873
@ReactProp(name = "detectionCountBeforeCapture", defaultInt = 15)
6974
public void setDetectionCountBeforeCapture(MainView view, int numberOfRectangles) {
7075
view.setDetectionCountBeforeCapture(numberOfRectangles);

android/src/main/java/com/documentscanner/views/MainView.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public void setOverlayColor(String rgbaColor) {
7676
view.setOverlayColor(rgbaColor);
7777
}
7878

79+
public void setSaveOnDevice(Boolean saveOnDevice) {
80+
view.setSaveOnDevice(saveOnDevice);
81+
}
82+
7983
public void setBrightness(double brightness) {
8084
view.setBrightness(brightness);
8185
}

android/src/main/java/com/documentscanner/views/OpenNoteCameraView.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public class OpenNoteCameraView extends JavaCameraView implements PictureCallbac
117117
private int numberOfRectangles = 15;
118118
private Boolean enableTorch = false;
119119
public String overlayColor = null;
120+
public Boolean saveOnDevice = false;
120121
private View blinkView = null;
121122
private View mView = null;
122123
private boolean manualCapture = false;
@@ -179,6 +180,10 @@ public void setOverlayColor(String rgbaColor) {
179180
this.overlayColor = rgbaColor;
180181
}
181182

183+
public void setSaveOnDevice(Boolean saveOnDevice) {
184+
this.saveOnDevice = saveOnDevice;
185+
}
186+
182187
public void setDetectionCountBeforeCapture(int numberOfRectangles) {
183188
this.numberOfRectangles = numberOfRectangles;
184189
}
@@ -653,12 +658,13 @@ public String saveToDirectory(Mat doc) {
653658
boolean isIntent = false;
654659
Uri fileUri = null;
655660
String folderName = "documents";
656-
File folder = new File(Environment.getExternalStorageDirectory().toString() + "/" + folderName);
661+
String folderDir = this.saveOnDevice ? Environment.getExternalStorageDirectory().toString() : this.mContext.getCacheDir().toString();
662+
File folder = new File( folderDir + "/" + folderName);
657663
if (!folder.exists()) {
658664
folder.mkdirs();
659665
Log.d(TAG, "wrote: created folder " + folder.getPath());
660666
}
661-
fileName = Environment.getExternalStorageDirectory().toString() + "/" + folderName + "/" + UUID.randomUUID()
667+
fileName = folderDir + "/" + folderName + "/" + UUID.randomUUID()
662668
+ ".jpg";
663669

664670
Mat endDoc = new Mat(Double.valueOf(doc.size().width).intValue(), Double.valueOf(doc.size().height).intValue(),
@@ -733,7 +739,9 @@ public void saveDocument(ScannedDocument scannedDocument) {
733739
mActivity.finish();
734740
} else {
735741
animateDocument(fileName, scannedDocument);
736-
addImageToGallery(fileName, mContext);
742+
if (this.saveOnDevice) {
743+
addImageToGallery(fileName, mContext);
744+
}
737745
}
738746

739747
// Record goal "PictureTaken"

index.js

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
import React from 'react';
2-
import { requireNativeComponent, NativeModules, View, Platform, DeviceEventEmitter } from 'react-native';
3-
import PropTypes from 'prop-types';
1+
import React from "react";
2+
import {
3+
requireNativeComponent,
4+
NativeModules,
5+
View,
6+
Platform,
7+
DeviceEventEmitter
8+
} from "react-native";
9+
import PropTypes from "prop-types";
410

5-
const RNPdfScanner = requireNativeComponent('RNPdfScanner', PdfScanner);
11+
const RNPdfScanner = requireNativeComponent("RNPdfScanner", PdfScanner);
612
const CameraManager = NativeModules.RNPdfScannerManager || {};
713

814
class PdfScanner extends React.Component {
9-
1015
static defaultProps = {
11-
onPictureTaken: ()=>{},
12-
onProcessing: ()=>{},
13-
}
16+
onPictureTaken: () => {},
17+
onProcessing: () => {}
18+
};
1419

1520
sendOnPictureTakenEvent(event) {
1621
return this.props.onPictureTaken(event.nativeEvent);
1722
}
1823

19-
sendOnRectanleDetectEvent(event) {
24+
sendOnRectangleDetectEvent(event) {
2025
if (!this.props.onRectangleDetect) return null;
2126
return this.props.onRectangleDetect(event.nativeEvent);
2227
}
@@ -28,19 +33,19 @@ class PdfScanner extends React.Component {
2833
return this.props.quality;
2934
}
3035

31-
componentWillMount(){
32-
if (Platform.OS === 'android') {
36+
componentWillMount() {
37+
if (Platform.OS === "android") {
3338
const { onPictureTaken, onProcessing } = this.props;
34-
DeviceEventEmitter.addListener('onPictureTaken', onPictureTaken);
35-
DeviceEventEmitter.addListener('onProcessingChange', onProcessing);
39+
DeviceEventEmitter.addListener("onPictureTaken", onPictureTaken);
40+
DeviceEventEmitter.addListener("onProcessingChange", onProcessing);
3641
}
3742
}
38-
39-
componentWillUnmount(){
40-
if (Platform.OS === 'android') {
43+
44+
componentWillUnmount() {
45+
if (Platform.OS === "android") {
4146
const { onPictureTaken, onProcessing } = this.props;
42-
DeviceEventEmitter.removeListener('onPictureTaken', onPictureTaken);
43-
DeviceEventEmitter.removeListener('onProcessingChange', onProcessing);
47+
DeviceEventEmitter.removeListener("onPictureTaken", onPictureTaken);
48+
DeviceEventEmitter.removeListener("onProcessingChange", onProcessing);
4449
}
4550
}
4651

@@ -54,14 +59,16 @@ class PdfScanner extends React.Component {
5459
<RNPdfScanner
5560
{...this.props}
5661
onPictureTaken={this.sendOnPictureTakenEvent.bind(this)}
57-
onRectangleDetect={this.sendOnRectanleDetectEvent.bind(this)}
58-
useFrontCam={this.props.useFrontCam||false}
59-
brightness={this.props.brightness||0}
60-
saturation={this.props.saturation||1}
61-
contrast={this.props.contrast||1}
62+
onRectangleDetect={this.sendOnRectangleDetectEvent.bind(this)}
63+
useFrontCam={this.props.useFrontCam || false}
64+
brightness={this.props.brightness || 0}
65+
saturation={this.props.saturation || 1}
66+
contrast={this.props.contrast || 1}
6267
quality={this.getImageQuality()}
63-
detectionCountBeforeCapture={this.props.detectionCountBeforeCapture||5}
64-
detectionRefreshRateInMS={this.props.detectionRefreshRateInMS||50}
68+
detectionCountBeforeCapture={
69+
this.props.detectionCountBeforeCapture || 5
70+
}
71+
detectionRefreshRateInMS={this.props.detectionRefreshRateInMS || 50}
6572
/>
6673
);
6774
}
@@ -79,11 +86,10 @@ PdfScanner.propTypes = {
7986
detectionCountBeforeCapture: PropTypes.number,
8087
detectionRefreshRateInMS: PropTypes.number,
8188
quality: PropTypes.number,
82-
83-
documentAnimation : PropTypes.bool,
89+
documentAnimation: PropTypes.bool,
8490
noGrayScale: PropTypes.bool,
8591
manualOnly: PropTypes.bool,
86-
...View.propTypes // include the default view properties
92+
...View.propTypes // include the default view properties
8793
};
8894

8995
export default PdfScanner;

0 commit comments

Comments
 (0)