1

I have a question.

I try to save canvas image to file

here is how i work. 1.Create canvas object with bitmap(form image) 2.print Text on canvas (Using drawText) 3.save with bitmap.compress() method

but it saved only original bitmap(with out text)

I wonder how I can save text printed bitmap to file? Here is my code

protected class MyView extends View{

    public MyView(Context context){
        super(context);
    }

    public void onDraw(Canvas canvas){
        Paint pnt = new Paint();
        pnt.setColor(Color.BLACK);
        canvas.scale(0.5f,0.5f);
        Resources res = getResources();
        BitmapDrawable bd =(BitmapDrawable)res.getDrawable(R.drawable.hanhwa);
        Bitmap bit = bd.getBitmap();
        canvas.drawBitmap(bit,0,0,null);

        pnt.setTextSize(30);
        canvas.drawText("hello",290,340,pnt);
        canvas.restore();


        //file save//
        File folder = new File(Environment.getExternalStorageDirectory()+"/DCIM/tmp");

        boolean isExist = true;
        if(!folder.exists()){
            isExist = folder.mkdir();
        }
        if(isExist){
            File file = null;
            boolean isFileExist = false ;
            file = new File(folder.getPath() + "/tmp.jpg");

            if(file != null && !file.exists()){
                try{
                    isFileExist = file.createNewFile();
                } catch(IOException e){
                    e.printStackTrace();
                }
            }
            else{
            }

            if(file.exists()){
                FileOutputStream fos = null;
                try{
                    fos = new FileOutputStream(file);

                    bit.compress(Bitmap.CompressFormat.JPEG,100,fos);
                }
                catch(Exception e){
                    e.printStackTrace();
                }
                finally{
                    try{
                        fos.close();
                    }
                    catch(IOException e){
                        e.printStackTrace();
                    }
                }

            }
        }
        else{
            //Toast.makeText(MyView.this,"foler not exist.",Toast.LENGTH_LONG).show();
        }
    }
}
DevMs
  • 11
  • 3

1 Answers1

0
Bitmap.compresss()

this method write a compressed version of the bitmap to the specified outputstream.

Pass bitmap instance to canvas

Here is the pseudo-code. (except other statement, ex. try-catch, statement)

  1. Canvas c = new Canvas(bit);
  2. c.drawText("hello",290,340,pnt);
  3. b.compress(Bitmap.CompressFormat.JPEG, 100, fos);

this post will be helpful too.

Community
  • 1
  • 1