Android将文本添加到图片并保存 [英] Android add text to Picture and save

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

问题描述

我想在图片中放置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;
}
;
};

解决方案

如果您只想使用文本",而不必使用TextView,则可以使用

如果确实需要将其用作TextView,则可以将视图转换为位图,然后使用drawBitmap()将其绘制在画布上.有关如何将其转换的示例,请参见此答案.

I want to place a Textiew to my picture and save it, but I'm not sure how to insert the text into the picture.

I can attach an image on my picture save it, and it works but now I want to insert a Textiew into the picture.

Here is my code:

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

解决方案

If you just want "text", and not necessarily a TextView, you can draw text directly on the Canvas using drawText().

Just change it to something like:

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

If you need it to be a 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.

这篇关于Android将文本添加到图片并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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