Shreyash Mahajan
Shreyash Mahajan

Reputation: 23606

How to Implement the erase on image with this example and save it?

I am using this demo to imnplement the paint in my application. This Demo.

Now i want Image as a Background of the paint. after that the paint done on that image should be erased as like the it has functionality right now for paint. And while i save that image then it should be save with that Image.

So What should i have to do for that ??

Updated: Save code:

 case PHOTO_SAVE:
            final Activity currentActivity  = this;
            Handler saveHandler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    final AlertDialog alertDialog = new AlertDialog.Builder(currentActivity).create();
                    alertDialog.setTitle("Drawing App");
                    alertDialog.setMessage("Your drawing is saved. :)");
                    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            return;
                        }
                    });
                    alertDialog.show();
                }
            } ;
            System.out.println("1");
           new ExportBitmapToFile(this,saveHandler, mBitmap).execute();
           System.out.println("2");
           return true;

And the ExportBitmapToFile class is:

private class ExportBitmapToFile extends AsyncTask<Intent,Void,Boolean> {
    private Context mContext;
    private Handler mHandler;
    private Bitmap nBitmap;
    private ProgressDialog  m_progressDialog = null; 
    @Override     
    protected void onPreExecute(){         
        m_progressDialog = new ProgressDialog(mContext);  
        m_progressDialog.setTitle("Drawing App");
        m_progressDialog.setMessage("Please wait...");
        m_progressDialog.setCancelable(false);         
        m_progressDialog.show();     
        System.out.println("3");
    }

    public ExportBitmapToFile(Context context,Handler handler,Bitmap bitmap) {
        mContext = context;
        nBitmap = bitmap;
        mHandler = handler;
    }

    @Override
    protected Boolean doInBackground(Intent... arg0) {
        try {
            if (!APP_FILE_PATH.exists()) {
                APP_FILE_PATH.mkdirs();
            }
            System.out.println("4");
            final FileOutputStream out = new FileOutputStream(new File(APP_FILE_PATH + "/"+filename+".jpg"));
            nBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            System.out.println("5");
            out.flush();
            out.close();
            return true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        //mHandler.post(completeRunnable);
        return false;
    }

    @Override
    protected void onPostExecute(Boolean bool) {
        super.onPostExecute(bool);
        if ( bool ){
            mHandler.sendEmptyMessage(1);
        }
        if (m_progressDialog.isShowing()) {             
            m_progressDialog.dismiss();          
        }         
    }
}

Upvotes: 0

Views: 591

Answers (1)

st0le
st0le

Reputation: 33545

A Small Change.

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            mBitmap = Bitmap.createScaledBitmap(mBackground, w, h, true);
            mCanvas = new Canvas(mBitmap);
        }

where mBackground is a Bitmap you initialize in the Constructor of MyView.

I'm pretty sure, that should do, if you have any problems do report back.

Update : See Comments

In That Case, forget the above changes, and modify the onDraw Method like this.

  @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFAAAAAA);
        canvas.drawBitmap(mBackground, 0, 0, mBitmapPaint); //Make sure mBackground is the same size as the view. 
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        canvas.drawPath(mPath, mPaint);
    }

While saving, You'll have to overlay both Bitmaps onto a new Bitmap Object and then finally write it to disk.

To Overlap both Bitmaps,

private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) { //code borrowed from stackoverflow question 1540272
        Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
        Canvas canvas = new Canvas(bmOverlay);
        canvas.drawBitmap(bmp1, new Matrix(), null); // or use the other overloaded functions
        canvas.drawBitmap(bmp2, new Matrix(), null);
        return bmOverlay;
    }

Upvotes: 2

Related Questions