我有一个问题。
我尝试将画布图像保存到文件
这是我的工作方式。 1.用位图创建画布对象(表单图像) 2.print在画布上的文字(使用drawText) 3.save with bitmap.compress()方法
但它只保存了原始位图(没有文字)
我想知道如何将文本打印的位图保存到文件中? 这是我的代码
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();
}
}
}
答案 0 :(得分:0)
Bitmap.compresss()
此方法将位图的压缩版本写入指定的输出流。
将位图实例传递给画布
这是伪代码。 (除了其他声明,例如try-catch,声明)
this post也会有所帮助。