antwankakki / FabricView

A new canvas drawing library for Android. Aims to be the Fabric.js for Android. Supports text, images, and hand/stylus drawing input. The library has a website and API docs, check it out
http://antwankakki.github.io/FabricView/
Apache License 2.0
1.04k stars 183 forks source link

any way to save draw result as image file #7

Closed ghost closed 9 years ago

ghost commented 9 years ago

would be nice we could save draw result as image file

antwankakki commented 9 years ago

Good note, will add it in today

antwankakki commented 9 years ago

Hello Just got home and wrote this method to retrieve current drawing as a Bitmap. getCanvasBitmap() You can check out other resources such as this question to see how you can save a bitmap! http://stackoverflow.com/questions/15662258/how-to-save-a-bitmap-on-internal-storage

ghost commented 9 years ago

great! Thanks

ghost commented 9 years ago

do you have source code code for the app show in the animated GIF in Sample Screen Recording?

antwankakki commented 9 years ago

No :( it was something I scrapped together on a computer in school and deleted later due to space issues there). I'll put together another one this weekend and upload it

ghost commented 9 years ago

Thanks! I have write following code to save FabricView as png. The result of png has transparent background, are there anyway to set initial fabricView background color or image? I tried the FabricView.setBackgroundColor(getColor(R.color.white)), it doesn't work, it still return transparent background png

final FabricView fabricView = (FabricView) findViewById(R.id.drawView); fabricView.setStyle(Paint.Style.STROKE); fabricView.setSize(UnitHelper.DIP2Pixel(this, 4));

    findViewById(R.id.commit).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                File file = getFileBackend().getTempFile("png");
                FileOutputStream fos = new FileOutputStream(file);
                fabricView.getCanvasBitmap().compress(Bitmap.CompressFormat.PNG, 90, fos);
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
ghost commented 9 years ago

I am able to covert the transparent background bitmap to white background using following sample code:

FileOutputStream fos = new FileOutputStream(file); Bitmap drawBmp = fabricView.getCanvasBitmap(); Bitmap newBmp = Bitmap.createBitmap(drawBmp.getWidth(), drawBmp.getHeight(), drawBmp.getConfig()); Canvas canvas = new Canvas(newBmp); canvas.drawColor(Color.WHITE); canvas.drawBitmap(drawBmp, 0, 0, null); newBmp.compress(Bitmap.CompressFormat.PNG, 90, fos); fos.close();

antwankakki commented 9 years ago

Glad you got it working!