//拍照
private void fromCarema(OnFilishedListener listener) {
if (hasSdcard()) {
String path = null;
if ( tempFile ==null || !tempFile.exists()) {
listener.onFilish(null);
return;
}
path = tempFile.getAbsolutePath();
if (TextUtils.isEmpty(path)) {
listener.onFilish(null);
return;
}
try {
Bitmap bitmap = BitmapUtils.scaleBitmap(path);
//+++++++++++++++++++画日期操作
bitmap = BitmapUtils.drawDate2Bitmap(bitmap);
File file = new File(path);
if (file.exists()) {
file.delete();
}
path = BitmapUtils.saveBitmap(bitmap);
listener.onFilish(path);
} catch (Exception e) {
e.printStackTrace();
Log.e(“保存图片”, “图片保存失败”);
listener.onFilish(null);
}
} else {
if (fragment != null) {
Toast.makeText(fragment.getActivity(), “未找到存储卡,无法存储照片!”, Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(activity, “未找到存储卡,无法存储照片!”, Toast.LENGTH_SHORT).show();
}
listener.onFilish(null);
}
}
public static Bitmap drawDate2Bitmap(Bitmap bitmap) {
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm”, Locale.CHINA);
String date = sdf.format(new Date());
Bitmap.Config bitmapConfig = bitmap.getConfig();
// set default bitmap config if none
if (bitmapConfig == null) {
bitmapConfig = Bitmap.Config.ARGB_8888;
}
bitmap = bitmap.copy(bitmapConfig, true); // 获取可改变的位图
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】 (Color.RED);
// text size in pixels
paint.setTextSize(30);
// text shadow
// paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
Rect bounds = new Rect();
paint.getTextBounds(date, 0, date.length(), bounds);
int x = (bitmap.getWidth() - bounds.width());
canvas.drawText(date, x - 10, bitmap.getHeight() - 10, paint);
canvas.save();
return bitmap;
}
2.如果是从图库选择的照片,我们需要先获取照片拍摄日期,然后再将日期画上去,代码如下:
//从图库选择
private void fromGallery(Intent data, OnFilishedListener listener) {
Uri uri = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA , MediaStore.Images.Media.DATE_TAKEN};
Cursor cursor = null;
if (uri == null)
return;
if (uri.getScheme().contains(“file”)) {
Long fileTime = (new File(uri.getPath())).lastModified();
String dateTime = TimeUtil.longToDate1(fileTime);
Log.i(“wtt”,"照片拍摄日期为dateTime: " + dateTime);
saveSelectPic( dateTime , uri.getPath(), listener);
} else if (uri.getScheme().contains(“content”)) {
if (fragment != null) {
cursor = fragment.getActivity().getContentResolver()
.query(uri, filePathColumn, null, null, null);
} else {
cursor = activity.getContentResolver().
query(uri,filePathColumn, null, null, null);
}
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
int dateIndex = cursor.getColumnIndexOrThrow(filePathColumn[1]);
String date = cursor.getString(dateIndex);
if (TextUtils.isEmpty(date)) {
date = TimeUtil.getStringDate1();
Android 添加照片拍摄日期到图片
这篇博客介绍了如何在Android中从相机或图库获取照片后,在图片上添加拍摄日期。通过`fromCamera()`方法拍摄照片,并使用`drawDate2Bitmap()`在Bitmap上绘制日期。对于从图库选择的照片,通过查询MediaStore获取拍摄日期。最后,将日期和图片路径传递给`saveSelectPic()`进行保存。
511

被折叠的 条评论
为什么被折叠?



