【问题标题】:Android add text to Picture and saveAndroid将文字添加到图片并保存
【发布时间】:2013-08-01 19:08:47
【问题描述】:

我想在我的图片上放置一个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;
}
;
};

【问题讨论】:

    标签: android camera android-camera android-camera-intent


    【解决方案1】:

    如果您只想要“文本”,而不一定是TextView,您可以使用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,您可以将视图转换为位图,然后使用drawBitmap() 在画布上绘制它。有关如何转换它的示例,请参阅this answer

    【讨论】:

    • 我应该在 myPaint 中添加什么?
    • Paint 让您定义文本的外观。对齐方式、颜色、字体等。您可能只需传递null,但我不知道默认绘制是什么样的。我从来没有将它用于文本。
    • @Geobits cameraBitmap 是什么?
    猜你喜欢
    • 1970-01-01
    • 2011-10-15
    • 2012-12-03
    • 2012-01-23
    • 1970-01-01
    • 2015-10-20
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多