如何在 android 中拍摄的照片中添加时间戳
how can I add timestamp in captured photo in android
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateTime = sdf.format(Calendar.getInstance().getTime());
Bundle extras = data.getExtras();
Bitmap src= (Bitmap) extras.get("data");
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);
canvas.drawText(dateTime , 0, 0, null);
click_image_id.setImageBitmap(result);
}
}
它会崩溃,如果我更改并评论 canvas 实现,例如
//
Bundle extras = data.getExtras();
Bitmap src= (Bitmap) extras.get("data");
click_image_id.setImageBitmap(src);
它执行视图,但我需要图像中的时间戳,需要专家帮助
您的问题是您需要 drawText()
需要的 Paint
对象,它不能为空。这里的代码将解决您的 NPE。
编辑:
第二个问题是绘制文本的位置,复制并粘贴此解决方案。它会在左下角放置一个带有日期的黑色文本。
如果要自定义文本,请自定义Paint
对象。我检查了这段代码,它工作正常。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateTime = sdf.format(Calendar.getInstance().getTime());
Bundle extras = data.getExtras();
Bitmap src= (Bitmap) extras.get("data");
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);
canvas.drawText(dateTime , 0, h, new Paint());
click_image_id.setImageBitmap(result);
}
}
您需要创建和配置一个 Paint
对象并在 canvas.drawText()
中使用它。尝试这样的事情(摘自 this 文章)。
private fun drawTextToBitmap(bitmap: Bitmap, textSize: Int = 78, text: String): Bitmap {
val canvas = Canvas(bitmap)
// new antialised Paint - empty constructor does also work
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
paint.color = Color.BLACK
// text size in pixels
val scale = resources.displayMetrics.density
paint.textSize = (textSize * scale).roundToInt().toFloat()
//custom fonts or a default font
val fontFace = ResourcesCompat.getFont(context, R.font.acrobat)
paint.typeface = Typeface.create(fontFace, Typeface.NORMAL)
// draw text to the Canvas center
val bounds = Rect()
//draw the text
paint.getTextBounds(text, 0, text.length, bounds)
//x and y defines the position of the text, starting in the top left corner
canvas.drawText(text, x, y, paint)
return bitmap
}
最后这段代码可以正常工作了感谢@ASP 和@BogdonAndroid
我检查了 solution
public static Bitmap mark(Bitmap src, boolean underline) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateTime = sdf.format(Calendar.getInstance().getTime());
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
Paint tPaint = new Paint();
tPaint.setTextSize(15);
tPaint.setColor(Color.BLUE);
tPaint.setStyle(Paint.Style.FILL);
float height = tPaint.measureText("yY");
canvas.drawBitmap(src,0,0,null);
canvas.drawText(dateTime, 20f, height+15f, tPaint);
return result;}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateTime = sdf.format(Calendar.getInstance().getTime());
Bundle extras = data.getExtras();
Bitmap src= (Bitmap) extras.get("data");
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);
canvas.drawText(dateTime , 0, 0, null);
click_image_id.setImageBitmap(result);
}
}
它会崩溃,如果我更改并评论 canvas 实现,例如 //
Bundle extras = data.getExtras();
Bitmap src= (Bitmap) extras.get("data");
click_image_id.setImageBitmap(src);
它执行视图,但我需要图像中的时间戳,需要专家帮助
您的问题是您需要 drawText()
需要的 Paint
对象,它不能为空。这里的代码将解决您的 NPE。
编辑:
第二个问题是绘制文本的位置,复制并粘贴此解决方案。它会在左下角放置一个带有日期的黑色文本。
如果要自定义文本,请自定义Paint
对象。我检查了这段代码,它工作正常。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateTime = sdf.format(Calendar.getInstance().getTime());
Bundle extras = data.getExtras();
Bitmap src= (Bitmap) extras.get("data");
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);
canvas.drawText(dateTime , 0, h, new Paint());
click_image_id.setImageBitmap(result);
}
}
您需要创建和配置一个 Paint
对象并在 canvas.drawText()
中使用它。尝试这样的事情(摘自 this 文章)。
private fun drawTextToBitmap(bitmap: Bitmap, textSize: Int = 78, text: String): Bitmap {
val canvas = Canvas(bitmap)
// new antialised Paint - empty constructor does also work
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
paint.color = Color.BLACK
// text size in pixels
val scale = resources.displayMetrics.density
paint.textSize = (textSize * scale).roundToInt().toFloat()
//custom fonts or a default font
val fontFace = ResourcesCompat.getFont(context, R.font.acrobat)
paint.typeface = Typeface.create(fontFace, Typeface.NORMAL)
// draw text to the Canvas center
val bounds = Rect()
//draw the text
paint.getTextBounds(text, 0, text.length, bounds)
//x and y defines the position of the text, starting in the top left corner
canvas.drawText(text, x, y, paint)
return bitmap
}
最后这段代码可以正常工作了感谢@ASP 和@BogdonAndroid
我检查了 solution
public static Bitmap mark(Bitmap src, boolean underline) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateTime = sdf.format(Calendar.getInstance().getTime());
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
Paint tPaint = new Paint();
tPaint.setTextSize(15);
tPaint.setColor(Color.BLUE);
tPaint.setStyle(Paint.Style.FILL);
float height = tPaint.measureText("yY");
canvas.drawBitmap(src,0,0,null);
canvas.drawText(dateTime, 20f, height+15f, tPaint);
return result;}