Android给图片加文字和图片水印实例代码

我们在做项目的时候有时候需要给图片添加水印,水寒今天就遇到了这样的问题,所以搞了一个工具类,贴出来大家直接调用就行。

/**
 * 图片工具类
 * @author 水寒
 *
 */
public class ImageUtil {

  /**
   * 设置水印图片在左上角
   * @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);
  }

  /**
   * 给图片添加文字到左上角
   * @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);
  }
}

使用方法如下:

添加一个布局,上面是原始图片,下面是添加水印后的图片

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <TextView
    android:id="@+id/sour_pic_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="原图" />

  <ImageView
    android:id="@+id/sour_pic"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerInside"/>

  <TextView
    android:id="@+id/watermark_pic_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="水印" />

  <ImageView
    android:id="@+id/wartermark_pic"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerInside"/>

</LinearLayout>

在Activity中获取到ImageView对象,并且获取Bitmap对象,对Bitmap进行canva绘图,添加水印:

/**
 * 图片工具类
 * @author 水寒
 *
 */
public class MainActivity extends Activity {

  private ImageView mSourImage;
  private ImageView mWartermarkImage;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initView();
  }

  private void initView(){
    mSourImage = (ImageView) findViewById(R.id.sour_pic);
    mWartermarkImage = (ImageView) findViewById(R.id.wartermark_pic);
    Bitmap sourBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sour_pic);
    mSourImage.setImageBitmap(sourBitmap);

    Bitmap waterBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.weixin);

    Bitmap watermarkBitmap = ImageUtil.createWaterMaskCenter(sourBitmap, waterBitmap);
    watermarkBitmap = ImageUtil.createWaterMaskLeftBottom(this, watermarkBitmap, waterBitmap, 0, 0);
    watermarkBitmap = ImageUtil.createWaterMaskRightBottom(this, watermarkBitmap, waterBitmap, 0, 0);
    watermarkBitmap = ImageUtil.createWaterMaskLeftTop(this, watermarkBitmap, waterBitmap, 0, 0);
    watermarkBitmap = ImageUtil.createWaterMaskRightTop(this, watermarkBitmap, waterBitmap, 0, 0);

    Bitmap textBitmap = ImageUtil.drawTextToLeftTop(this, watermarkBitmap, "左上角", 16, Color.RED, 0, 0);
    textBitmap = ImageUtil.drawTextToRightBottom(this, textBitmap, "右下角", 16, Color.RED, 0, 0);
    textBitmap = ImageUtil.drawTextToRightTop(this, textBitmap, "右上角", 16, Color.RED, 0, 0);
    textBitmap = ImageUtil.drawTextToLeftBottom(this, textBitmap, "左下角", 16, Color.RED, 0, 0);
    textBitmap = ImageUtil.drawTextToCenter(this, textBitmap, "中间", 16, Color.RED);

    mWartermarkImage.setImageBitmap(textBitmap);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android 图片添加水印的实现方法

    Android 图片添加水印的实现方法 实现效果图: 手机端打水印(文字和图片)使用的是Bitmap.Matrix和Canvas类的一些方法, 可以实现拉伸.旋转.位移等等效果. 原理很简单, 就是在画布Canvas上绘制图形.图片.文字等等, 得到你想要的效果图片. 百度搜索图片打水印有很多结果, 没找到斜着打水印的代码,有很多公司都要求上图的效果, 所以写着玩玩. /* 添加全屏斜着45度的文字 / public static Bitmap drawCenterLable(Context c

  • Android 给图片加上水印的示例代码(支持logo+文字)

    本文介绍了Android 给图片加上水印的示例代码(支持logo+文字),分享给大家,具体如下: 现在我们想要往图片上打上水印,该水印应符合这样的需求的: 支持logo+文字: 文字信息支持多行展示: 用户可以选择水印在图片上的生成位置(左上.右上.右下和左下). 粗略的结构图低配版大概就长这样... 水印结构图.png 现在提供这样的一种思路去实现这一个需求,我们可以通过自定义一个view,view的布局中包含logo.公司名称和相关信息,这个view就是我们要打上图片的水印. 这样的一个vi

  • Android实现为图片添加水印

    添加水印的方法挺简单的,具体内容如下 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView iv = (ImageView) findViewByI

  • Python 实现给图片加文字或logo水印

    目录 前言 环境依赖 代码 验证一下 执行结果 前言 本文提供给图片添加文字或者logo图片水印的python工具,打造专属图片. 环境依赖 ffmpeg环境安装,ffmpy安装: pip install ffmpy -i https://pypi.douban.com/simple 代码 上代码. #!/user/bin/env python # coding=utf-8 """ @project : csdn @author : 剑客阿良_ALiang @file : i

  • android中图片加载到内存的实例代码

    本文演示android中图片加载到内存 首先设计界面: 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="matc

  • 随时给自己贴的图片加文字的php水印

    随时给自己贴的图片加文字  <?  Header( "Content-type: image/jpeg");  function makethumb($srcFile,$text,$size=12,$R=0,$G=0,$B=0) {  if(!$text){  $text='welcome xs.net.ru xayle';  $size=20;  $R=255;  }  $data = GetImageSize($srcFile,&$info);  switch ($d

  • Android给图片加文字和图片水印实例代码

    我们在做项目的时候有时候需要给图片添加水印,水寒今天就遇到了这样的问题,所以搞了一个工具类,贴出来大家直接调用就行. /** * 图片工具类 * @author 水寒 * */ public class ImageUtil { /** * 设置水印图片在左上角 * @param Context * @param src * @param watermark * @param paddingLeft * @param paddingTop * @return */ public static Bi

  • Android自定义实现图片加文字功能

    Android自定义实现图片加文字功能 分四步来写: 1,组合控件的xml; 2,自定义组合控件的属性; 3,自定义继承组合布局的class类,实现带两参数的构造器; 4,在xml中展示组合控件. 具体实现过程: 一.组合控件的xml 我接触的有两种方式,一种是普通的Activity的xml:一种是父节点为merge的xml.我项目中用的是第一种,但个人感觉第二种好,因为第一种多了相对或者绝对布局层. 我写的 custom_pictext.xml <?xml version="1.0&qu

  • php给图片加文字水印

    注释非常的详细了,这里就不多废话了 <?php /*给图片加文字水印的方法*/ $dst_path = 'http://f4.topitme.com/4/15/11/1166351597fe111154l.jpg'; $dst = imagecreatefromstring(file_get_contents($dst_path)); /*imagecreatefromstring()--从字符串中的图像流新建一个图像,返回一个图像标示符,其表达了从给定字符串得来的图像 图像格式将自动监测,只要

  • Android实现自定义带文字和图片Button的方法

    本文实例讲述了Android实现自定义带文字和图片Button的方法.分享给大家供大家参考.具体分析如下: 在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就是利用系统自带的Button来实现,这种方式代码量最小.在Button的属性中有一个是drawableLeft,这个属性可以把图片设置在文字的左边,但是这种方式必须让icon的背景色是透明的,如果icon的背景色不是透明的话,会导致点击按钮时i

  • Android 自定义 HorizontalScrollView 打造多图片OOM 的横向滑动效果(实例代码)

    自从Gallery被谷歌废弃以后,Google推荐使用ViewPager和HorizontalScrollView来实现Gallery的效果.的确HorizontalScrollView可以实现Gallery的效果,但是HorizontalScrollView存在一个很大的问题,如果你仅是用来展示少量的图片,应该是没问题的,但是如果我希望HorizontalScrollView可以想ViewPager一样,既可以绑定数据集(动态改变图片),还能做到,不管多少图片都不会OOM(ViewPager内

  • Android 中图片和按钮按下状态变化实例代码解析

    1.图片设置背景选择器,以便点按或设置选中与否,背景切换 res/drawable/selector_settings_item_back.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused

  • webuploader 实现图片批量上传功能附实例代码

    1.导入资源 2.JSP代码 <div class="page-container"> <div class="row cl"> <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>项目名称:</label> <div class="formCont

随机推荐