我有使用相机意图的应用程序并保存拍摄到特定文件夹的图片。现在我缺少一个功能,那就是拍照后我应该添加纯文本(时间戳,图片下边缘/上边框上的更多信息)并保存。
如何实现这一目标?我的代码在
之下public void takePicture( View view)
{
Intent intentCamera = new Intent();
intentCamera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
File photofile = null;
try{
photofile=createImageFile();
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photofile));
startActivityForResult(intentCamera, ActivityStartamera);
//Toast.makeText(this,Uri.fromFile(photofile).toString(), Toast.LENGTH_LONG).show();
}
catch (IOException e)
{
e.printStackTrace();
Toast.makeText(this, "Error happened", Toast.LENGTH_LONG).show();
imageFileName="";
}
}
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if(requestCode == ActivityStartamera && resultCode == RESULT_OK)
{
//
//here i should add plain text to taken picture
//
Toast.makeText(this, "Picture taken", Toast.LENGTH_LONG).show();
}
else{
image.delete();
}
}
File createImageFile () throws IOException{
String timeStamp = new SimpleDateFormat("MMyyyydd_HHmmss").format(new Date());
imageFileName = "picture_"+timeStamp;
storage = Environment.getExternalStoragePublicDirectory("PicturesForApp");
if (!storage.exists())
{
Toast.makeText(this, "Folder made for pictures", Toast.LENGTH_LONG).show();
storage.mkdirs();
}
image = new File(storage + "/" +imageFileName +".jpg");
return image;
}
我尝试过像this这样的东西但是没有用。
答案 0 :(得分:1)
请使用此方法我相信它会对您有所帮助。
public Bitmap addTextToImage(Bitmap src, String textToAddOnImage, int x, int y, int color, int alpha, int size, boolean underline) {
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
paint.setColor(color);
paint.setAlpha(alpha);
paint.setTextSize(size);
paint.setAntiAlias(true);
paint.setUnderlineText(underline);
canvas.drawText(textToAddOnImage, x, y, paint);
return result;
}
像这样调用此方法
// use this bitmap to show in imageView and you can also save the bitmap.
Bitmap bmp = addTextToImage(srcBitmap, "Mustanser Iqbal", 200, 200, Color.GREEN, 80, 24, false);
File f = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + fileName + ".png");
FileOutputStream fos = new FileOutputStream(f);
bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);