图像在画布上为JPEG文件画布、上为、图像、文件

2023-09-12 00:11:58 作者:冰冷的温柔

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

我要保存在画布上为JPEG文件中显示的图像,我该怎么办呢?

解决方案 创建一个空的位图 创建一个新的Canvas对象,这个位图传递给它 在调用view.draw(Canvas)的通过它您刚才创建的画布对象。 Refer方法的详细资料。 使用Bitmap.com preSS()写的位图的内容到OutputStream,文件可能。

伪code:

 位图位= Bitmap.createBitmap(view.getWidth(),view.getHeight(),Bitmap.Config.ARGB_8888);
帆布油画=新的Canvas(位);
view.draw(画布);
bitmap.com preSS(Bitmap.Com pressFormat.JPEG,100,FOS);
 

I'm drawing 2D images on the canvas.

PS基本操作介绍

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

解决方案

create an empty bitmap create a new Canvas object and pass this bitmap to it call view.draw(Canvas) passing it the canvas object you just created. Refer Documentation of method for details. Use Bitmap.compress() to write the contents of the bitmap to an OutputStream, file maybe.

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);