2011-08-29 71 views
26

我正在android應用程序上工作。在我的項目我有一個關於簽名捕獲即用戶任務應該保持他/她的簽名移動的屏幕上,一旦保存按鈕被點擊的簽名具有存儲在數據庫。我搜索並找到了一些鏈接,但仍然沒有找到確切的解決方案。 我也試過TouchPaint.java,但我沒有找到用於佈局的xml文件。 你能否給我們提供一些示例代碼?我會很感激你......Android簽名捕獲

+1

也許視圖這[相關問題] [1]給你一些提示。 [1]:http://stackoverflow.com/questions/4658703/signature-capture-in-phonegap-android-application – Ber

+0

可能重複:需要實現簽名捕獲(HTTP://計算器.com/q/3752003/145173) –

回答

4

你可能需要手勢生成器

我覺得這個鏈接。

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的)

+0

嗨,感謝您的回覆。我只需要TouchPaint應用程序。但是當我執行應用程序時,我無法查看輸出。我是否需要添加任何其他新文件來執行應用程序?我已經添加了兩個其他類GraphicsActivity和PictureLayout ... –

+2

你可以在android sdk的** API演示** com.example.android.apis.graphics **中找到TouchPaing/FingerPaint的完整工作示例** – Samuel

4

我知道這是一個老問題,但我需要實現我自己的視圖來捕獲簽名,因爲我使用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 
    } 

} 

另外,我有一個blog post here通過的如何捕捉簽名的細節去。基本上你可以用兩種不同的方式做到這一點。您可以將簽名作爲圖像捕捉到視圖中,然後保存該位圖(也可以將其發送到服務器)。或者您可以捕獲點的二維數組,並以任何您想要的方式重新構建簽名。

+1

你的「博客​​文章」不存在。 – Pierre

+0

如何在繪製後檢索位圖?每次繪圖時,我都會得到一張空白紙,它會粘在紙上。但保存,不能得到與圖紙上的位圖? – Pierre

+0

+1謝謝你的代碼,看我的帖子是你的C#代碼的java版本 – Pierre

29

這裏是擁有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; 
    } 
} 

我想羅布·克羅爾的建議,其工作良好,但它是直線性,使簽名不是人看。如果你知道我的意思: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(); 
+0

是否有可能檢查用戶是否已經簽名或不簽名? –

+0

@ItuokeAjanlekoko你可以添加一個事件來檢查行的長度,如果它不止一個點,將bool標誌設置爲true – Pierre

+1

是的。有這樣的事情,但我沒有檢查線路長度。我將它添加到運動事件和運動事件中。如果用戶清除,它將變成錯誤。謝謝。 –