Thursday, September 25, 2014

Save Gesture as Bitmap


Write inside your activity_main.xml file


<android.gesture.GestureOverlayView android:id="@+id/signaturePad"
         android:layout_width="match_parent"
         android:layout_height="0dp"

         android:layout_weight="5"

         android:background="@color/white"

         android:eventsInterceptionEnabled="true"

         android:fadeEnabled="false"

         android:gestureColor="@color/black"

         android:gestureStrokeLengthThreshold="0.1"

         android:gestureStrokeType="multiple"

         android:orientation="vertical" >

</android.gesture.GestureOverlayView>

<ImageView 

android:id="@+id/imvSignature"

android:layout_width="wrap_content"

 android:layout_height="150dp"

 android:layout_marginTop="5dp"

/>   

public class MainActivity extends Activity implements OnGesturePerformedListener{


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ImageView imvSignature =(ImageView) findViewById(R.id.imvSignature);

GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.signaturePad);
gestures.addOnGesturePerformedListener(this);

}



@Override
public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {
try {
gestures.setDrawingCacheEnabled(true);
   Bitmap bm = Bitmap.createBitmap(gestures.getDrawingCache());
  imvSignature.setImageBitmap(bm);
File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "signature.png");
  f.createNewFile();
  FileOutputStream os = new FileOutputStream(f);
 os = new FileOutputStream(f);
 //compress to specified format (PNG), quality - which is ignored for PNG, and out stream
 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
 os.close();
  } catch (Exception e) {
           Log.v("Gestures", e.getMessage());
           e.printStackTrace();
  }
}

simple draw anything on the screen , the same will be converted to image and will be stored in the sdcard.For confirmation i show the drawn gesture through a imageview.
N.B.:: It can beuseful to take signature of the user and send to the server or save in the sdcard for future reference.

References : http://www.intertech.com/Blog/android-gestureoverlayview-to-capture-a-quick-signature-or-drawing/

->http://androidresearch.wordpress.com/2012/01/10/working-with-gesture-api-in-android/

Thanx

No comments:

Post a Comment