Android 编程技巧之 ----- 给图片添加水印



简单记录下给图片加水印的代码:


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了= =),当然还有其他方法,可以参考以下链接:

获取imageview里的Bitmap


File(图片文件) -> 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,然后解码返回。


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值