1 | package ve.gob.cenditel.tibisaymovil; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.FileOutputStream; |
---|
5 | import java.util.Calendar; |
---|
6 | import android.app.Activity; |
---|
7 | import android.content.Context; |
---|
8 | import android.content.ContextWrapper; |
---|
9 | import android.content.Intent; |
---|
10 | import android.graphics.Bitmap; |
---|
11 | import android.graphics.Canvas; |
---|
12 | import android.graphics.Color; |
---|
13 | import android.graphics.Paint; |
---|
14 | import android.graphics.Path; |
---|
15 | import android.graphics.RectF; |
---|
16 | import android.os.Bundle; |
---|
17 | import android.os.Environment; |
---|
18 | import android.provider.MediaStore.Images; |
---|
19 | import android.util.AttributeSet; |
---|
20 | import android.util.Log; |
---|
21 | import android.view.MotionEvent; |
---|
22 | import android.view.View; |
---|
23 | import android.view.Window; |
---|
24 | import android.view.View.OnClickListener; |
---|
25 | import android.view.ViewGroup.LayoutParams; |
---|
26 | import android.widget.LinearLayout; |
---|
27 | import android.widget.Toast; |
---|
28 | |
---|
29 | public class CaptureImgHandwrittenSignatureActivity extends Activity{ |
---|
30 | |
---|
31 | LinearLayout mContent; |
---|
32 | signature mSignature; |
---|
33 | LinearLayout mClear, mGetSign, mCancel; |
---|
34 | |
---|
35 | public static String tempDir; |
---|
36 | public int count = 1; |
---|
37 | public String current = null; |
---|
38 | |
---|
39 | private Bitmap mBitmap; |
---|
40 | View mView; |
---|
41 | File mypath; |
---|
42 | private Canvas canvas; |
---|
43 | |
---|
44 | private String uniqueId; |
---|
45 | private String fileToSign; |
---|
46 | |
---|
47 | @Override |
---|
48 | public void onCreate(Bundle savedInstanceState) |
---|
49 | { |
---|
50 | super.onCreate(savedInstanceState); |
---|
51 | //Capturando archivo que será firmado |
---|
52 | fileToSign = getIntent().getExtras().getString("FILE_TO_SIGN"); |
---|
53 | |
---|
54 | //Estilando la barra de titulo |
---|
55 | final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); |
---|
56 | setContentView(R.layout.activity_capture_img_handwritten_signature); |
---|
57 | //Estilando Barra de titulo |
---|
58 | if(customTitleSupported) |
---|
59 | getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); |
---|
60 | |
---|
61 | tempDir = Environment.getExternalStorageDirectory() + "/" + |
---|
62 | getResources().getString(R.string.app_name) + "/" + |
---|
63 | getResources().getString(R.string.external_dir) + "/"; |
---|
64 | |
---|
65 | /* |
---|
66 | * Crea si es necesario, un directorio en el cual la aplicación |
---|
67 | * puede colocar sus propios archivos de datos personalizados |
---|
68 | */ |
---|
69 | ContextWrapper cw = new ContextWrapper(getApplicationContext()); |
---|
70 | File directory = cw.getDir(getResources().getString(R.string.external_dir), Context.MODE_PRIVATE); |
---|
71 | File directory2 = cw.getDir(getResources().getString(R.string.external_dir_files), Context.MODE_PRIVATE); |
---|
72 | |
---|
73 | prepareDirectory(); |
---|
74 | |
---|
75 | // Obtiene identificador unico para el nombre del archivo |
---|
76 | uniqueId = getTodaysDate() + "_" + getCurrentTime(); |
---|
77 | current = uniqueId + ".png"; |
---|
78 | |
---|
79 | //Ruta completa para crear el archivo |
---|
80 | mypath= new File(directory,current); |
---|
81 | |
---|
82 | // Obtiene referencia a los recursos del layout |
---|
83 | mContent = (LinearLayout) findViewById(R.id.linearLayout); |
---|
84 | mClear = (LinearLayout)findViewById(R.id.button_clear_zone); |
---|
85 | mGetSign = (LinearLayout)findViewById(R.id.button_accept_zone); |
---|
86 | mCancel = (LinearLayout)findViewById(R.id.button_cancel_zone); |
---|
87 | |
---|
88 | mSignature = new signature(this, null); |
---|
89 | mContent.addView(mSignature, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); |
---|
90 | mGetSign.setEnabled(false); |
---|
91 | mView = mContent; |
---|
92 | |
---|
93 | |
---|
94 | mClear.setOnClickListener(new OnClickListener() |
---|
95 | { |
---|
96 | public void onClick(View v) |
---|
97 | { |
---|
98 | mSignature.clear(); |
---|
99 | mGetSign.setEnabled(false); |
---|
100 | } |
---|
101 | }); |
---|
102 | |
---|
103 | mGetSign.setOnClickListener(new OnClickListener() |
---|
104 | { |
---|
105 | public void onClick(View v) |
---|
106 | { |
---|
107 | boolean error = captureSignature(); |
---|
108 | if(!error){ |
---|
109 | mView.setDrawingCacheEnabled(true); |
---|
110 | mSignature.save(mView); |
---|
111 | |
---|
112 | |
---|
113 | Intent intent = new Intent(CaptureImgHandwrittenSignatureActivity.this, SelectSignUbicationHandwrittenSignatureActivity.class); |
---|
114 | intent.putExtra("STATUS", "done"); |
---|
115 | intent.putExtra("IMAGE_CAPTURED", mypath.getAbsolutePath()); |
---|
116 | intent.putExtra("FILE_TO_SIGN", fileToSign); |
---|
117 | startActivity(intent); |
---|
118 | Log.i("DEBUG", "saliendo"); |
---|
119 | // setResult(RESULT_OK,intent); |
---|
120 | // finish(); |
---|
121 | |
---|
122 | } |
---|
123 | } |
---|
124 | }); |
---|
125 | |
---|
126 | mCancel.setOnClickListener(new OnClickListener() |
---|
127 | { |
---|
128 | public void onClick(View v) |
---|
129 | { |
---|
130 | finish(); |
---|
131 | } |
---|
132 | }); |
---|
133 | |
---|
134 | } |
---|
135 | |
---|
136 | @Override |
---|
137 | protected void onDestroy() { |
---|
138 | super.onDestroy(); |
---|
139 | } |
---|
140 | |
---|
141 | private boolean captureSignature() { |
---|
142 | |
---|
143 | boolean error = false; |
---|
144 | //String errorMessage = ""; |
---|
145 | |
---|
146 | /* |
---|
147 | // por ahora no nos interesa mostrar la caja de texto |
---|
148 | if(yourName.getText().toString().equalsIgnoreCase("")){ |
---|
149 | errorMessage = errorMessage + "Por favor introduzca su nombre\n"; |
---|
150 | error = true; |
---|
151 | } |
---|
152 | |
---|
153 | if(error){ |
---|
154 | Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); |
---|
155 | toast.setGravity(Gravity.TOP, 105, 50); |
---|
156 | toast.show(); |
---|
157 | } |
---|
158 | */ |
---|
159 | return error; |
---|
160 | } |
---|
161 | |
---|
162 | private String getTodaysDate() { |
---|
163 | |
---|
164 | final Calendar c = Calendar.getInstance(); |
---|
165 | int todaysDate = (c.get(Calendar.YEAR) * 10000) + |
---|
166 | ((c.get(Calendar.MONTH) + 1) * 100) + |
---|
167 | (c.get(Calendar.DAY_OF_MONTH)); |
---|
168 | return(String.valueOf(todaysDate)); |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | private String getCurrentTime() { |
---|
173 | |
---|
174 | final Calendar c = Calendar.getInstance(); |
---|
175 | int currentTime = (c.get(Calendar.HOUR_OF_DAY) * 10000) + |
---|
176 | (c.get(Calendar.MINUTE) * 100) + |
---|
177 | (c.get(Calendar.SECOND)); |
---|
178 | return(String.valueOf(currentTime)); |
---|
179 | |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * Prepara directorio |
---|
184 | * @return boolean |
---|
185 | */ |
---|
186 | private boolean prepareDirectory() |
---|
187 | { |
---|
188 | try |
---|
189 | { |
---|
190 | if (makedirs()) |
---|
191 | { |
---|
192 | return true; |
---|
193 | } else { |
---|
194 | return false; |
---|
195 | } |
---|
196 | } catch (Exception e) |
---|
197 | { |
---|
198 | e.printStackTrace(); |
---|
199 | Toast.makeText(this, "Could not initiate File System.. Is Sdcard mounted properly?", Toast.LENGTH_LONG).show(); |
---|
200 | return false; |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | /** |
---|
205 | * Crea directorio utilizando la variable tmpDir |
---|
206 | * @return boolean |
---|
207 | */ |
---|
208 | private boolean makedirs() |
---|
209 | { |
---|
210 | File tempdir = new File(tempDir); |
---|
211 | if (!tempdir.exists()) |
---|
212 | tempdir.mkdirs(); |
---|
213 | |
---|
214 | if (tempdir.isDirectory()) |
---|
215 | { |
---|
216 | File[] files = tempdir.listFiles(); |
---|
217 | for (File file : files) |
---|
218 | { |
---|
219 | if (!file.delete()) |
---|
220 | { |
---|
221 | System.out.println("Failed to delete " + file); |
---|
222 | } |
---|
223 | } |
---|
224 | } |
---|
225 | return (tempdir.isDirectory()); |
---|
226 | } |
---|
227 | |
---|
228 | public class signature extends View |
---|
229 | { |
---|
230 | private static final float STROKE_WIDTH = 5f; |
---|
231 | private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2; |
---|
232 | private Paint paint = new Paint(); |
---|
233 | private Path path = new Path(); |
---|
234 | |
---|
235 | private float lastTouchX; |
---|
236 | private float lastTouchY; |
---|
237 | private final RectF dirtyRect = new RectF(); |
---|
238 | |
---|
239 | public signature(Context context, AttributeSet attrs) |
---|
240 | { |
---|
241 | super(context, attrs); |
---|
242 | paint.setAntiAlias(true); |
---|
243 | paint.setColor(Color.BLACK); |
---|
244 | paint.setStyle(Paint.Style.STROKE); |
---|
245 | paint.setStrokeJoin(Paint.Join.ROUND); |
---|
246 | paint.setStrokeWidth(STROKE_WIDTH); |
---|
247 | } |
---|
248 | |
---|
249 | public void save(View v) |
---|
250 | { |
---|
251 | |
---|
252 | if(mBitmap == null) |
---|
253 | { |
---|
254 | mBitmap = Bitmap.createBitmap (mContent.getWidth(), mContent.getHeight(), Bitmap.Config.ARGB_8888); |
---|
255 | } |
---|
256 | canvas = new Canvas(mBitmap); |
---|
257 | try |
---|
258 | { |
---|
259 | //Log.v("*****yourName:", yourName.getText().toString()); |
---|
260 | |
---|
261 | FileOutputStream mFileOutStream = new FileOutputStream(mypath); |
---|
262 | |
---|
263 | v.draw(canvas); |
---|
264 | mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream); |
---|
265 | mFileOutStream.flush(); |
---|
266 | mFileOutStream.close(); |
---|
267 | String url = Images.Media.insertImage(getContentResolver(), mBitmap, "title", null); |
---|
268 | |
---|
269 | // Toast.makeText(CaptureImgHandwrittenSignatureActivity.this, |
---|
270 | // getString(R.string.firma_guardada) + " " + mypath, Toast.LENGTH_SHORT).show(); |
---|
271 | |
---|
272 | //In case you want to delete the file |
---|
273 | //boolean deleted = mypath.delete(); |
---|
274 | //Log.v("log_tag","deleted: " + mypath.toString() + deleted); |
---|
275 | //If you want to convert the image to string use base64 converter |
---|
276 | |
---|
277 | } |
---|
278 | catch(Exception e) |
---|
279 | { |
---|
280 | Log.v("log_tag", e.toString()); |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | public void clear() |
---|
285 | { |
---|
286 | path.reset(); |
---|
287 | invalidate(); |
---|
288 | } |
---|
289 | |
---|
290 | @Override |
---|
291 | protected void onDraw(Canvas canvas) |
---|
292 | { |
---|
293 | canvas.drawPath(path, paint); |
---|
294 | // canvas.drawCircle(lastTouchX, lastTouchX, HALF_STROKE_WIDTH, paint); |
---|
295 | } |
---|
296 | |
---|
297 | @Override |
---|
298 | public boolean onTouchEvent(MotionEvent event) |
---|
299 | { |
---|
300 | float eventX = event.getX(); |
---|
301 | float eventY = event.getY(); |
---|
302 | mGetSign.setEnabled(true); |
---|
303 | |
---|
304 | switch (event.getAction()) |
---|
305 | { |
---|
306 | case MotionEvent.ACTION_DOWN: |
---|
307 | path.moveTo(eventX, eventY); |
---|
308 | lastTouchX = eventX; |
---|
309 | lastTouchY = eventY; |
---|
310 | |
---|
311 | return true; |
---|
312 | |
---|
313 | case MotionEvent.ACTION_MOVE: |
---|
314 | |
---|
315 | case MotionEvent.ACTION_UP: |
---|
316 | |
---|
317 | resetDirtyRect(eventX, eventY); |
---|
318 | int historySize = event.getHistorySize(); |
---|
319 | for (int i = 0; i < historySize; i++) |
---|
320 | { |
---|
321 | float historicalX = event.getHistoricalX(i); |
---|
322 | float historicalY = event.getHistoricalY(i); |
---|
323 | expandDirtyRect(historicalX, historicalY); |
---|
324 | path.lineTo(historicalX, historicalY); |
---|
325 | } |
---|
326 | path.lineTo(eventX, eventY); |
---|
327 | break; |
---|
328 | |
---|
329 | default: |
---|
330 | Log.i("DRAW:","Ignored touch event: " + event.toString()); |
---|
331 | return false; |
---|
332 | } |
---|
333 | |
---|
334 | invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH), |
---|
335 | (int) (dirtyRect.top - HALF_STROKE_WIDTH), |
---|
336 | (int) (dirtyRect.right + HALF_STROKE_WIDTH), |
---|
337 | (int) (dirtyRect.bottom + HALF_STROKE_WIDTH)); |
---|
338 | |
---|
339 | lastTouchX = eventX; |
---|
340 | lastTouchY = eventY; |
---|
341 | |
---|
342 | return true; |
---|
343 | } |
---|
344 | |
---|
345 | private void debug(String string){ |
---|
346 | } |
---|
347 | |
---|
348 | private void expandDirtyRect(float historicalX, float historicalY) |
---|
349 | { |
---|
350 | if (historicalX < dirtyRect.left) |
---|
351 | { |
---|
352 | dirtyRect.left = historicalX; |
---|
353 | } |
---|
354 | else if (historicalX > dirtyRect.right) |
---|
355 | { |
---|
356 | dirtyRect.right = historicalX; |
---|
357 | } |
---|
358 | |
---|
359 | if (historicalY < dirtyRect.top) |
---|
360 | { |
---|
361 | dirtyRect.top = historicalY; |
---|
362 | } |
---|
363 | else if (historicalY > dirtyRect.bottom) |
---|
364 | { |
---|
365 | dirtyRect.bottom = historicalY; |
---|
366 | } |
---|
367 | } |
---|
368 | |
---|
369 | private void resetDirtyRect(float eventX, float eventY) |
---|
370 | { |
---|
371 | dirtyRect.left = Math.min(lastTouchX, eventX); |
---|
372 | dirtyRect.right = Math.max(lastTouchX, eventX); |
---|
373 | dirtyRect.top = Math.min(lastTouchY, eventY); |
---|
374 | dirtyRect.bottom = Math.max(lastTouchY, eventY); |
---|
375 | } |
---|
376 | |
---|
377 | } |
---|
378 | } |
---|