1 package tz.signaturecapture; |
1 package tz.signaturecapture; |
2 |
2 |
3 import java.io.File; |
3 import java.io.File; |
4 import java.io.FileOutputStream; |
4 import java.io.FileOutputStream; |
|
5 import java.io.IOException; |
|
6 import java.io.OutputStream; |
5 import java.util.Calendar; |
7 import java.util.Calendar; |
6 |
8 |
7 import android.app.Activity; |
9 import android.app.Activity; |
8 import android.content.Context; |
10 import android.content.Context; |
9 import android.content.ContextWrapper; |
11 import android.content.ContextWrapper; |
10 import android.content.Intent; |
12 import android.content.Intent; |
11 import android.graphics.Bitmap; |
13 import android.graphics.Bitmap; |
14 import android.graphics.Paint; |
16 import android.graphics.Paint; |
15 import android.graphics.Path; |
17 import android.graphics.Path; |
16 import android.graphics.RectF; |
18 import android.graphics.RectF; |
17 import android.os.Bundle; |
19 import android.os.Bundle; |
18 import android.os.Environment; |
20 import android.os.Environment; |
19 import android.provider.MediaStore.Images; |
|
20 import android.util.AttributeSet; |
21 import android.util.AttributeSet; |
21 import android.util.Log; |
22 import android.util.Log; |
22 import android.view.Gravity; |
23 import android.view.Gravity; |
23 import android.view.MotionEvent; |
24 import android.view.MotionEvent; |
24 import android.view.View; |
25 import android.view.View; |
25 import android.view.View.OnClickListener; |
26 import android.view.View.OnClickListener; |
|
27 import android.view.View.OnTouchListener; |
26 import android.view.ViewGroup.LayoutParams; |
28 import android.view.ViewGroup.LayoutParams; |
27 import android.view.Window; |
29 import android.view.Window; |
28 import android.widget.Button; |
30 import android.widget.Button; |
29 import android.widget.EditText; |
31 import android.widget.EditText; |
30 import android.widget.LinearLayout; |
32 import android.widget.LinearLayout; |
31 import android.widget.Toast; |
33 import android.widget.Toast; |
32 |
34 |
33 public class CaptureSignature extends Activity { |
35 public class CaptureSignature extends Activity { |
34 |
36 |
35 LinearLayout mContent; |
37 LinearLayout mContent; |
36 signature mSignature; |
38 SignatureWidget mSignature; |
37 Button mClear, mGetSign, mCancel; |
39 Button mClear, mGetSign, mCancel; |
38 public static String tempDir; |
40 public static String tempDir; |
39 public int count = 1; |
41 public int count = 1; |
40 public String current = null; |
42 public String current = null; |
41 private Bitmap mBitmap; |
|
42 View mView; |
43 View mView; |
43 File mypath; |
44 File mypath; |
44 |
45 |
45 private String uniqueId; |
46 private String uniqueId; |
46 private EditText yourName; |
47 private EditText yourName; |
61 current = uniqueId + ".png"; |
62 current = uniqueId + ".png"; |
62 mypath= new File(directory,current); |
63 mypath= new File(directory,current); |
63 |
64 |
64 |
65 |
65 mContent = (LinearLayout) findViewById(R.id.linearLayout); |
66 mContent = (LinearLayout) findViewById(R.id.linearLayout); |
66 mSignature = new signature(this, null); |
67 mSignature = new SignatureWidget(this, null); |
67 mSignature.setBackgroundColor(Color.WHITE); |
68 mSignature.setBackgroundColor(Color.WHITE); |
|
69 mSignature.setOnTouchListener(new OnTouchListener() { |
|
70 @Override |
|
71 public boolean onTouch(View v, MotionEvent event) { |
|
72 mGetSign.setEnabled(true); |
|
73 return false; |
|
74 } |
|
75 }); |
68 mContent.addView(mSignature, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); |
76 mContent.addView(mSignature, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); |
69 mClear = (Button)findViewById(R.id.clear); |
77 mClear = (Button)findViewById(R.id.clear); |
70 mGetSign = (Button)findViewById(R.id.getsign); |
78 mGetSign = (Button)findViewById(R.id.getsign); |
71 mGetSign.setEnabled(false); |
79 mGetSign.setEnabled(false); |
72 mCancel = (Button)findViewById(R.id.cancel); |
80 mCancel = (Button)findViewById(R.id.cancel); |
90 { |
98 { |
91 Log.v("log_tag", "Panel Saved"); |
99 Log.v("log_tag", "Panel Saved"); |
92 boolean error = captureSignature(); |
100 boolean error = captureSignature(); |
93 if(!error){ |
101 if(!error){ |
94 mView.setDrawingCacheEnabled(true); |
102 mView.setDrawingCacheEnabled(true); |
95 mSignature.save(mView); |
103 try { |
96 Bundle b = new Bundle(); |
104 FileOutputStream os = new FileOutputStream(mypath); |
97 b.putString("status", "done"); |
105 mSignature.save(mView, os); |
98 Intent intent = new Intent(); |
106 os.flush(); |
99 intent.putExtras(b); |
107 os.close(); |
100 setResult(RESULT_OK,intent); |
108 setResult(RESULT_OK, new Intent().putExtra("status", |
101 finish(); |
109 "done")); |
|
110 finish(); |
|
111 } catch (IOException e) { |
|
112 Toast.makeText(CaptureSignature.this, String.format( |
|
113 "Failed to save signature: %", e.getMessage()), |
|
114 Toast.LENGTH_LONG).show(); |
|
115 } |
|
116 |
102 } |
117 } |
103 } |
118 } |
104 }); |
119 }); |
105 |
120 |
106 mCancel.setOnClickListener(new OnClickListener() |
121 mCancel.setOnClickListener(new OnClickListener() |
204 } |
219 } |
205 } |
220 } |
206 return (tempdir.isDirectory()); |
221 return (tempdir.isDirectory()); |
207 } |
222 } |
208 |
223 |
209 public class signature extends View |
224 public static class SignatureWidget extends View |
210 { |
225 { |
211 private static final float STROKE_WIDTH = 5f; |
226 private static final float STROKE_WIDTH = 5f; |
212 private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2; |
227 private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2; |
213 private Paint paint = new Paint(); |
228 private Paint paint = new Paint(); |
214 private Path path = new Path(); |
229 private Path path = new Path(); |
215 |
230 |
216 private float lastTouchX; |
231 private float lastTouchX; |
217 private float lastTouchY; |
232 private float lastTouchY; |
218 private final RectF dirtyRect = new RectF(); |
233 private final RectF dirtyRect = new RectF(); |
219 |
234 |
220 public signature(Context context, AttributeSet attrs) |
235 public SignatureWidget(Context context, AttributeSet attrs) |
221 { |
236 { |
222 super(context, attrs); |
237 super(context, attrs); |
223 paint.setAntiAlias(true); |
238 paint.setAntiAlias(true); |
224 paint.setColor(Color.BLACK); |
239 paint.setColor(Color.BLACK); |
225 paint.setStyle(Paint.Style.STROKE); |
240 paint.setStyle(Paint.Style.STROKE); |
226 paint.setStrokeJoin(Paint.Join.ROUND); |
241 paint.setStrokeJoin(Paint.Join.ROUND); |
227 paint.setStrokeWidth(STROKE_WIDTH); |
242 paint.setStrokeWidth(STROKE_WIDTH); |
228 } |
243 } |
229 |
244 |
230 public void save(View v) |
245 public void save(View parent, OutputStream os) |
231 { |
246 { |
232 Log.v("log_tag", "Width: " + v.getWidth()); |
247 Log.v("log_tag", "Width: " + parent.getWidth()); |
233 Log.v("log_tag", "Height: " + v.getHeight()); |
248 Log.v("log_tag", "Height: " + parent.getHeight()); |
234 if(mBitmap == null) |
249 Bitmap bm = Bitmap.createBitmap(parent.getWidth(), parent.getHeight(), Bitmap.Config.RGB_565);; |
235 { |
250 Canvas canvas = new Canvas(bm); |
236 mBitmap = Bitmap.createBitmap (mContent.getWidth(), mContent.getHeight(), Bitmap.Config.RGB_565);; |
251 parent.draw(canvas); |
237 } |
252 bm.compress(Bitmap.CompressFormat.PNG, 90, os); |
238 Canvas canvas = new Canvas(mBitmap); |
|
239 try |
|
240 { |
|
241 FileOutputStream mFileOutStream = new FileOutputStream(mypath); |
|
242 |
|
243 v.draw(canvas); |
|
244 mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream); |
|
245 mFileOutStream.flush(); |
|
246 mFileOutStream.close(); |
|
247 String url = Images.Media.insertImage(getContentResolver(), mBitmap, "title", null); |
|
248 Log.v("log_tag","url: " + url); |
|
249 //In case you want to delete the file |
|
250 //boolean deleted = mypath.delete(); |
|
251 //Log.v("log_tag","deleted: " + mypath.toString() + deleted); |
|
252 //If you want to convert the image to string use base64 converter |
|
253 |
|
254 } |
|
255 catch(Exception e) |
|
256 { |
|
257 Log.v("log_tag", e.toString()); |
|
258 } |
|
259 } |
253 } |
260 |
254 |
261 public void clear() |
255 public void clear() |
262 { |
256 { |
263 path.reset(); |
257 path.reset(); |
273 @Override |
267 @Override |
274 public boolean onTouchEvent(MotionEvent event) |
268 public boolean onTouchEvent(MotionEvent event) |
275 { |
269 { |
276 float eventX = event.getX(); |
270 float eventX = event.getX(); |
277 float eventY = event.getY(); |
271 float eventY = event.getY(); |
278 mGetSign.setEnabled(true); |
|
279 |
272 |
280 switch (event.getAction()) |
273 switch (event.getAction()) |
281 { |
274 { |
282 case MotionEvent.ACTION_DOWN: |
275 case MotionEvent.ACTION_DOWN: |
283 path.moveTo(eventX, eventY); |
276 path.moveTo(eventX, eventY); |