android 给图片加文字、图片水印

Stamper is a tool for stamping a pattern into a picture,it likes a watermark.

Stamper是一个给图片打水印的工具,支持图片水印和文字水印,水印位置可以任意调节。

效果图:

 

实现原理:

Stamper将原图当作画布,把水印图或者文字画入原图中。生成bitmap。

Stamper使用方法:

 

1.添加依赖

Step 1. Add the JitPack repository to your build file; Add it in your root build.gradle at the end of repositories:

	allprojects {
        repositories {
        ...
        maven { url 'https://jitpack.io' }
        }
        }

Step 2. Add the dependency

	dependencies {
	         implementation 'com.github.KernHu:Stamper:1.1'
	}

2.代码部分:

 

1.use it stamp a text;

Bitmap bitmap3 = BitmapFactory.decodeResource(getResources(), R.drawable.sample_plot_3);
Stamper.with(MainActivity.this)
      .setLabel("National Geography")
      .setLabelColor(getResources().getColor(R.color.theme))//Color.rgb(255, 60, 70)
      .setLabelSize(60)
      .setMasterBitmap(bitmap3)
      .setStampType(StampType.TEXT)
      .setStampPadding(new StampPadding(bitmap3.getWidth() / 4, bitmap3.getHeight() / 6))
      .setStampWatcher(mStampWatcher)
      .setRequestId(1001)
      .build();

2.use it stamp a image;

Bitmap bitmap4 = BitmapFactory.decodeResource(getResources(), R.drawable.sample_plot_4);
Bitmap watermark = BitmapFactory.decodeResource(getResources(), R.drawable.ic_watermark);
Stamper.with(MainActivity.this)
      .setMasterBitmap(bitmap4)
      .setWatermark(watermark)
      .setStampType(StampType.IMAGE)
      .setStampPadding(new StampPadding(bitmap4.getWidth() - watermark.getWidth() - 40, 40))
      .setStampWatcher(mStampWatcher)
      .setRequestId(1002)
      .build();

3.the callback

StampWatcher mStampWatcher = new StampWatcher() {
        @Override
        protected void onSuccess(Bitmap bitmap, int requestId) {
            super.onSuccess(bitmap, requestId);
			
            switch (requestId) {

                case 1001:
                    //the result of text stamper

                    break;
                case 1002:
                    //the result of image stamper

                    break;
            }
        }

        @Override
        protected void onError(String error, int requestId) {
            super.onError(error, requestId);

            switch (requestId) {

                case 1001://

                    break;
                case 1002://

                    break;
            }
        }
    };

 

完成代码见github:

https://github.com/KernHu/Stamper

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值