I am working on android application. In my project I have a task regarding signature capture i.e the user should keep his/her signature on the screen of the mobile and once save button is clicked the signature has to stored in the database. I have searched and found some links but still I didn't find the exact solution. I also tried TouchPaint.java but there I didnt find the xml file for layout. Could you please suggest us with some sample code? I will be thankful to you....
If no signature has been previously stored on the device, tap Create Signature, or to replace an existing signature, tap Clear Saved Signature and re-tap > Create Signature. Tap to use your camera to capture an image of your signature. (You can also Hand draw a signature or tap to choose an image on your device.)
If you don't currently have the Generate Signed Bundle or APK dialog open, click Build > Generate Signed Bundle/APK. In the Generate Signed Bundle or APK dialog, select either Android App Bundle or APK and click Next.
A signature capture pad is a device that electronically captures a person's handwritten signature on an LCD touchpad using a pen-type stylus. As a user digitally "signs" a data capture's LCD touchpad, a sensor "reads" the pressure from the tip of the stylus, transmitting signature data to a computer.
Here is the working Java version of Has AlTaiar's C# Signature View, Took me a while to get it to work 100% correctly
public class CaptureSignatureView extends View { private Bitmap _Bitmap; private Canvas _Canvas; private Path _Path; private Paint _BitmapPaint; private Paint _paint; private float _mX; private float _mY; private float TouchTolerance = 4; private float LineThickness = 4; public CaptureSignatureView(Context context, AttributeSet attr) { super(context, attr); _Path = new Path(); _BitmapPaint = new Paint(Paint.DITHER_FLAG); _paint = new Paint(); _paint.setAntiAlias(true); _paint.setDither(true); _paint.setColor(Color.argb(255, 0, 0, 0)); _paint.setStyle(Paint.Style.STROKE); _paint.setStrokeJoin(Paint.Join.ROUND); _paint.setStrokeCap(Paint.Cap.ROUND); _paint.setStrokeWidth(LineThickness); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); _Bitmap = Bitmap.createBitmap(w, (h > 0 ? h : ((View) this.getParent()).getHeight()), Bitmap.Config.ARGB_8888); _Canvas = new Canvas(_Bitmap); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.WHITE); canvas.drawBitmap(_Bitmap, 0, 0, _BitmapPaint); canvas.drawPath(_Path, _paint); } private void TouchStart(float x, float y) { _Path.reset(); _Path.moveTo(x, y); _mX = x; _mY = y; } private void TouchMove(float x, float y) { float dx = Math.abs(x - _mX); float dy = Math.abs(y - _mY); if (dx >= TouchTolerance || dy >= TouchTolerance) { _Path.quadTo(_mX, _mY, (x + _mX) / 2, (y + _mY) / 2); _mX = x; _mY = y; } } private void TouchUp() { if (!_Path.isEmpty()) { _Path.lineTo(_mX, _mY); _Canvas.drawPath(_Path, _paint); } else { _Canvas.drawPoint(_mX, _mY, _paint); } _Path.reset(); } @Override public boolean onTouchEvent(MotionEvent e) { super.onTouchEvent(e); float x = e.getX(); float y = e.getY(); switch (e.getAction()) { case MotionEvent.ACTION_DOWN: TouchStart(x, y); invalidate(); break; case MotionEvent.ACTION_MOVE: TouchMove(x, y); invalidate(); break; case MotionEvent.ACTION_UP: TouchUp(); invalidate(); break; } return true; } public void ClearCanvas() { _Canvas.drawColor(Color.WHITE); invalidate(); } public byte[] getBytes() { Bitmap b = getBitmap(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); b.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } public Bitmap getBitmap() { View v = (View) this.getParent(); Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); v.draw(c); return b; } }
I tried Rob Croll's suggestion, which worked good, but it is straight liney, rendering the signature not human looking. If you know what I mean :P
Here is how you append the view on an empty linear layout
LinearLayout mContent = (LinearLayout) findViewById(R.id.linearLayout); CaptureSignatureView mSig = new CaptureSignatureView(this, null); mContent.addView(mSig, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
Here is how to get the bytes or Bitmap of the Signature
byte[] signature = mSig.getBytes(); Bitmap signature = mSig.getBitmap();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With