-
Notifications
You must be signed in to change notification settings - Fork 511
/
RSSignatureCaptureMainView.java
218 lines (172 loc) · 6.48 KB
/
RSSignatureCaptureMainView.java
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.rssignaturecapture;
import android.util.Log;
import android.view.ViewGroup;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import android.util.Base64;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import java.lang.Boolean;
public class RSSignatureCaptureMainView extends LinearLayout implements OnClickListener,RSSignatureCaptureView.SignatureCallback {
LinearLayout buttonsLayout;
RSSignatureCaptureView signatureView;
Activity mActivity;
int mOriginalOrientation;
Boolean saveFileInExtStorage = false;
String viewMode = "portrait";
Boolean showBorder = true;
Boolean showNativeButtons = true;
Boolean showTitleLabel = true;
int maxSize = 500;
public RSSignatureCaptureMainView(Context context, Activity activity) {
super(context);
Log.d("React:", "RSSignatureCaptureMainView(Contructtor)");
mOriginalOrientation = activity.getRequestedOrientation();
mActivity = activity;
this.setOrientation(LinearLayout.VERTICAL);
this.signatureView = new RSSignatureCaptureView(context,this);
// add the buttons and signature views
this.buttonsLayout = this.buttonsLayout();
this.addView(this.buttonsLayout);
this.addView(signatureView);
setLayoutParams(new android.view.ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
}
public RSSignatureCaptureView getSignatureView() {
return signatureView;
}
public void setSaveFileInExtStorage(Boolean saveFileInExtStorage) {
this.saveFileInExtStorage = saveFileInExtStorage;
}
public void setViewMode(String viewMode) {
this.viewMode = viewMode;
if (viewMode.equalsIgnoreCase("portrait")) {
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (viewMode.equalsIgnoreCase("landscape")) {
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
public void setShowNativeButtons(Boolean showNativeButtons) {
this.showNativeButtons = showNativeButtons;
if (showNativeButtons) {
Log.d("Added Native Buttons", "Native Buttons:" + showNativeButtons);
buttonsLayout.setVisibility(View.VISIBLE);
} else {
buttonsLayout.setVisibility(View.GONE);
}
}
public void setMaxSize(int size) {
this.maxSize = size;
}
private LinearLayout buttonsLayout() {
// create the UI programatically
LinearLayout linearLayout = new LinearLayout(this.getContext());
Button saveBtn = new Button(this.getContext());
Button clearBtn = new Button(this.getContext());
// set orientation
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setBackgroundColor(Color.WHITE);
// set texts, tags and OnClickListener
saveBtn.setText("Save");
saveBtn.setTag("Save");
saveBtn.setOnClickListener(this);
clearBtn.setText("Reset");
clearBtn.setTag("Reset");
clearBtn.setOnClickListener(this);
linearLayout.addView(saveBtn);
linearLayout.addView(clearBtn);
// return the whoe layout
return linearLayout;
}
// the on click listener of 'save' and 'clear' buttons
@Override public void onClick(View v) {
String tag = v.getTag().toString().trim();
// save the signature
if (tag.equalsIgnoreCase("save")) {
this.saveImage();
}
// empty the canvas
else if (tag.equalsIgnoreCase("Reset")) {
this.signatureView.clearSignature();
}
}
/**
* save the signature to an sd card directory
*/
final void saveImage() {
// the directory where the signature will be saved
File myDir = getContext().getExternalFilesDir("/saved_signature");
// make the directory if it does not exist yet
if (!myDir.exists()) {
myDir.mkdirs();
}
// set the file name of your choice
String fname = "signature.png";
// in our case, we delete the previous file, you can remove this
File file = new File(myDir, fname);
if (file.exists()) {
file.delete();
}
try {
Log.d("React Signature", "Save file-======:" + saveFileInExtStorage);
// save the signature
if (saveFileInExtStorage) {
FileOutputStream out = new FileOutputStream(file);
this.signatureView.getSignature().compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Bitmap resizedBitmap = getResizedBitmap(this.signatureView.getSignature());
resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.NO_WRAP);
WritableMap event = Arguments.createMap();
event.putString("pathName", file.getAbsolutePath());
event.putString("encoded", encoded);
ReactContext reactContext = (ReactContext) getContext();
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "topChange", event);
} catch (Exception e) {
e.printStackTrace();
}
}
public Bitmap getResizedBitmap(Bitmap image) {
Log.d("React Signature","maxSize:"+maxSize);
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
public void reset() {
if (this.signatureView != null) {
this.signatureView.clearSignature();
}
}
@Override public void onDragged() {
WritableMap event = Arguments.createMap();
event.putBoolean("dragged", true);
ReactContext reactContext = (ReactContext) getContext();
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "topChange", event);
}
}