Tuesday, 16 April 2013

Hi,

HereI am posting code for Digital Signature.

Create Android project in that

Create MainAct.Class

package com.example.digitalsignatre;

/**
 *  Rahul Baradia
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    public Canvas  mCanvas;   
    private MyView signature;
    private Button saveButton;
    private Button clearButton;

    // onCreate Activity
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFF003F87);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(3);

        // content view
        signature = new MyView(this);        

        // Dynamically created button on bitmap & canvas       
        RelativeLayout myLayout = new RelativeLayout(this);       
        myLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        myLayout.setBackgroundColor(Color.WHITE);

        // For Save Button
        saveButton = new Button(this);
        saveButton.setText("Save");
        saveButton.setOnClickListener(this);

        // For Clear Button
        clearButton = new Button(this);
        clearButton.setText("Clear");
        clearButton.setOnClickListener(this);

        myLayout.addView(signature);
        myLayout.addView(saveButton);
        myLayout.addView(clearButton);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT);
        //Alignments
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        saveButton.setLayoutParams(params);

        //Alignments
        RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT);
        params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        clearButton.setLayoutParams(params2);

        saveButton.bringToFront();
        clearButton.bringToFront();
        this.setContentView(myLayout);
        new Thread(new RefreshRunner()).start();

        // onclick listner for CLEAR button
        clearButton.setOnClickListener(new OnClickListener() {           
            public void onClick(View v) {
                //Activity for Clearing the Screen
                startActivity(new Intent(MainActivity.this, MainActivity.class));
                finish();
            }
        });
    }

    public Paint mPaint;
    public Bitmap mBitmap;
    public View view;
    public Bitmap b;
    public int w;
    public int h;
    public int Wid;
    public int Ht;

    public void colorChanged(int color) {
        mPaint.setColor(color);
    }

    // Bitmap MyView
    public class MyView extends View implements OnClickListener
    {
        public int height;
        public int width;        
        private Bitmap  mBitmap;       
        private Path    mPath;
        private Paint   mBitmapPaint;

        public MyView(Context c)
        {
            super(c);
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);  
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh)
        {
            super.onSizeChanged(w, h, oldw, oldh); 
            Wid = w;
            Ht = h;
            mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);           
            mCanvas = new Canvas(mBitmap);   


        }

        @Override
        protected void onDraw(final Canvas canvas)
        { try{
            //canvas BG color
            canvas.drawColor(Color.WHITE);   


            canvas.drawPath(mPath, mPaint);
            canvas.drawBitmap(mBitmap, 0, 0, null);

            // onclick listner for SAVE button
            saveButton.setOnClickListener(new OnClickListener() { 
                public void onClick(View v) {
                    //capture the image 
                    try {       
                        //background color of Bitmap
                        Bitmap newBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), mBitmap.getConfig());

                        Canvas canvas = new Canvas(newBitmap);
                        canvas.drawColor(Color.WHITE);
                        canvas.drawBitmap(mBitmap, 0, 0, null);

                        newBitmap = Bitmap.createScaledBitmap(newBitmap, 200, 150, true);
                        // Saving the Image
                        saveAsJpg(newBitmap);   
                    } catch (IOException e) {
                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                       
                        e.printStackTrace();
                    }
                } 
            });     
        }catch (Exception e) {Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();

       
        e.printStackTrace();
        }
        }

        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;

        private void touch_start(float x, float y)
        {
            mPath.reset();
            mPath.moveTo(x, y);
            mX = x;
            mY = y;
            System.out.println("---- " +mX);
        }
        private void touch_move(float x, float y)
        {
            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                mX = x;
                mY = y;
            }
        }
        private void touch_up()
        {
            mPath.lineTo(mX, mY);
            // commit the path to our offscreen
            mCanvas.drawPath(mPath, mPaint);

            // kill this so we don't double draw
            mPath.reset();
        }

        @Override
        public boolean onTouchEvent(MotionEvent event)
        {

            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;

            }
            return true;
        }

        public void onClick(View v)
        {
            //In myView
        }
    }

    public void onClick(View v)
    {
        //onCreate
    }
    public static final String DIRECTORY = "/sdcard";
    public static  String signatureImagePath = DIRECTORY+"/signature.png"; 

    // saveAsJpg Method
    public void saveAsJpg(Bitmap b) throws IOException
    {  
        File f= new File(DIRECTORY);
        if(! f.exists())
        {
            f.mkdir();
        }
        FileOutputStream fos = null;
        try { 
            fos = new FileOutputStream(signatureImagePath);
            if(fos != null)
            {     
                b.compress(Bitmap.CompressFormat.PNG, 100, fos );
                Toast.makeText(getApplicationContext(), "Saved to External Memory", Toast.LENGTH_SHORT).show();
                fos.close();
            }
        }catch( NullPointerException e )
        {
            Log.e("testSaveView", "Exception: " + e.toString() );
        }
    }
}



AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.digitalsignatre"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
   
   // give permission for external storage
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
   


    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Here is the app image



3 comments:

  1. Hi.. In top left corner save button is appear what i do?

    ReplyDelete
  2. hi this was really fabulous but there is memory leakage problem with this code
    its saying out of memory how to overcome that please help me with that.

    ReplyDelete