简单记录下给图片加水印的代码:
public Bitmap addWaterMask(Bitmap src, long time, int resolution) {
if (src == null) {
return null;
}
int w = src.getWidth();
int h = src.getHeight();
//需要处理图片太大造成的内存超过的问题,这里我的图片很小所以不写相应代码了
Bitmap newb = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);// 创建一个新的和src宽度高度一样的位图
Canvas cv = new Canvas(newb);
cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src
//加入文字
Typeface font = Typeface.create("宋体", Typeface.NORMAL);
TextPaint textPaint = new TextPaint();
textPaint.setColor(Color.WHITE);
textPaint.setTypeface(font);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
String mask = sdf.format(System.currentTimeMillis());
if (time > 0) {
mask = sdf.format(time);
}
if (resolution == 240) {
textPaint.setTextSize(10);
cv.drawText(mask, w - 120, h - 14, textPaint);
} else if (resolution == 720) {
textPaint.setTextSize(26);
cv.drawText(mask, w - 310, h - 36, textPaint);
} else if (resolution == 1088) {
textPaint.setTextSize(40);
cv.drawText(mask, w - 460, h - 56, textPaint);
} else {
textPaint.setTextSize(62);
cv.drawText(mask, w - 720, h - 84, textPaint);
}
//这里是自动换行的
// StaticLayout layout = new StaticLayout(mask, textPaint, w, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);
// layout.draw(cv);
cv.save(Canvas.ALL_SAVE_FLAG);// 保存
cv.restore();// 存储
return newb;
}
src - 需要加水印的Bitmap
time - 加时间水印所需要的时间(其他样式类似)
resolution - 图片分辨率(可用于判断水印大小和位置)
可以看到其实加水印的处理流程还是比较简单的,不过是将Bitmap加载到Canvas中,
然后用TextPaint(或者其他画笔)通过draw方法画上去,最后保存返回。其中注意点:
注释中也说了,图片过大需要做防止内存溢出的操作,这是需要注意的,可做缓存或者
其他类似的处理。(Canvas - 画布跟画家,Paint - 画笔,Bitmap - 要画的信息区域)
这里参数是bitmap,可能需要一些简单的转换方法,毕竟很多时候图片都是以文件或者
ImageView(Drawable等)形式存在的,这里罗列下:
ImageView -> Bitmap @@@
Bitmap image = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
可以看到这里先从ImageView得到Drawable,再得到Bitmap(这下也清楚如何将
Drawable转换为Bitmap了= =),当然还有其他方法,可以参考以下链接:
public Bitmap getBitmap(File f, int width) {
if (!f.exists()) {
return null;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; /* just decode bound , no memory use */
BitmapFactory.decodeFile(f.getPath(), options); /* get image info to options */
float realWidth = options.outWidth; /* here is the original size of this image */
float realHeight = options.outHeight;
float scale = 1;
if (width > 0) scale = realWidth / width;
if (scale <= 0)
scale = 1;
options.inSampleSize = (int) scale;
options.inJustDecodeBounds = false; /* will really product an image , would use memory */
return BitmapFactory.decodeFile(f.getPath(), options);
}
上面给出了图片文件转换为Bitmap的方法,参数说明:
f - 需要转换的图片源文件
width - 需要压缩的Bitmap宽度(这里只是通过宽度来判断scale来压缩,当然,高度也行)
options.inJustDecodeBounds = true;
设置的inJustDecodeBounds得值为true有何作用,看源码注释:
大致意思就是设置为true后,最主要为了解码器后续解码时无需为图片的像素分配内存,
通俗点讲就是为了得到图片的其他额外信息,而非图片的像素细节。
BitmapFactory.decodeFile(f.getPath(), options);
通过decodeFile即把图片文件的宽高等信息读入options中
options.inSampleSize = (int) scale;
这里设置要解码的Bitmap的scale,通过真实图片的width跟传入的width参数决定
options.inJustDecodeBounds = false; /* will really product an image , would use memory */
return BitmapFactory.decodeFile(f.getPath(), options);
可以看到需要真正从文件解码到Bitmap时就需要像素信息了,将inJustDecodeBounds
置为了false,然后解码返回。