private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap photo = BitmapFactory.decodeFile(uri, options);
int width = photo.getWidth();
int height = photo.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);//建立一个空的Bitmap
Canvas canvas = new Canvas(bitmap);
Paint photoPaint = new Paint(); //建立画笔
photoPaint.setDither(true); //获取跟清晰的图像采样
photoPaint.setFilterBitmap(true);//过滤一些
Rect src = new Rect(0, 0, width, height);//创建一个指定的新矩形的坐标
Rect dst = new Rect(0, 0, width, height);//创建一个指定的新矩形的坐标
canvas.drawBitmap(photo, src, dst, photoPaint);
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//设置画笔
textPaint.setTextSize(width / 20);//字体大小
textPaint.setTypeface(Typeface.DEFAULT);//采用默认的宽度
textPaint.setColor(Color.RED);//采用的颜色
canvas.drawText("保单号:" + policyNum, 10, height - 100, textPaint);
canvas.drawText("经度:" + String.format("%.6f", longitude) + "\t纬度:" + String.format("%.6f", latitude), 10, height - 70, textPaint);
canvas.drawText("时间:" + sdf.format(new Date(System.currentTimeMillis())), 10, height - 40, textPaint);
canvas.save(Canvas.ALL_SAVE_FLAG);//保存
canvas.restore();//存储
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.PNG, 0, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
}
}
}