public static void watermark(String filename) {
try {
String watermark=FilepathConstants.CASEATTACHMENT + File.separator + "watermark.png";
BitmapFactory.Options oldOpts = new BitmapFactory.Options();
oldOpts.inSampleSize = 4;
oldOpts.inJustDecodeBounds = false;
Bitmap oriBmp = BitmapFactory.decodeFile(filename,oldOpts);
Bitmap rs=doodle(oriBmp,makeTextBitMap());
File destFile = new File(filename);
destFile.createNewFile();
OutputStream os = new FileOutputStream(destFile);
// 存储
rs.compress(CompressFormat.JPEG, 100, os);
// 关闭流
os.close();
} catch (Exception e) {
Log.d("watermark", "水印错误");
}
}
/**
* 组合图片和源图片
* @param src 源图片
* @param watermark 涂鸦图片
* @return
*/
public static Bitmap doodle(Bitmap src, Bitmap watermark)
{
// 另外创建一张图片
Bitmap newb = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
Canvas canvas = new Canvas(newb);
canvas.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入原图片src
//canvas.drawBitmap(watermark, (src.getWidth() - watermark.getWidth()) / 2, (src.getHeight() - watermark.getHeight()) / 2, null); // 涂鸦图片画到原图片中间位置
canvas.drawBitmap( watermark, src.getWidth() - watermark.getWidth() + 5, src.getHeight() - watermark.getHeight() + 5, null );//在src的右下角画入水印
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
watermark.recycle();
watermark = null;
return newb;
}
public static Bitmap makeTextBitMap(){
int w = 360,h = 140;
String mstrTitle =“这里是你要加的水印文字这里是你要加的水印文字这里是你要加的水印文字这里是你要加的水印文字这里是你要加的水印文字这里是你要加的水印文字”;
;
Bitmap mbmpTest = Bitmap.createBitmap(w,h, Config.ARGB_8888);
Canvas canvasTemp = new Canvas(mbmpTest);
canvasTemp.drawColor(Color.TRANSPARENT);
Paint p = new Paint();
String familyName = "宋体";
Typeface font = Typeface.create(familyName,Typeface.BOLD);
p.setColor(Color.RED);
p.setTypeface(font);
p.setTextSize(22);
canvasTemp.drawText(mstrTitle,0,100,p);
return mbmpTest;
}