android实现图片加水印
16lz
2021-01-26
最近做了一个图片添加水印的功能:传入的参数为:原图的bitmap,水印的字符串,以及context。
原图的bitmap可以先取得图片的文件输入流fis,之后通过 Bitmap bm = BitmapFactory.decodeStream(fis, null, opt); 方法来取得。
添加水印的方法如下:
public Bitmap addWaterMark(Bitmap src, String water, Context context){Bitmap tarBitmap = src.copy(Config.ARGB_8888, true);int w = tarBitmap.getWidth();int h = tarBitmap.getHeight();Canvas canvas = new Canvas(tarBitmap);//启用抗锯齿和使用设备的文本字距 Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//字体的相关设置textPaint.setTextSize(100.0f);//字体大小 textPaint.setTypeface(Typeface.DEFAULT_BOLD);textPaint.setColor(Color.YELLOW);textPaint.setShadowLayer(3f, 1, 1,context.getResources().getColor(android.R.color.background_dark));//图片上添加水印的位置,这里设置的是中下部3/4处canvas.drawText(water, w/2-50, (float) (h*0.75), textPaint);canvas.save(Canvas.ALL_SAVE_FLAG); canvas.restore(); return tarBitmap;}
更多相关文章
- Android(安卓)使用OPENCV实现图像实时对比
- Android7.0添加快捷方式(Shortcut)到手机桌面
- android String.xlm中使用emoji表情的方法
- Android(安卓)Action Bar学习(一)--基本介绍及使用
- 软件开发文档(以太网)
- android Fragment开发文档翻译 - 1
- Android快速开发框架dyh详解(四)---数据层的使用
- android listview嵌套gridview动态增加gridview的item项
- 第一章:初入Android大门(不同的Activity之间的数据传递与回调--s