android 给图片加水印

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LZQ19931007/article/details/51131263

生成水印照片类


package com.chinawit.cwandroidbase.Infrastructure.CWComponent.CWImage.WaterMark;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;

/**
 * Created by lenovo on 2016/3/15.
 */
public class WaterMarkUtil {
    private float imageDrawFormX, imageDrawFormY;
    private float imageOffsetX,imageOffsetY;
    private int textColor, textSize;
    private Context context;

    public WaterMarkUtil() {
        imageDrawFormX = 0;
        imageDrawFormY = 0;
        imageOffsetX= 0;
        imageOffsetY= 0;
        textColor = Color.BLUE;
        textSize = 15;
    }

    public WaterMarkUtil(Context context) {
        this.context = context;
        imageDrawFormX = 0;
        imageDrawFormY = 0;
        imageOffsetX= 0;
        imageOffsetY= 0;
        textColor = Color.BLUE;
        textSize = 15;
    }

    public void setImageDrawXY(float imageDrawFormX, float imageDrawFormY) {
        this.imageDrawFormX = imageDrawFormX;
        this.imageDrawFormY = imageDrawFormY;
    }

    public void setImageOffsetXY(float imageOffsetX,float imageOffsetY){
        this.imageOffsetX = imageOffsetX;
        this.imageOffsetY = imageOffsetY;
    }
    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    // add waterMark
    public Bitmap addWaterMarkByImageAndWord(Bitmap src, Bitmap mark, String title) {
        if (src == null) {
            return null;
        }
        int srcWidth = src.getWidth();
        int srcHeight = src.getHeight();
        //create the new blank bitmap
        Bitmap newbitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newbitmap);
        //draw src into
        canvas.drawBitmap(src, imageDrawFormX, imageDrawFormY, null);
        Paint paint = new Paint();
        if (mark != null) {
            int markWidth = mark.getWidth();
            int markHeight = mark.getHeight();
            paint.setAlpha(50);
            //draw watermark into
            canvas.drawBitmap(mark,imageOffsetX,imageOffsetY, null);
        }
        if (title != null) {
            String familyName = "宋体";
            Typeface font = Typeface.create(familyName, Typeface.BOLD);
            TextPaint textPaint = new TextPaint();
            textPaint.setColor(textColor);
            textPaint.setTypeface(font);
            textPaint.setTextSize(textSize);
            textPaint.setAntiAlias(true);
            StaticLayout layout = new StaticLayout(title, textPaint, srcWidth, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);
            layout.draw(canvas);
        }
        //save all clip
        canvas.save(Canvas.ALL_SAVE_FLAG);
        //store
        canvas.restore();
        return newbitmap;
    }
}

实例使用

package com.chinawit.cwandroidbase.SampleImage;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;

import com.chinawit.cwandroidbase.Infrastructure.CWComponent.CWImage.WaterMark.WaterMarkUtil;
import com.chinawit.cwandroidbase.R;

public class WaterMarkActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_water_mark);
        Bitmap src = BitmapFactory.decodeResource(getResources(), R.mipmap.main_bg);
        int w = src.getWidth();
        int h = src.getHeight();
        float scale = (float) (1000000.0/(w*h));
        Matrix matrix = new Matrix();
        matrix.setScale(scale, scale);
        Bitmap src1 = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);

        src.recycle();

        Bitmap water = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

        WaterMarkUtil waterMarkUtil = new WaterMarkUtil();
        waterMarkUtil.setTextSize(50);
        waterMarkUtil.setImageOffsetXY(w/2-water.getWidth(), h/2-water.getHeight());
        src1 = waterMarkUtil.addWaterMarkByImageAndWord(src1,water,"Aeric waterMark test");

        ImageView imageView = (ImageView)findViewById(R.id.imageView);
        imageView.setImageBitmap(src1);
    }


}

展开阅读全文

没有更多推荐了,返回首页