Android将文本添加到图片并保存 [英] Android add text to Picture and save
问题描述
我想在图片中放置Textiew
并将其保存,但是我不确定如何将文字插入图片中.
我可以将图像附加到图片上并保存,它可以工作,但是现在我想在图片中插入Textiew
.
这是我的代码:
PictureCallback cameraPictureCallbackJpeg = new PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
// TODO Auto-generated method stub
Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
wid = cameraBitmap.getWidth();
hgt = cameraBitmap.getHeight();
Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newImage);
canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
Drawable drawable = getResources().getDrawable(R.drawable.love);
drawable.setBounds(20, 20, 260, 160);
drawable.draw(canvas);
File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyPicture/");
storagePath.mkdirs();
File myImage = new File(storagePath,Long.toString(System.currentTimeMillis()) + ".jpg");
try
{
FileOutputStream out = new FileOutputStream(myImage);
newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);
out.flush();
out.close();
}
catch(FileNotFoundException e)
{
Log.d("In Saving File", e + "");
}
catch(IOException e)
{
Log.d("In Saving File", e + "");
}
camera.startPreview();
drawable = null;
newImage.recycle();
newImage = null;
cameraBitmap.recycle();
cameraBitmap = null;
}
;
};
如果您只想使用文本",而不必使用 如果确实需要将其用作 I want to place a I can attach an image on my picture save it, and it works but now I want to insert a Here is my code:
If you just want "text", and not necessarily a Just change it to something like: If you need it to be a 这篇关于Android将文本添加到图片并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!TextView
,则可以使用TextView
,则可以将视图转换为位图,然后使用drawBitmap()
将其绘制在画布上.有关如何将其转换的示例,请参见此答案.Textiew
to my picture and save it, but I'm not sure how to insert the text into the picture.Textiew
into the picture.PictureCallback cameraPictureCallbackJpeg = new PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
// TODO Auto-generated method stub
Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
wid = cameraBitmap.getWidth();
hgt = cameraBitmap.getHeight();
Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newImage);
canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
Drawable drawable = getResources().getDrawable(R.drawable.love);
drawable.setBounds(20, 20, 260, 160);
drawable.draw(canvas);
File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyPicture/");
storagePath.mkdirs();
File myImage = new File(storagePath,Long.toString(System.currentTimeMillis()) + ".jpg");
try
{
FileOutputStream out = new FileOutputStream(myImage);
newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);
out.flush();
out.close();
}
catch(FileNotFoundException e)
{
Log.d("In Saving File", e + "");
}
catch(IOException e)
{
Log.d("In Saving File", e + "");
}
camera.startPreview();
drawable = null;
newImage.recycle();
newImage = null;
cameraBitmap.recycle();
cameraBitmap = null;
}
;
};
TextView
, you can draw text directly on the Canvas using drawText()
. Canvas canvas = new Canvas(newImage);
canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
canvas.drawText("some text here", x, y, myPaint);
...
newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);
TextView
for sure, you can convert the view to a bitmap, then draw it on the canvas with drawBitmap()
. See this answer for an example of how to convert it.