2

I want to capture image of surface view with content draw on surface view. I n my app i have surface view on which user can draw some bitmap by drag and drop some objects on it. After that on a button click i want to store image of that surfaceview with its bitmap content in sd card.

For these i have a canvas object and all bitmap are draw on it. below is my code -

                mHolder = mSurfaceView.getHolder();
                mHolder.lockCanvas();
                canvas.drawColor(0,Mode.CLEAR);
                //border's properties
                paint = new Paint();
                FontMetrics fm = new FontMetrics();
                Paint paint2 = new Paint();
                paint2.setColor(Color.WHITE);
                paint2.setTextSize(30);
                paint2.setTextAlign(Paint.Align.CENTER);
                paint2.getFontMetrics(fm);  
                canvas.drawText("Building", RectLeft+60, RectTop+60, paint2);
                paint.setStyle(Paint.Style.STROKE);
                paint.setColor(color);
                paint.setStrokeWidth(3);
                canvas.drawRect(RectLeft, RectTop, RectRight, RectBottom, paint);
                mHolder.unlockCanvasAndPost(canvas);

How to do that.

Thanks in advance.

EDITED

Below is my code that captures only view that i have created in xml and not draw what i have draw on surface.

public static Bitmap getBitmapFromView(View view) {
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    //Get the view's background
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) 
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);
    else 
        //does not have background drawable, then draw white background on the canvas
        canvas.drawColor(Color.WHITE);
    // draw the view on the canvas
    view.draw(canvas);
    //return the bitmap
    return returnedBitmap;
}

EDITED my hole fragment for drawing is below-

public class DrawingLayoutFragment extends Fragment implements SurfaceHolder.Callback{

      @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
  ....
  ....
   mSurfaceView = (SurfaceView) view.findViewById(R.id.surfaceview);
  init();
  return view;
}

public void init(){
    mSurfaceView.getHolder().addCallback(this);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
        RectLeft = width/2 - 60;
        RectTop = height/2 - 60 ;
        RectRight = width/2 + 60;
        RectBottom = height/2 + 60;
        DrawFocusRect(RectLeft, RectTop, RectRight, RectBottom, getResources().getColor(com.nep.R.color.white_color));
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    mHolder = mSurfaceView.getHolder();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    try{
        if(mHolder!=null){
            mHolder.lockCanvas();
            canvas = null;
            mHolder.addCallback(null);
            mHolder = null;
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}

 private void DrawFocusRect(float RectLeft, float RectTop, float RectRight, float RectBottom, int color){
         try {
                if(canvas!=null){
                    mHolder.lockCanvas();
                }
                else{
                    canvas = mHolder.lockCanvas();
                }
                canvas.drawColor(0,Mode.CLEAR);
                paint = new Paint();
                FontMetrics fm = new FontMetrics();
                Paint paint2 = new Paint();
                paint2.setColor(Color.WHITE);
                paint2.setTextSize(30);
                paint2.setTextAlign(Paint.Align.CENTER);
                paint2.getFontMetrics(fm);  
                canvas.drawText("Building", RectLeft+60, RectTop+60, paint2);
                paint.setStyle(Paint.Style.STROKE);
                paint.setColor(color);
                paint.setStrokeWidth(3);
                canvas.drawRect(RectLeft, RectTop, RectRight, RectBottom, paint);
                mHolder.unlockCanvasAndPost(canvas);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
      public void createFIle(){
    try {
        DrawFocusRect(RectLeft, RectTop, RectRight, RectBottom, getResources().getColor(com.nep.R.color.white_color));
         Bitmap b = Bitmap.createBitmap(mSurfaceView.getWidth(), mSurfaceView.getHeight(), Bitmap.Config.ARGB_8888); 
         Canvas c = new Canvas(b);
         mSurfaceView.draw(c);

    //  Bitmap bmp = getBitmapFromView(frame);

        String mFile = mPath+"_layout011.png";
        File directory = new File (mFile);
        directory.createNewFile();
        OutputStream fo = new FileOutputStream(directory);
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        b.compress(CompressFormat.JPEG, 100, os);
        os.writeTo(fo);
        fo.close();
        os.close();
    //  mSurfaceView.destroyDrawingCache();
        BasicMethods.showToast("Saved at - "+mFile, mContext);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

i have user this code and it is not working,so i put my surface view inside other view and try to capture bitmap from that i was previously posted.i have try both way but not geting any solution.

fadden
  • 51,356
  • 5
  • 116
  • 166
Ravi Bhandari
  • 4,682
  • 8
  • 40
  • 68

1 Answers1

3

Create a Bitmap, create a Canvas for the bitmap, execute all of your drawing commands on that Canvas, save the bitmap.

You can't capture the SurfaceView's surface, so re-drawing onto a Bitmap is the easiest way. This comes up fairly often; see e.g. this.

Community
  • 1
  • 1
fadden
  • 51,356
  • 5
  • 116
  • 166
  • Thanks for your reply,Please check my updated question.I have added code for geting bitmap.which i am using currently but it is not working. – Ravi Bhandari Dec 13 '14 at 03:34
  • Now I'm confused. Are you drawing on the SurfaceView's View, or the SurfaceView's Surface? Looking at your first code snippet again, it seems you're ignoring the Canvas returned by `lockCanvas()`... where is the Canvas coming from? (FWIW http://stackoverflow.com/questions/2661536/ has another take on the problem, but that's for the View part only.) – fadden Dec 13 '14 at 05:03