android - 从 URL 向图像添加水印并分享
问题描述
我想在共享之前从我的可绘制对象中添加水印图像(我的徽标),以便共享的图像具有半透明水印。
到目前为止,我只能使用以下代码使用共享意图从 URL 共享图像:
public void shareItem(String url) {
Picasso.with(getApplicationContext()).load(url).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent, "Share with"));
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}
public Uri getLocalBitmapUri(Bitmap bmp) {
Uri bmpUri = null;
try {
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
解决方案
您可以在我的代码中使用您的 uri 作为源,并为输出文件(带水印)创建新的 uri,以便通过意图共享
这是调用代码
String address = Environment.getExternalStorageDirectory().getAbsolutePath();
Bitmap source = BitmapFactory.decodeResource(getResources(), R.drawable.birds); // the original file is cuty.jpg i added in resources
Bitmap dest = addWatermark(source);
try {
dest.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(address + "/output.jpg")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
这种方法将完成漂亮的工作。
public Bitmap addWatermark( Bitmap source) {
int width, height;
Canvas canvas;
Paint paint;
Bitmap bitmap, watermark;
Matrix matrix;
float scale;
RectF rectF;
width = source.getWidth();
height = source.getHeight();
// Create a new bitmap file and draw it on the canvas
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);
canvas = new Canvas(bitmap);
canvas.drawBitmap(source, 0, 0, paint);
// now ready your watermark from the resources
watermark = BitmapFactory.decodeResource(getResources(), R.drawable.apple);
// scale / adjust height of your logo/watermark
// i am scaling it down to 30%
scale = (float) (((float) height * 0.30) / (float) watermark.getHeight());
// now create the matrix
matrix = new Matrix();
matrix.postScale(scale, scale);
// Determine the post-scaled size of the watermark
rectF = new RectF(0, 0, watermark.getWidth(), watermark.getHeight());
matrix.mapRect(rectF);
// below method will decide the position of the logo on the image
//for right bottom corner use below line
// matrix.postTranslate(width - rectF.width(), height - rectF.height());
// i am going to add it my logo at the top left corner
matrix.postTranslate(15,15);
// set alpha/opacity of paint which is going to draw watermark
paint.setAlpha(60);
// now draw the watermark on the canvas
canvas.drawBitmap(watermark, matrix, paint);
//cleaning up the memory
watermark.recycle();
// now return the watermarked image to the calling location
return bitmap;
}
在左上角应用 60% alpha 水印后的图像
推荐阅读
- c# - 在 Api 的 Ok ()(状态 200)中返回 json 的最佳方法是什么?
- android - Cloud Firebase 列表反转
- javascript -
- bing - How can i re-create bing's webmaster verifiction DNS TXT record for an already verified domain?
- java - 将数据从 SOAP 处理程序传递到 Web 服务服务器类
- docker - How to add a Google static IP address to Linux and make it work with docker
- amazon-web-services - AWS Cloud Watch Alarm script
- ios - SwiftUI: How do you dismiss a sheet and launch a different sheet from ContentView?
- python - Python: Turn my dictionary into literal string
- node.js - 如何在带有 supertest、passport 和 JEST 的 cookie 中使用 jwt 测试身份验证