如何绘制的图像并保存图像到Android? [英] How to paint on Image and save that image in to Android?

查看:149
本文介绍了如何绘制的图像并保存图像到Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的画布。我想用我的已经保存的图像,并希望该图像上的一些油漆。从那以后,我要保存它。

I am new to canvas. I want to use the My already saved Image and want some paint on that image. after that i want to save it.

我知道,与使用Canvas是可能的。我能够做画上的图像,但同时我要存储的图像只保存了绘画。不是图像的画。

I know that with using Canvas it is possible. I can able to do painting on the Image but while i am going to store that image it only saved the painting. Not the Image with painting.

这样可以anybudy告诉我怎么画图像code和保存图像?

So can anybudy tell me code of how to paint on image and save that image ?

感谢。

下面是我的使用做油漆的SurfaceView code。 来源$ C ​​$ C:

Here is my Code that use to do paint on the SurfaceView. Source Code:

    @Override
        public void run() {
            //Canvas canvas = null;
            while (_run){

                try{
                    canvas = mSurfaceHolder.lockCanvas(null);
                    if(mBitmap == null){
                        mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
                    }

                    final Canvas c = new Canvas (mBitmap);
                    //canvas.drawColor(0, PorterDuff.Mode.CLEAR);
                    c.drawColor(0, PorterDuff.Mode.CLEAR);
                    canvas.drawColor(Color.WHITE); 
//                  Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);

//                    if(!(DrawingActivity.imagePath==null)){
//                      canvas.drawBitmap(DrawingActivity.mBitmap, 0, 0, null);
//                    }
                    commandManager.executeAll(c);
                    canvas.drawBitmap (mBitmap, 0,  0,null);
                } finally {

                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                }
            }

        }

我使用mBitmap保存位图到SD卡。

I am using mBitmap to save the Bitmap to the SDCard.

推荐答案

您的问题是绘图一遍又一遍整个画布上:

Your problem is your drawing over and over on your entire canvas:

 final Canvas c = new Canvas (mBitmap); // creates a new canvas with your image is painted background
 c.drawColor(0, PorterDuff.Mode.CLEAR); // this makes your whole Canvas transparent
 canvas.drawColor(Color.WHITE);  // this makes it all white on another canvas
 canvas.drawBitmap (mBitmap, 0,  0,null); // this draws your bitmap on another canvas

使用逻辑大致是这样的:

Use logic roughly like this:

@Override
public void run() {

Canvas c = new Canvas(mBitmap);


/* Paint your things here, example: c.drawLine()... Beware c.drawColor will fill your canvas, so your bitmap will be cleared!!!*/
...

/* Now mBitmap will have both the original image & your painting */
String path = Environment.getExternalStorageDirectory().toString(); // this is the sd card
OutputStream fOut = null;
File file = new File(path, "MyImage.jpg");
fOut = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
}

另外不要忘了加上必要的权限来保存文件:

Also don't forget to add necessary permission to save your file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

之外&lt;应用&GT;&LT; /用途&gt;在清单文件

这篇关于如何绘制的图像并保存图像到Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆