微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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),paddingTop));
  }

  private static Bitmap createWaterMaskBitmap(Bitmap src,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,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,int paddingRight,int paddingBottom) {
    return createWaterMaskBitmap(src,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,int paddingTop) {
    return createWaterMaskBitmap( src,paddingTop));
  }

  /**
   * 设置水印图片到左下角
   * @param Context
   * @param src
   * @param watermark
   * @param paddingLeft
   * @param paddingBottom
   * @return
   */
  public static Bitmap createWaterMaskLeftBottom(
      Context context,paddingBottom));
  }

  /**
   * 设置水印图片到中间
   * @param Context
   * @param src
   * @param watermark
   * @return
   */
  public static Bitmap createWaterMaskCenter(Bitmap src,Bitmap watermark) {
    return createWaterMaskBitmap(src,(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 paddingTop) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(color);
    paint.setTextSize(dp2px(context,size));
    Rect bounds = new Rect();
    paint.getTextBounds(text,text.length(),bounds);
    return drawTextToBitmap(context,bitmap,text,paint,bounds,paddingTop) + bounds.height());
  }

  /**
   * 绘制文字到右下角
   * @param context
   * @param bitmap
   * @param text
   * @param size
   * @param color
   * @param paddingLeft
   * @param paddingTop
   * @return
   */
  public static Bitmap drawTextToRightBottom(Context context,int paddingBottom) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(color);
    paint.setTextSize(dp2px(context,bitmap.getWidth() - bounds.width() - dp2px(context,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,paddingTop) + bounds.height());
  }

  /**
   * 绘制文字到左下方
   * @param context
   * @param bitmap
   * @param text
   * @param size
   * @param color
   * @param paddingLeft
   * @param paddingBottom
   * @return
   */
  public static Bitmap drawTextToLeftBottom(Context context,paddingBottom));
  }

  /**
   * 绘制文字到中间
   * @param context
   * @param bitmap
   * @param text
   * @param size
   * @param color
   * @return
   */
  public static Bitmap drawTextToCenter(Context context,int color) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(color);
    paint.setTextSize(dp2px(context,(bitmap.getWidth() - bounds.width()) / 2,(bitmap.getHeight() + bounds.height()) / 2);
  }

  //图片上绘制文字
  private static Bitmap drawTextToBitmap(Context context,Paint paint,Rect bounds,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,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,width,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);
    watermarkBitmap = ImageUtil.createWaterMaskRightBottom(this,0);
    watermarkBitmap = ImageUtil.createWaterMaskLeftTop(this,0);
    watermarkBitmap = ImageUtil.createWaterMaskRightTop(this,0);

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

    mWartermarkImage.setimageBitmap(textBitmap);
  }
}

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐