-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathScan.java
More file actions
executable file
·134 lines (112 loc) · 5.04 KB
/
Scan.java
File metadata and controls
executable file
·134 lines (112 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.neutrinos.plugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.StrictMode;
import android.util.Base64;
import com.scanlibrary.ScanActivity;
import com.scanlibrary.ScanConstants;
import java.io.ByteArrayOutputStream;
import java.net.URL;
public class Scan extends CordovaPlugin {
private static final int REQUEST_CODE = 99;
private static final int PHOTOLIBRARY = 0; // Choose image from picture library (same as SAVEDPHOTOALBUM for Android)
private static final int CAMERA = 1; // Take picture from camera
private int srcType;
private int quality;
private boolean returnBase64;
public CallbackContext callbackContext;
public static final int PERMISSION_DENIED_ERROR = 20;
public static final int TAKE_PIC_SEC = 0;
public static final int SAVE_TO_ALBUM_SEC = 1;
protected final static String[] permissions = { Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE };
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("scanDoc")) {
this.srcType = CAMERA;
//Take the values from the arguments if they're not already defined (this is tricky)
//[sourceType, fileName, quality, returnBase64]
this.srcType = args.getInt(0);
this.quality = args.getInt(2);
this.returnBase64 = args.getBoolean(3);
this.callbackContext = callbackContext;
cordova.setActivityResultCallback(this);
int preference = 0;
try {
if (this.srcType == CAMERA) {
preference = ScanConstants.OPEN_CAMERA;
} else if (this.srcType == PHOTOLIBRARY) {
preference = ScanConstants.OPEN_MEDIA;
}
Intent intent = new Intent(cordova.getActivity().getApplicationContext(), ScanActivity.class);
intent.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE, preference);
intent.putExtra("quality", this.quality);
cordova.getActivity().startActivityForResult(intent, REQUEST_CODE);
} catch (IllegalArgumentException e) {
this.callbackContext.error("Illegal Argument Exception");
PluginResult r = new PluginResult(PluginResult.Status.ERROR);
this.callbackContext.sendPluginResult(r);
} catch (Exception e) {
this.callbackContext.error("Something went wrong! Try reducing the quality option.");
PluginResult r = new PluginResult(PluginResult.Status.ERROR);
this.callbackContext.sendPluginResult(r);
}
return true;
} else {
return false;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == cordova.getActivity().RESULT_OK) {
Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);
if (uri != null) {
String fileLocation = "file://" + FileHelper.getRealPath(uri, this.cordova);
if(returnBase64) {
this.callbackContext.success(convertUrlToBase64(fileLocation));
} else {
this.callbackContext.success(fileLocation);
}
} else {
this.callbackContext.error("null data from scan libary");
}
} else {
this.callbackContext.error("Incorrect result or user canceled the action.");
}
}
/**
*
* @param url - web url
* @return - Base64 String
* Method used to Convert URL to Base64 String
*/
public String convertUrlToBase64(String url) {
URL newurl;
Bitmap bitmap;
String base64 = "";
try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
newurl = new URL(url);
bitmap = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
base64 = Base64.encodeToString(outputStream.toByteArray(), Base64.NO_WRAP);
} catch (Exception e) {
this.callbackContext.error(e.getMessage());
}
return base64;
}
}