Android给图片加文字和图片水印
来源:互联网 发布:淘宝马云怎么赚钱 编辑:程序博客网 时间:2024/06/14 19:29
前两天遇到这个功能需求,在网上查询到了一些资料,特意记录一下:给图片添加水印的基本思路都是载入原图,添加文字或者载入水印图片,保存图片这三个部分
添加水印图片1:
private Bitmap createWaterMaskImage(Context gContext, Bitmap src, Bitmap watermark) { String tag = "createBitmap"; Log.d(tag, "create a new bitmap"); if (src == null) { return null; } int w = src.getWidth(); int h = src.getHeight(); int ww = watermark.getWidth(); int wh = watermark.getHeight(); // create the new blank bitmap Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图 Canvas cv = new Canvas(newb); // draw src into cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src // draw watermark into cv.drawBitmap(watermark, 20, 20, null);// 在src的右下角画入水印 // save all clip cv.save(Canvas.ALL_SAVE_FLAG);// 保存 // store cv.restore();// 存储 return newb; }
添加水印图片2:
/** * 设置水印图片在左上角 * @param Context * @param src * @param watermark * @param paddingLeft * @param paddingTop * @return */ public static Bitmap createWaterMaskLeftTop( Context context, Bitmap src, Bitmap watermark, int paddingLeft, int paddingTop) { return createWaterMaskBitmap(src, watermark, dp2px(context, paddingLeft), dp2px(context, paddingTop)); } private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark, int paddingLeft, int paddingTop) { if (src == null) { return null; } int width = src.getWidth(); int height = src.getHeight(); //创建一个bitmap Bitmap newb = Bitmap.createBitmap(width, height, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图 //将该图片作为画布 Canvas canvas = new Canvas(newb); //在画布 0,0坐标上开始绘制原始图片 canvas.drawBitmap(src, 0, 0, null); //在画布上绘制水印图片 canvas.drawBitmap(watermark, paddingLeft, paddingTop, null); // 保存 canvas.save(Canvas.ALL_SAVE_FLAG); // 存储 canvas.restore(); return newb; } /** * 设置水印图片在右下角 * @param Context * @param src * @param watermark * @param paddingRight * @param paddingBottom * @return */ public static Bitmap createWaterMaskRightBottom( Context context, Bitmap src, Bitmap watermark, int paddingRight, int paddingBottom) { return createWaterMaskBitmap(src, watermark, src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight), src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom)); } /** * 设置水印图片到右上角 * @param Context * @param src * @param watermark * @param paddingRight * @param paddingTop * @return */ public static Bitmap createWaterMaskRightTop( Context context, Bitmap src, Bitmap watermark, int paddingRight, int paddingTop) { return createWaterMaskBitmap( src, watermark, src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight), dp2px(context, paddingTop)); } /** * 设置水印图片到左下角 * @param Context * @param src * @param watermark * @param paddingLeft * @param paddingBottom * @return */ public static Bitmap createWaterMaskLeftBottom( Context context, Bitmap src, Bitmap watermark, int paddingLeft, int paddingBottom) { return createWaterMaskBitmap(src, watermark, dp2px(context, paddingLeft), src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom)); } /** * 设置水印图片到中间 * @param Context * @param src * @param watermark * @return */ public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) { return createWaterMaskBitmap(src, watermark, (src.getWidth() - watermark.getWidth()) / 2, (src.getHeight() - watermark.getHeight()) / 2); }添加文字1:
public static Bitmap scaleWithWH(Bitmap src, double w, double h) { if (w == 0 || h == 0 || src == null) { return src; } else { // 记录src的宽高 int width = src.getWidth(); int height = src.getHeight(); // 创建一个matrix容器 Matrix matrix = new Matrix(); // 计算缩放比例 float scaleWidth = (float) (w / width); float scaleHeight = (float) (h / height); // 开始缩放 matrix.postScale(scaleWidth, scaleHeight); // 创建缩放后的图片 return Bitmap.createBitmap(src, 0, 0, width, height, matrix, true); } } public Bitmap drawTextToBitmap(Context gContext, int gResId, String gText) { Resources resources = gContext.getResources(); float scale = resources.getDisplayMetrics().density; Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId); bitmap = scaleWithWH(bitmap, 300*scale, 300*scale); android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig(); // set default bitmap config if none if(bitmapConfig == null) { bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; } // resource bitmaps are imutable, // so we need to convert it to mutable one bitmap = bitmap.copy(bitmapConfig, true); Canvas canvas = new Canvas(bitmap); // new antialised Paint Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // text color - #3D3D3D paint.setColor(Color.RED); paint.setTextSize((int) (18 * scale)); paint.setDither(true); //获取跟清晰的图像采样 paint.setFilterBitmap(true);//过滤一些 Rect bounds = new Rect(); paint.getTextBounds(gText, 0, gText.length(), bounds); int x = 30; int y = 30; canvas.drawText(gText, x * scale, y * scale, paint); return bitmap; }添加文字2:
/** * 给图片添加文字到左上角 * @param context * @param bitmap * @param text * @return */ public static Bitmap drawTextToLeftTop(Context context, Bitmap bitmap, String text, int size, int color, int paddingLeft, int paddingTop) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(color); paint.setTextSize(dp2px(context, size)); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); return drawTextToBitmap(context, bitmap, text, paint, bounds, dp2px(context, paddingLeft), dp2px(context, paddingTop) + bounds.height()); } /** * 绘制文字到右下角 * @param context * @param bitmap * @param text * @param size * @param color * @param paddingLeft * @param paddingTop * @return */ public static Bitmap drawTextToRightBottom(Context context, Bitmap bitmap, String text, int size, int color, int paddingRight, int paddingBottom) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(color); paint.setTextSize(dp2px(context, size)); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); return drawTextToBitmap(context, bitmap, text, paint, bounds, bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight), bitmap.getHeight() - dp2px(context, paddingBottom)); } /** * 绘制文字到右上方 * @param context * @param bitmap * @param text * @param size * @param color * @param paddingRight * @param paddingTop * @return */ public static Bitmap drawTextToRightTop(Context context, Bitmap bitmap, String text, int size, int color, int paddingRight, int paddingTop) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(color); paint.setTextSize(dp2px(context, size)); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); return drawTextToBitmap(context, bitmap, text, paint, bounds, bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight), dp2px(context, paddingTop) + bounds.height()); } /** * 绘制文字到左下方 * @param context * @param bitmap * @param text * @param size * @param color * @param paddingLeft * @param paddingBottom * @return */ public static Bitmap drawTextToLeftBottom(Context context, Bitmap bitmap, String text, int size, int color, int paddingLeft, int paddingBottom) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(color); paint.setTextSize(dp2px(context, size)); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); return drawTextToBitmap(context, bitmap, text, paint, bounds, dp2px(context, paddingLeft), bitmap.getHeight() - dp2px(context, paddingBottom)); } /** * 绘制文字到中间 * @param context * @param bitmap * @param text * @param size * @param color * @return */ public static Bitmap drawTextToCenter(Context context, Bitmap bitmap, String text, int size, int color) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(color); paint.setTextSize(dp2px(context, size)); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); return drawTextToBitmap(context, bitmap, text, paint, bounds, (bitmap.getWidth() - bounds.width()) / 2, (bitmap.getHeight() + bounds.height()) / 2); } //图片上绘制文字 private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text, Paint paint, Rect bounds, int paddingLeft, int paddingTop) { android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig(); paint.setDither(true); // 获取跟清晰的图像采样 paint.setFilterBitmap(true);// 过滤一些 if (bitmapConfig == null) { bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; } bitmap = bitmap.copy(bitmapConfig, true); Canvas canvas = new Canvas(bitmap); canvas.drawText(text, paddingLeft, paddingTop, paint); return bitmap; } /** * 缩放图片 * @param src * @param w * @param h * @return */ public static Bitmap scaleWithWH(Bitmap src, double w, double h) { if (w == 0 || h == 0 || src == null) { return src; } else { // 记录src的宽高 int width = src.getWidth(); int height = src.getHeight(); // 创建一个matrix容器 Matrix matrix = new Matrix(); // 计算缩放比例 float scaleWidth = (float) (w / width); float scaleHeight = (float) (h / height); // 开始缩放 matrix.postScale(scaleWidth, scaleHeight); // 创建缩放后的图片 return Bitmap.createBitmap(src, 0, 0, width, height, matrix, true); } } /** * dip转pix * @param context * @param dp * @return */ public static int dp2px(Context context, float dp) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dp * scale + 0.5f); }
阅读全文
0 0
- Android给图片加文字和图片水印
- Android给图片加文字和图片水印
- Android给图片加文字和图片水印
- android 给图片加文字水印
- Android-给图片加文字水印
- java给图片加水印和文字
- PHP给图片加文字水印和图片水印
- 给图片加文字水印
- java给图片加水印,文字水印
- android图片加水印,文字
- android图片加水印,文字
- android图片加水印,文字
- android 给图片加水印
- php给图片加文字水印
- CxImage给图片加水印文字
- Quartz2D - 给图片加文字水印
- PHP实现给图片加文字水印
- java给图片加文字水印
- Persistence.xml 配置说明
- Ubuntu上tensorflow设置环境变量
- Springboot 整合 Dubbo/ZooKeeper 详解 SOA 案例
- Linux Vim 配置
- python判断字符串是字母 数字 大小写
- Android给图片加文字和图片水印
- AsyncTask+PullToRefreshListView+ImageLoder+上拉加载,下拉刷新
- 分析器错误消息: 在应用程序级别以外使用注册为 allowDefinition= 'MachineToApplication' 的节是错误的解决办法
- python--插入排序
- 初学Android之PullToRefresh+listview+ViewPager
- 元数据
- 以后再看系列
- Xilinx zynq7000,zynq7k上CLOCK Distribution 调试笔记
- PCA美国城市生活质量介绍(3)