Android 从URL向图像添加水印并共享
在共享之前,我想从我的绘图设备中添加一个水印图像(我的徽标),以便共享的图像具有半透明水印 到目前为止,我只能使用以下代码共享URL中的图像:Android 从URL向图像添加水印并共享,android,Android,在共享之前,我想从我的绘图设备中添加一个水印图像(我的徽标),以便共享的图像具有半透明水印 到目前为止,我只能使用以下代码共享URL中的图像: public void shareItem(String url) { Picasso.with(getApplicationContext()).load(url).into(new Target() { @Override public void onBitmapLoaded(B
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,以便通过intent共享 这是电话号码
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水印后的图像