Android 添加水印图片

    /**
     * 给图片加水印
     *  水印位置根据服务端给的处理
     * @param src       原图
     * @param watermark 水印
     * @return 加水印的原图
     */
    public  Bitmap WaterMask(Bitmap src, Bitmap watermark) {
        int w = src.getWidth();
        int h = src.getHeight();
        Log.i("WaterMask", "原图宽: "+w);
        Log.i("WaterMask", "原图高: "+h);
        // 设置原图想要的大小
        float newWidth = UIUtils.getScreenWidth(ShowPdfActivity.this);
        float newHeight = h*(newWidth/w);
        // 计算缩放比例
        float scaleWidth = ( newWidth) / w;
        float scaleHeight = (newHeight) / h;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        src = Bitmap.createBitmap(src, 0, 0, w, h, matrix, true);

        //根据bitmap缩放水印图片
        float w1 = w / 5;
        float h1 = (float) (w1 / 5);
        //获取原始水印图片的宽、高
        int w2 = watermark.getWidth();
        int h2 = watermark.getHeight();

        //计算缩放的比例
        float scalewidth = ((float) w1) / w2;
        float scaleheight = ((float) h1) / h2;

        Matrix matrix1 = new Matrix();
        matrix1.postScale((float) 0.2, (float) 0.2);

        watermark = Bitmap.createBitmap(watermark, 0, 0, w2, h2, matrix1, true);
        //获取新的水印图片的宽、高
        w2 = watermark.getWidth();
        h2 = watermark.getHeight();

        Bitmap result = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
        Canvas cv = new Canvas(result);
        //在canvas上绘制原图和新的水印图
        cv.drawBitmap(src, 0, 0, null);
        //水印图绘制在画布的右下角,距离右边和底部都为20
//        cv.drawBitmap(watermark, src.getWidth() - w2-20, src.getHeight() - h2-20, null);
        cv.drawBitmap(watermark, newWidth-w2, 0, null);
        cv.save();
        cv.restore();

        return result;
    }

    /**
     * 给图片加水印
     *  水印位置根据服务端给的处理
     * @param src       原图
     * @param watermark 水印
     * @return 加水印的原图
     */
    public  Bitmap WaterMaskWithVertical(Bitmap src, Bitmap watermark) {
        int w = src.getWidth();
        int h = src.getHeight();
        Log.i("WaterMask", "原图宽: "+w);
        Log.i("WaterMask", "原图高: "+h);
        // 设置原图想要的大小
        float newWidth = UIUtils.getScreenWidth(ShowPdfActivity.this);
        float newHeight = h*(newWidth/w);
        // 计算缩放比例
        float scaleWidth = ( newWidth) / w;
        float scaleHeight = (newHeight) / h;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        src = Bitmap.createBitmap(src, 0, 0, w, h, matrix, true);

        //根据bitmap缩放水印图片
        float w1 = w / 1;
        float h1 = (float) (w1 / 1);
        //获取原始水印图片的宽、高
        int w2 = watermark.getWidth();
        int h2 = watermark.getHeight();

        //计算缩放的比例
        float scalewidth = ((float) w1) / w2;
        float scaleheight = ((float) h1) / h2;

        Matrix matrix1 = new Matrix();
        matrix1.postScale((float) 0.5, scaleHeight);

        watermark = Bitmap.createBitmap(watermark, 0, 0, w2, h2, matrix1, true);
        //获取新的水印图片的宽、高
        w2 = watermark.getWidth();
        h2 = watermark.getHeight();

        Bitmap result = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
        Canvas cv = new Canvas(result);
        //在canvas上绘制原图和新的水印图
        cv.drawBitmap(src, 0, 0, null);
        //水印图绘制在画布的右下角,距离右边和底部都为20
//        cv.drawBitmap(watermark, src.getWidth() - w2-20, src.getHeight() - h2-20, null);
        cv.drawBitmap(watermark, newWidth-w2, 0, null);
        cv.save();
        cv.restore();

        return result;
    }
    /**
     * 给图片加水印
     *  水印位置根据服务端给的处理
     * @param src       原图
     * @param watermark 水印
     * @return 加水印的原图
     */
    public  Bitmap WaterMaskWithVertical(Bitmap src, Bitmap watermark,Bitmap qianzhang ) {
        int w = src.getWidth();
        int h = src.getHeight();
        Log.i("WaterMask", "原图宽: "+w);
        Log.i("WaterMask", "原图高: "+h);
        // 设置原图想要的大小
        float newWidth = UIUtils.getScreenWidth(ShowPdfActivity.this);
        float newHeight = h*(newWidth/w);
        // 计算缩放比例
        float scaleWidth = ( newWidth) / w;
        float scaleHeight = (newHeight) / h;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        src = Bitmap.createBitmap(src, 0, 0, w, h, matrix, true);




        //根据bitmap缩放水印图片
        float w1 = w / 1;
        float h1 = (float) (w1 / 1);
        //获取原始水印图片的宽、高
        int w2 = watermark.getWidth();
        int h2 = watermark.getHeight();

        //计算缩放的比例
        float scalewidth = ((float) w1) / w2;
        float scaleheight = ((float) h1) / h2;

        Matrix matrix1 = new Matrix();
        matrix1.postScale((float) 0.5, scaleHeight);

        watermark = Bitmap.createBitmap(watermark, 0, 0, w2, h2, matrix1, true);
        //获取新的水印图片的宽、高
        w2 = watermark.getWidth();
        h2 = watermark.getHeight();

        Bitmap result = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
        Canvas cv = new Canvas(result);
        //在canvas上绘制原图和新的水印图
        cv.drawBitmap(src, 0, 0, null);
        //水印图绘制在画布的右下角,距离右边和底部都为20
//        cv.drawBitmap(watermark, src.getWidth() - w2-20, src.getHeight() - h2-20, null);
        cv.drawBitmap(watermark, newWidth-w2, 0, null);
        //获取原始水印图片的宽、高
        int w3 = qianzhang.getWidth();
        int h3 = qianzhang.getHeight();
        Matrix matrix2 = new Matrix();
        matrix2.postScale((float) 0.1, (float) 0.2);

        qianzhang = Bitmap.createBitmap(qianzhang, 0, 0, w3, h3, matrix2, true);
        //获取新的水印图片的宽、高
        int w4 = qianzhang.getWidth();
        int h4 = qianzhang.getHeight();
        cv.drawBitmap(qianzhang,newWidth-w2,(int)newHeight/2-(int)h4/2,null);
        cv.save();
        cv.restore();

        return result;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值