欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-21 15:13:10
我们在做项目的时候有时候需要给图片添加水印,水寒今天就遇到了这样的问题,所以搞了一个工具类,贴出来大家直接调用就行。 /** * 图片工具类 * @aut...

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

/**
 * 图片工具类
 * @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);
  }
}

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