Android签名捕捉

我正在android应用程序工作。 在我的项目中,我有一个关于签名捕获的任务,即用户应该在移动设备的屏幕上保留他/她的签名 ,一旦保存button被点击,签名必须存储在数据库中 。 我已经search并find了一些链接,但仍然没有find确切的解决scheme。 我也试过TouchPaint.java,但我没有find布局的XML文件。 你能不能给我们一些示例代码? 我会感激你的….

Solutions Collecting From Web of "Android签名捕捉"

对于任何寻找解决scheme的人,您都可以在http://www.mysamplecode.com/2011/11/android-capture-signature-using-canvas.html上find

它实际上是将签名写入文件,但是很容易改变并写入数据库。

这是Has AlTaiar的C#签名视图的工作Java版本,花了我一段时间让它正常工作100%

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; } } 

我试过罗布·克罗尔的build议,这个build议很好用,但是它是直线型的,使得签名看起来不像人。 如果你知道我的意思:P

这是你如何在一个空的线性布局上追加视图

 LinearLayout mContent = (LinearLayout) findViewById(R.id.linearLayout); CaptureSignatureView mSig = new CaptureSignatureView(this, null); mContent.addView(mSig, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 

这里是如何获取签名的字节或位图

 byte[] signature = mSig.getBytes(); Bitmap signature = mSig.getBitmap(); 

你可能需要手势build设者

我想这个链接。

http://android-developers.blogspot.com/2009/10/gestures-on-android-16.html

将对你有用。 如果你需要再次检查签名。

UPDATE

你在说这个吗?

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/TouchPaint.html

那么这个例子不使用xml 。 它具有作为内部类的视图(MyView)

我知道这是一个古老的问题,但我需要实现我自己的观点来捕获签名,因为我使用MonoForAndroid(C#不是Java)。 所以我在这里添加我的视图代码,以防有人需要它。

 using System; using Android.Content; using Android.Graphics; using Android.Views; namespace MyApp.Views { public class CaptureSignatureView : View { public CaptureSignatureView(Context c, SignatureData signatureData) : base(c) { SignatureData = signatureData; _Path = new Path(); _BitmapPaint = new Paint(PaintFlags.Dither); _paint = new Paint { AntiAlias = true, Dither = true, Color = Color.Argb(255, 0, 0, 0) }; _paint.SetStyle(Paint.Style.Stroke); _paint.StrokeJoin = Paint.Join.Round; _paint.StrokeCap = Paint.Cap.Round; _paint.StrokeWidth = 8; } protected override void OnSizeChanged(int w, int h, int oldw, int oldh) { base.OnSizeChanged(w, h, oldw, oldh); _Bitmap = Bitmap.CreateBitmap(w, (h > 0 ? h : ((View)this.Parent).Height), Bitmap.Config.Argb8888); _Canvas = new Canvas(_Bitmap); } protected override void OnDraw(Canvas canvas) { canvas.DrawColor(Color.White); canvas.DrawBitmap(_Bitmap, 0, 0, _BitmapPaint); canvas.DrawPath(_Path, _paint); } private float _mX, _mY; private const float TouchTolerance = 4; private void TouchStart(float x, float y) { _Path.Reset(); _Path.MoveTo(x, y); _mX = x; _mY = y; SignatureData.AddPoint(SignatureState.Start, (int)x, (int)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); SignatureData.AddPoint(SignatureState.Move, (int)x, (int)y); _mX = x; _mY = y; } } private void TouchUp() { if (!_Path.IsEmpty) { _Path.LineTo(_mX, _mY); _Canvas.DrawPath(_Path, _paint); } else { _Canvas.DrawPoint(_mX, _mY, _paint); } SignatureData.AddPoint(SignatureState.End, (int)_mX, (int)_mY); _Path.Reset(); } public override bool OnTouchEvent(MotionEvent e) { var x = e.GetX(); var y = e.GetY(); switch (e.Action) { case MotionEventActions.Down: TouchStart(x, y); Invalidate(); break; case MotionEventActions.Move: TouchMove(x, y); Invalidate(); break; case MotionEventActions.Up: TouchUp(); Invalidate(); break; } return true; } public void ClearCanvas() { _Canvas.DrawColor(Color.White); Invalidate(); } public Bitmap CanvasBitmap() { return _Bitmap; } public void Clear() { ClearCanvas(); SignatureData = new SignatureData(); } #region Implementation private Bitmap _Bitmap; private Canvas _Canvas; private readonly Path _Path; private readonly Paint _BitmapPaint; private readonly Paint _paint; public SignatureData SignatureData; #endregion } } 

另外,我在这里有一篇博客文章 ,详细介绍如何捕获签名。 基本上你可以用两种不同的方法做到这一点。 您可以将签名作为图像捕获视图,然后保存该位图(也可以将其发送到服务器)。 或者您可以捕获点的二维数组,并以任何您想要的方式重新构build签名。