画布上的图像到JPEG文件

时间:2021-12-28 00:49:29

I'm drawing 2D images on the canvas.

我正在画布上绘制2D图像。

I want to save image shown on canvas to JPEG file, how can I do it?

我想将画布上显示的图像保存为JPEG文件,我该怎么办?

3 个解决方案

#1


24  

  1. create an empty bitmap
  2. 创建一个空位图

  3. create a new Canvas object and pass this bitmap to it
  4. 创建一个新的Canvas对象并将此位图传递给它

  5. call view.draw(Canvas) passing it the canvas object you just created. Refer Documentation of method for details.
  6. 调用view.draw(Canvas)将刚刚创建的画布对象传递给它。有关详细信息,请参阅方法文档

  7. Use Bitmap.compress() to write the contents of the bitmap to an OutputStream, file maybe.
  8. 使用Bitmap.compress()将位图的内容写入OutputStream,文件可能。

Pseudo code:

Bitmap  bitmap = Bitmap.createBitmap( view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 

#2


12  

  1. set Drawing Cache Enabled
  2. 设置绘图缓存已启用

  3. Draw whatever you want
  4. 画任何你想要的东西

  5. Get Bitmap object from view
  6. 从视图中获取Bitmap对象

  7. Compress and save the image file
  8. 压缩并保存图像文件


import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class CanvasTest extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Draw2d d = new Draw2d(this);
        setContentView(d);
    }

    public class Draw2d extends View {

        public Draw2d(Context context) {
            super(context);
            setDrawingCacheEnabled(true);
        }

        @Override
        protected void onDraw(Canvas c) {
            Paint paint = new Paint();
            paint.setColor(Color.RED);          
            c.drawCircle(50, 50, 30, paint);

            try {
                getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/mnt/sdcard/arun.jpg")));
            } catch (Exception e) {
                Log.e("Error--------->", e.toString());
            }
            super.onDraw(c);
        }

    }

}

#3


6  

Canvas to JPG:

画布到JPG:

Canvas canvas = null;
FileOutputStream fos = null;
Bitmap bmpBase = null;

bmpBase = Bitmap.createBitmap(image_width, image_height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bmpBase);
// draw what ever you want canvas.draw...

// Save Bitmap to File
try
{
    fos = new FileOutputStream(your_path);
    bmpBase.compress(Bitmap.CompressFormat.PNG, 100, fos);

    fos.flush();
    fos.close();
    fos = null;
}
catch (IOException e)
{
    e.printStackTrace();
}
finally
{
    if (fos != null)
    {
        try
        {
            fos.close();
            fos = null;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

#1


24  

  1. create an empty bitmap
  2. 创建一个空位图

  3. create a new Canvas object and pass this bitmap to it
  4. 创建一个新的Canvas对象并将此位图传递给它

  5. call view.draw(Canvas) passing it the canvas object you just created. Refer Documentation of method for details.
  6. 调用view.draw(Canvas)将刚刚创建的画布对象传递给它。有关详细信息,请参阅方法文档

  7. Use Bitmap.compress() to write the contents of the bitmap to an OutputStream, file maybe.
  8. 使用Bitmap.compress()将位图的内容写入OutputStream,文件可能。

Pseudo code:

Bitmap  bitmap = Bitmap.createBitmap( view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 

#2


12  

  1. set Drawing Cache Enabled
  2. 设置绘图缓存已启用

  3. Draw whatever you want
  4. 画任何你想要的东西

  5. Get Bitmap object from view
  6. 从视图中获取Bitmap对象

  7. Compress and save the image file
  8. 压缩并保存图像文件


import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class CanvasTest extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Draw2d d = new Draw2d(this);
        setContentView(d);
    }

    public class Draw2d extends View {

        public Draw2d(Context context) {
            super(context);
            setDrawingCacheEnabled(true);
        }

        @Override
        protected void onDraw(Canvas c) {
            Paint paint = new Paint();
            paint.setColor(Color.RED);          
            c.drawCircle(50, 50, 30, paint);

            try {
                getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/mnt/sdcard/arun.jpg")));
            } catch (Exception e) {
                Log.e("Error--------->", e.toString());
            }
            super.onDraw(c);
        }

    }

}

#3


6  

Canvas to JPG:

画布到JPG:

Canvas canvas = null;
FileOutputStream fos = null;
Bitmap bmpBase = null;

bmpBase = Bitmap.createBitmap(image_width, image_height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bmpBase);
// draw what ever you want canvas.draw...

// Save Bitmap to File
try
{
    fos = new FileOutputStream(your_path);
    bmpBase.compress(Bitmap.CompressFormat.PNG, 100, fos);

    fos.flush();
    fos.close();
    fos = null;
}
catch (IOException e)
{
    e.printStackTrace();
}
finally
{
    if (fos != null)
    {
        try
        {
            fos.close();
            fos = null;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}